Skip to content

Commit deed7b2

Browse files
authoredSep 26, 2024··
chore(fixes #2801): use function default parameters instead of defaultProps (#2806)
1 parent 717a5e8 commit deed7b2

File tree

4 files changed

+80
-7
lines changed

4 files changed

+80
-7
lines changed
 

‎src/Fade.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import PropTypes from 'prop-types';
33
import classNames from 'classnames';
44
import { Transition } from 'react-transition-group';
55
import {
6+
addDefaultProps,
67
mapToCssModules,
78
omit,
89
pick,
10+
tagPropType,
911
TransitionPropTypeKeys,
1012
TransitionTimeouts,
11-
tagPropType,
1213
} from './utils';
1314

1415
const propTypes = {
@@ -50,12 +51,13 @@ function Fade(props) {
5051
children,
5152
innerRef = ref,
5253
...otherProps
53-
} = props;
54+
} = addDefaultProps(defaultProps, props);
5455

5556
const transitionProps = pick(
5657
{ defaultProps, ...otherProps },
5758
TransitionPropTypeKeys,
5859
);
60+
5961
const childProps = omit(otherProps, TransitionPropTypeKeys);
6062

6163
return (
@@ -77,6 +79,5 @@ function Fade(props) {
7779
}
7880

7981
Fade.propTypes = propTypes;
80-
Fade.defaultProps = defaultProps;
8182

8283
export default Fade;

‎src/Tooltip.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import classNames from 'classnames';
33
import TooltipPopoverWrapper, { propTypes } from './TooltipPopoverWrapper';
4+
import { addDefaultProps } from './utils';
45

56
const defaultProps = {
67
placement: 'top',
@@ -14,9 +15,11 @@ function Tooltip(props) {
1415
const popperClasses = classNames('tooltip', 'show', props.popperClassName);
1516
const classes = classNames('tooltip-inner', props.innerClassName);
1617

18+
const _props = addDefaultProps(defaultProps, props);
19+
1720
return (
1821
<TooltipPopoverWrapper
19-
{...props}
22+
{..._props}
2023
arrowClassName={arrowClasses}
2124
popperClassName={popperClasses}
2225
innerClassName={classes}
@@ -25,6 +28,5 @@ function Tooltip(props) {
2528
}
2629

2730
Tooltip.propTypes = propTypes;
28-
Tooltip.defaultProps = defaultProps;
2931

3032
export default Tooltip;

‎src/__tests__/utils.spec.js

+50
Original file line numberDiff line numberDiff line change
@@ -234,19 +234,22 @@ describe('Utils', () => {
234234
describe('isFunction', () => {
235235
it('should return `true` for functions', () => {
236236
function test() {}
237+
237238
expect(Utils.isFunction(test)).toBe(true);
238239
expect(Utils.isFunction(Array.prototype.slice)).toBe(true);
239240
});
240241

241242
it('should return `true` for async functions', () => {
242243
async function asyncFunc() {}
244+
243245
expect(Utils.isFunction(asyncFunc)).toEqual(
244246
typeof asyncFunc === 'function',
245247
);
246248
});
247249

248250
it('should return `true` for generator functions', () => {
249251
function* genFunc() {}
252+
250253
expect(Utils.isFunction(genFunc)).toEqual(typeof genFunc === 'function');
251254
});
252255

@@ -256,6 +259,7 @@ describe('Utils', () => {
256259
return arguments;
257260
}.apply(undefined, array);
258261
}
262+
259263
expect(Utils.isFunction(toArgs([1, 2, 3]))).toBe(false);
260264
expect(Utils.isFunction([1, 2, 3])).toBe(false);
261265
expect(Utils.isFunction(true)).toBe(false);
@@ -309,6 +313,52 @@ describe('Utils', () => {
309313
});
310314
});
311315

316+
describe('addDefaultProps', () => {
317+
it('should return an object', () => {
318+
const defaultProps = {
319+
a: 1,
320+
b: 2,
321+
c: 3,
322+
};
323+
const props = {
324+
a: 4,
325+
b: 5,
326+
};
327+
expect(Utils.addDefaultProps(defaultProps, props)).toEqual(
328+
expect.any(Object),
329+
);
330+
});
331+
332+
it('should return an object with default props', () => {
333+
const defaultProps = {
334+
a: 1,
335+
b: 2,
336+
c: 3,
337+
d: {
338+
e: 4,
339+
f: 5,
340+
},
341+
};
342+
const props = {
343+
a: 4,
344+
b: 5,
345+
d: {
346+
e: 6,
347+
f: 5,
348+
},
349+
};
350+
expect(Utils.addDefaultProps(defaultProps, props)).toEqual({
351+
a: 4,
352+
b: 5,
353+
c: 3,
354+
d: {
355+
e: 6,
356+
f: 5,
357+
},
358+
});
359+
});
360+
});
361+
312362
// TODO
313363
// describe('getScrollbarWidth', () => {
314364
// // jsdom workaround https://github.com/tmpvar/jsdom/issues/135#issuecomment-68191941

‎src/utils.js

+22-2
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,8 @@ export function toNumber(value) {
264264
return isBinary || /^0o[0-7]+$/i.test(value)
265265
? parseInt(value.slice(2), isBinary ? 2 : 8)
266266
: /^[-+]0x[0-9a-f]+$/i.test(value)
267-
? NAN
268-
: +value;
267+
? NAN
268+
: +value;
269269
}
270270

271271
export function isFunction(value) {
@@ -381,3 +381,23 @@ export const focusableElements = [
381381
'video[controls]',
382382
'[contenteditable]:not([contenteditable="false"])',
383383
];
384+
385+
export function addDefaultProps(defaultProps, props) {
386+
if (!defaultProps || !props) return props;
387+
388+
let result = { ...props };
389+
390+
Object.keys(defaultProps).forEach((key) => {
391+
if (result[key] === undefined) {
392+
result[key] = defaultProps[key];
393+
}
394+
if (
395+
Object.keys(defaultProps[key] || {}).length > 0 &&
396+
typeof defaultProps[key] === 'object'
397+
) {
398+
addDefaultProps(defaultProps[key], result);
399+
}
400+
});
401+
402+
return result;
403+
}

0 commit comments

Comments
 (0)
Please sign in to comment.