Skip to content

Commit 5172799

Browse files
Fix Pagination story config and array merge strategy in BentoProvider
1 parent d7bdfda commit 5172799

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

packages/bento-design-system/src/BentoConfigContext.tsx

+14-11
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@ export function useBentoConfig() {
1010
return useContext(BentoConfigContext);
1111
}
1212

13+
export const deepmerge = deepmergeCustom({
14+
mergeRecords: (value, utils) => {
15+
// NOTE(vince): in case of a JSX.Element in the config (like the Navigation's activeVisualElement),
16+
// we don't want to merge the props of the two elements, but we want to take just the second element instead.
17+
if (React.isValidElement(value[0]) || React.isValidElement(value[1])) {
18+
return value[1];
19+
}
20+
return utils.actions.defaultMerge;
21+
},
22+
// NOTE(fede): in case of an array in the config (like AreaLoader's dots),
23+
// we don't want to merge the two arrays, but we want to take just the second one instead.
24+
mergeArrays: (value) => value[1],
25+
});
26+
1327
export function BentoConfigProvider({
1428
value: config,
1529
children,
@@ -23,17 +37,6 @@ export function BentoConfigProvider({
2337
// in case this is the top level provider.
2438
const parentConfig = useBentoConfig();
2539

26-
const deepmerge = deepmergeCustom({
27-
mergeRecords: (value, utils) => {
28-
// NOTE(vince): in case of a JSX.Element in the config (like the Navigation's activeVisualElement),
29-
// we don't want to merge the props of the two elements, but we want to take just the second element instead.
30-
if (React.isValidElement(value[0]) || React.isValidElement(value[1])) {
31-
return value[1];
32-
}
33-
return utils.actions.defaultMerge;
34-
},
35-
});
36-
3740
return (
3841
<BentoConfigContext.Provider value={deepmerge(parentConfig, config) as BentoConfig}>
3942
{children}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { deepmerge } from "../src/BentoConfigContext";
2+
3+
describe("deepmerge", () => {
4+
it("merges two objects", () => {
5+
const input1 = { a: 1, b: 2 };
6+
const input2 = { c: 3, d: 4 };
7+
const output = { a: 1, b: 2, c: 3, d: 4 };
8+
expect(deepmerge(input1, input2)).toEqual(output);
9+
});
10+
11+
it("does not merge two arrays", () => {
12+
const input1 = [1, 2, 3];
13+
const input2 = [4, 5, 6];
14+
expect(deepmerge(input1, input2)).toEqual(input2);
15+
});
16+
17+
it("does not merge object fields that are valid React elements", () => {
18+
const input1 = { a: 1, b: 2 };
19+
const input2 = { a: <div>hello</div>, b: 4 };
20+
expect(deepmerge(input1, input2)).toEqual(input2);
21+
});
22+
});

packages/storybook/stories/index.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ export const BentoProvider = createBentoProvider(
140140
custom: "customColor1",
141141
},
142142
},
143+
pagination: {
144+
itemsPerPageOptions: [5, 10, 20, 50],
145+
},
143146
},
144147
sprinkles
145148
);

0 commit comments

Comments
 (0)