Skip to content

Commit e5648a9

Browse files
committed
feat(mergeProps): utility to merge component props and global config
1 parent ea6698f commit e5648a9

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export { default as useEvent } from './hooks/useEvent';
22
export { default as useMergedState } from './hooks/useMergedState';
3+
export { default as mergeProps } from './mergeProps';
34
export { supportNodeRef, supportRef, useComposeRef } from './ref';
45
export { default as get } from './utils/get';
56
export { default as set } from './utils/set';

src/mergeProps.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default function mergeProps<A, B, C>(
2+
defaultProps: A,
3+
config: B,
4+
props: C,
5+
): A & B & C {
6+
return { ...defaultProps, ...config, ...props };
7+
}

tests/mergeProps.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import mergeProps from '../src/mergeProps';
2+
3+
test('merge className', () => {
4+
expect(
5+
mergeProps(
6+
{ type: 'default', shape: 'round' },
7+
{ className: 'foo', type: 'secondary' },
8+
{ className: 'bar' },
9+
),
10+
).toEqual({
11+
className: 'bar',
12+
type: 'secondary',
13+
shape: 'round',
14+
});
15+
});

0 commit comments

Comments
 (0)