Skip to content

Commit

Permalink
fixup! Introduce usage of nested RUIProvider (#541)
Browse files Browse the repository at this point in the history
  • Loading branch information
bedrich-schindler committed Jun 6, 2024
1 parent 1361b70 commit 3698da4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
42 changes: 41 additions & 1 deletion src/docs/customize/global-props.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,45 @@ props across whole application and then override some of them in a specific
part of the application.

When nested `RUIProvider` is used, the props are merged deeply together. This
means that you can extend specific object with new props or override existing
means that you can extend specific object with new props or override existing
ones. If you need to remove some prop, you can set it to `undefined`.

```docoff-react-preview
React.createElement(() => {
const [variant, setVariant] = React.useState('filled');
return (
<RUIProvider globalProps={{
Grid: {
columns: {
xs: '1fr',
md: '1fr 1fr',
},
justifyItems: 'center',
rows: {
xs: '50px',
md: '100px',
},
},
}}>
<RUIProvider globalProps={{
Grid: {
columns: {
sm: '1fr 1fr 1fr',
},
justifyItems: 'undefined',
rows: undefined,
},
}}>
<Grid>
<docoff-placeholder bordered>Grid item</docoff-placeholder>
<docoff-placeholder bordered>Grid item</docoff-placeholder>
<docoff-placeholder bordered>Grid item</docoff-placeholder>
<docoff-placeholder bordered>Grid item</docoff-placeholder>
<docoff-placeholder bordered>Grid item</docoff-placeholder>
<docoff-placeholder bordered>Grid item</docoff-placeholder>
</Grid>
</RUIProvider>
</RUIProvider>
);
});
```
6 changes: 3 additions & 3 deletions src/utils/__tests__/mergeDeep.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('mergeDeep', () => {
},
state: {
items: [3, 4, 5],
itemsSize: 5,
itemsSize: 3,
},
};
const expectedObj = {
Expand All @@ -70,8 +70,8 @@ describe('mergeDeep', () => {
},
},
state: {
items: [1, 2, 3, 4, 5],
itemsSize: 5,
items: [3, 4, 5],
itemsSize: 3,
},
};

Expand Down
6 changes: 2 additions & 4 deletions src/utils/mergeDeep.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const isObject = (obj) => obj && typeof obj === 'object';
const isObject = (obj) => obj && typeof obj === 'object' && !Array.isArray(obj);

/**
* Performs a deep merge of objects and returns new object.
Expand All @@ -17,9 +17,7 @@ export const mergeDeep = (...objects) => objects.reduce((prev, obj) => {
const pVal = prev[key];
const oVal = obj[key];

if (Array.isArray(pVal) && Array.isArray(oVal)) {
newObject[key] = pVal.concat(...oVal);
} else if (isObject(pVal) && isObject(oVal)) {
if (isObject(pVal) && isObject(oVal)) {
newObject[key] = mergeDeep(pVal, oVal);
} else {
newObject[key] = oVal;
Expand Down

0 comments on commit 3698da4

Please sign in to comment.