Skip to content

Commit e23dd3f

Browse files
committed
refactor: clean components with fade functionality
1 parent 96e98fe commit e23dd3f

File tree

7 files changed

+53
-120
lines changed

7 files changed

+53
-120
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
},
4040
"dependencies": {
4141
"@popperjs/core": "^2.4.0",
42+
"@coreui/utils": "1.2.4",
4243
"classnames": "^2.2.6",
4344
"core-js": "^3.6.5",
4445
"perfect-scrollbar": "^1.5.0",

src/CAlert.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ const CAlert = props => {
5656
return (
5757
<CFade
5858
{...alertTransition}
59-
tag="div"
6059
className={classes}
6160
in={Boolean(isOpen)}
6261
role="alert"

src/CCollapse.js

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,28 @@
1-
import React, {useState} from 'react';
2-
import PropTypes from 'prop-types';
3-
import classNames from 'classnames';
4-
import {
5-
omit,
6-
pick,
7-
TransitionTimeouts,
8-
TransitionPropTypeKeys,
9-
TransitionStatuses
10-
} from './Shared/helper.js';
11-
import {Transition} from 'react-transition-group';
1+
import React, {useState} from 'react'
2+
import PropTypes from 'prop-types'
3+
import classNames from 'classnames'
4+
import { pickByKeys } from '@coreui/utils/src'
5+
import { TransitionPropTypeKeys } from './Shared/helper.js'
6+
import { Transition } from 'react-transition-group'
127

138
const transitionStatusToClassHash = {
14-
[TransitionStatuses.ENTERING]: 'collapsing',
15-
[TransitionStatuses.ENTERED]: 'collapse show',
16-
[TransitionStatuses.EXITING]: 'collapsing',
17-
[TransitionStatuses.EXITED]: 'collapse',
9+
entering: 'collapsing',
10+
entered: 'collapse show',
11+
exiting: 'collapsing',
12+
exited: 'collapse'
1813
};
1914

20-
const getTransitionClass = status=>{
21-
return transitionStatusToClassHash[status] || 'collapse';
15+
const getTransitionClass = status => {
16+
return transitionStatusToClassHash[status] || 'collapse'
2217
}
2318

24-
const getHeight = node=>{
19+
const getHeight = node => {
2520
return node.scrollHeight;
2621
}
2722

2823
//component - CoreUI / CCollapse
2924

30-
const CCollapse = props=>{
25+
const CCollapse = props => {
3126

3227
const {
3328
children,
@@ -36,7 +31,7 @@ const CCollapse = props=>{
3631
innerRef,
3732
show,
3833
navbar,
39-
...otherProps
34+
...rest
4035
} = props;
4136

4237
const [height, setHeight] = useState(null);
@@ -68,9 +63,11 @@ const CCollapse = props=>{
6863
}
6964

7065
//render
71-
72-
const transitionProps = pick(otherProps, TransitionPropTypeKeys);
73-
const childProps = omit(otherProps, TransitionPropTypeKeys);
66+
const transitionProps = pickByKeys(rest, TransitionPropTypeKeys)
67+
const childPropKeys = Object.keys(rest).filter(attr => {
68+
return !TransitionPropTypeKeys.includes(attr)
69+
})
70+
const childProps = pickByKeys(rest, childPropKeys)
7471

7572
return (
7673
<Transition
@@ -83,13 +80,13 @@ const CCollapse = props=>{
8380
onExited={onExited}
8481
>
8582
{(status) => {
86-
let collapseClass = getTransitionClass(status);
83+
let collapseClass = getTransitionClass(status)
8784
const classes = classNames(
8885
className,
8986
collapseClass,
9087
navbar && 'navbar-collapse'
9188
);
92-
const style = height === null ? null : { height };
89+
const style = height === null ? null : { height }
9390
return (
9491
<div
9592
{...childProps}
@@ -99,11 +96,10 @@ const CCollapse = props=>{
9996
>
10097
{children}
10198
</div>
102-
);
99+
)
103100
}}
104101
</Transition>
105-
);
106-
102+
)
107103
}
108104

109105
CCollapse.propTypes = {
@@ -125,7 +121,7 @@ CCollapse.defaultProps = {
125121
appear: false,
126122
enter: true,
127123
exit: true,
128-
timeout: TransitionTimeouts.Collapse,
124+
timeout: 350,
129125
};
130126

131-
export default CCollapse;
127+
export default CCollapse

src/CFade.js

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import React from 'react';
2-
import PropTypes from 'prop-types';
3-
import classNames from 'classnames';
4-
import { omit, pick, TransitionPropTypeKeys, TransitionTimeouts, tagPropType} from './Shared/helper.js';
5-
import {Transition} from 'react-transition-group';
1+
import React from 'react'
2+
import PropTypes from 'prop-types'
3+
import classNames from 'classnames'
4+
import { pickByKeys } from '@coreui/utils/src'
5+
import { TransitionPropTypeKeys, tagPropType } from './Shared/helper.js'
6+
import { Transition } from 'react-transition-group'
67

78
//component - CoreUI / CFade
8-
9-
const CFade = props=>{
9+
const CFade = props => {
1010

1111
const {
1212
tag: Tag,
@@ -16,32 +16,33 @@ const CFade = props=>{
1616
innerRef,
1717
baseClass,
1818
baseClassActive,
19-
...attributes
20-
} = props;
19+
...rest
20+
} = props
2121

2222
//render
23-
24-
const transitionProps = pick(attributes, TransitionPropTypeKeys);
25-
const childProps = omit(attributes, TransitionPropTypeKeys);
23+
const transitionProps = pickByKeys(rest, TransitionPropTypeKeys)
24+
const childPropKeys = Object.keys(rest).filter(key => {
25+
return !TransitionPropTypeKeys.includes(key)
26+
})
27+
const childProps = pickByKeys(rest, childPropKeys)
2628

2729
return (
2830
<Transition {...transitionProps}>
2931
{(status) => {
30-
const isActive = status === 'entered';
32+
const isActive = status === 'entered'
3133
const classes = classNames(
3234
className,
3335
baseClass,
3436
isActive && baseClassActive
35-
);
37+
)
3638
return (
3739
<Tag className={classes} {...childProps} ref={innerRef}>
3840
{children}
3941
</Tag>
40-
);
42+
)
4143
}}
4244
</Transition>
43-
);
44-
45+
)
4546
}
4647

4748
CFade.propTypes = {
@@ -64,11 +65,11 @@ CFade.defaultProps = {
6465
//
6566
baseClass: 'fade',
6667
baseClassActive: 'show',
67-
timeout: TransitionTimeouts.Fade,
68+
timeout: 150,
6869
appear: true,
6970
enter: true,
7071
exit: true,
71-
in: true,
72+
in: true
7273
};
7374

74-
export default CFade;
75+
export default CFade

src/CTabPane.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ const CTabPane = props => {
3939

4040
return (
4141
<CFade
42-
tag="div"
4342
in={isActive}
4443
baseClass={context && context.fade ? 'fade' : ''}
4544
className={classes}

src/CToast.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ const CToast = props => {
9393
<Context.Provider value={{ close }}>
9494
{
9595
state && <CFade
96-
tag={'div'}
9796
className={classes}
9897
role="alert"
9998
aria-live="assertive"

src/Shared/helper.js

Lines changed: 5 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,4 @@
1-
import PropTypes from 'prop-types';
2-
3-
4-
/* eslint key-spacing: ["error", { afterColon: true, align: "value" }] */
5-
// These are all setup to match what is in the bootstrap _variables.scss
6-
// https://github.com/twbs/bootstrap/blob/v4-dev/scss/_variables.scss
7-
export const TransitionTimeouts = {
8-
Fade: 150, // $transition-fade
9-
Collapse: 350, // $transition-collapse
10-
Modal: 300, // $modal-transition
11-
Carousel: 600, // $carousel-transition
12-
};
13-
14-
export const PopperPlacements = [
15-
'auto-start',
16-
'auto',
17-
'auto-end',
18-
'top-start',
19-
'top',
20-
'top-end',
21-
'right-start',
22-
'right',
23-
'right-end',
24-
'bottom-end',
25-
'bottom',
26-
'bottom-start',
27-
'left-end',
28-
'left',
29-
'left-start',
30-
];
1+
import PropTypes from 'prop-types'
312

323
// Duplicated Transition.propType keys to ensure that Reactstrap builds
334
// for distribution properly exclude these keys for nested child HTML attributes
@@ -45,16 +16,9 @@ export const TransitionPropTypeKeys = [
4516
'onEntered',
4617
'onExit',
4718
'onExiting',
48-
'onExited',
19+
'onExited'
4920
];
5021

51-
export const TransitionStatuses = {
52-
ENTERING: 'entering',
53-
ENTERED: 'entered',
54-
EXITING: 'exiting',
55-
EXITED: 'exited',
56-
};
57-
5822
export const canUseDOM = !!(
5923
typeof window !== 'undefined' &&
6024
window.document &&
@@ -68,7 +32,7 @@ export const targetPropType = PropTypes.oneOfType([
6832
PropTypes.shape({ current: PropTypes.any }),
6933
]);
7034

71-
const tagPropType = PropTypes.oneOfType([
35+
export const tagPropType = PropTypes.oneOfType([
7236
PropTypes.func,
7337
PropTypes.string,
7438
PropTypes.shape({ $$typeof: PropTypes.symbol, render: PropTypes.func }),
@@ -79,30 +43,6 @@ const tagPropType = PropTypes.oneOfType([
7943
]))
8044
]);
8145

82-
export function omit(obj, omitKeys) {
83-
const result = {};
84-
Object.keys(obj).forEach(key => {
85-
if (omitKeys.indexOf(key) === -1) {
86-
result[key] = obj[key];
87-
}
88-
});
89-
return result;
90-
}
91-
92-
export function pick(obj, keys) {
93-
const pickKeys = Array.isArray(keys) ? keys : [keys];
94-
let length = pickKeys.length;
95-
let key;
96-
const result = {};
97-
98-
while (length > 0) {
99-
length -= 1;
100-
key = pickKeys[length];
101-
result[key] = obj[key];
102-
}
103-
return result;
104-
}
105-
10646

10747
export function DOMElement(props, propName, componentName) {
10848
if (!(props[propName] instanceof Element)) {
@@ -116,7 +56,7 @@ export function DOMElement(props, propName, componentName) {
11656
}
11757
}
11858

119-
function deprecated(propType, explanation) {
59+
export function deprecated(propType, explanation) {
12060
return function validate(props, propName, componentName, ...rest) {
12161
if (props[propName] !== null && typeof props[propName] !== 'undefined') {
12262
console.error(
@@ -125,6 +65,4 @@ function deprecated(propType, explanation) {
12565
}
12666
return propType(props, propName, componentName, ...rest);
12767
}
128-
}
129-
130-
export { tagPropType, deprecated }
68+
}

0 commit comments

Comments
 (0)