1 | import React, { useRef } from 'react';
|
2 | import PropTypes from 'prop-types';
|
3 | import classNames from 'classnames';
|
4 | import { Transition } from 'react-transition-group';
|
5 | import {
|
6 | addDefaultProps,
|
7 | mapToCssModules,
|
8 | omit,
|
9 | pick,
|
10 | tagPropType,
|
11 | TransitionPropTypeKeys,
|
12 | TransitionTimeouts,
|
13 | } from './utils';
|
14 |
|
15 | const propTypes = {
|
16 | ...Transition.propTypes,
|
17 | children: PropTypes.oneOfType([
|
18 | PropTypes.arrayOf(PropTypes.node),
|
19 | PropTypes.node,
|
20 | ]),
|
21 | tag: tagPropType,
|
22 | baseClass: PropTypes.string,
|
23 | baseClassActive: PropTypes.string,
|
24 | className: PropTypes.string,
|
25 | cssModule: PropTypes.object,
|
26 | innerRef: PropTypes.oneOfType([
|
27 | PropTypes.object,
|
28 | PropTypes.string,
|
29 | PropTypes.func,
|
30 | ]),
|
31 | };
|
32 |
|
33 | const defaultProps = {
|
34 | ...Transition.defaultProps,
|
35 | timeout: TransitionTimeouts.Fade,
|
36 | appear: true,
|
37 | enter: true,
|
38 | exit: true,
|
39 | in: true,
|
40 | };
|
41 |
|
42 | function Fade(props) {
|
43 | const ref = useRef(null);
|
44 |
|
45 | const {
|
46 | tag: Tag = 'div',
|
47 | baseClass = 'fade',
|
48 | baseClassActive = 'show',
|
49 | className,
|
50 | cssModule,
|
51 | children,
|
52 | innerRef = ref,
|
53 | ...otherProps
|
54 | } = addDefaultProps(defaultProps, props);
|
55 |
|
56 | const transitionProps = pick(
|
57 | { defaultProps, ...otherProps },
|
58 | TransitionPropTypeKeys,
|
59 | );
|
60 |
|
61 | const childProps = omit(otherProps, TransitionPropTypeKeys);
|
62 |
|
63 | return (
|
64 | <Transition nodeRef={innerRef} {...transitionProps}>
|
65 | {(status) => {
|
66 | const isActive = status === 'entered';
|
67 | const classes = mapToCssModules(
|
68 | classNames(className, baseClass, isActive && baseClassActive),
|
69 | cssModule,
|
70 | );
|
71 | return (
|
72 | <Tag className={classes} {...childProps} ref={innerRef}>
|
73 | {children}
|
74 | </Tag>
|
75 | );
|
76 | }}
|
77 | </Transition>
|
78 | );
|
79 | }
|
80 |
|
81 | Fade.propTypes = propTypes;
|
82 |
|
83 | export default Fade;
|