Skip to content

Commit 0988107

Browse files
committed
when adding a chipTheme, try and merge with the default theme in order to try and override already configured default
- added a deep merge function to handle this scenario
1 parent 3554f02 commit 0988107

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/Chip.js

+24-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import themeable from 'react-themeable';
55
import { chipTheme } from './theme';
66

77
const Chip = ({ selected, theme, onRemove, children }) => {
8-
const themr = themeable(theme);
8+
// if someone provided a theme, try and merge between the original and provided
9+
const mergedTheme = deepMerge(chipTheme, theme);
10+
const themr = themeable(mergedTheme);
911
return (
1012
<div {...themr(1, 'chip', selected ? 'chipSelected' : '')}>
1113
{children}
@@ -27,4 +29,25 @@ Chip.defaultProps = {
2729
selected: false,
2830
}
2931

32+
// from https://stackoverflow.com/a/49798508/1824521
33+
const deepMerge = (...sources) => {
34+
let acc = {}
35+
for (const source of sources) {
36+
if (source instanceof Array) {
37+
if (!(acc instanceof Array)) {
38+
acc = []
39+
}
40+
acc = [...acc, ...source]
41+
} else if (source instanceof Object) {
42+
for (let [key, value] of Object.entries(source)) {
43+
if (value instanceof Object && key in acc) {
44+
value = deepMerge(acc[key], value)
45+
}
46+
acc = { ...acc, [key]: value }
47+
}
48+
}
49+
}
50+
return acc
51+
}
52+
3053
export default Radium(Chip);

0 commit comments

Comments
 (0)