@@ -5,7 +5,9 @@ import themeable from 'react-themeable';
5
5
import { chipTheme } from './theme' ;
6
6
7
7
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 ) ;
9
11
return (
10
12
< div { ...themr ( 1 , 'chip' , selected ? 'chipSelected' : '' ) } >
11
13
{ children }
@@ -27,4 +29,25 @@ Chip.defaultProps = {
27
29
selected : false ,
28
30
}
29
31
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
+
30
53
export default Radium ( Chip ) ;
0 commit comments