Skip to content

Commit a5bee21

Browse files
utnapischtimThiefMaster
authored andcommitted
feat: add 0:n override
* this change enables to add multiple components to one override id
1 parent 317f0b4 commit a5bee21

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/overridable.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,20 @@ function Overridable({id, children, ...restProps}) {
4343

4444
if (id in overriddenComponents) {
4545
// If there's an override, we replace the component's content with the override + props
46-
const Overridden = overriddenComponents[id];
47-
const element = React.createElement(Overridden, {...childProps, ...restProps});
48-
return <DevModeWrapper id={id}>{element}</DevModeWrapper>;
46+
47+
if (Array.isArray(overriddenComponents[id])) {
48+
// if the override is an array we display all of them instead of the possible default children
49+
const overrides = overriddenComponents[id];
50+
const elements = overrides.map((overridden, i) =>
51+
React.createElement(overridden, {...childProps, ...restProps, key: `${id}-${i}`})
52+
);
53+
return <DevModeWrapper id={id}>{elements}</DevModeWrapper>;
54+
} else {
55+
// if it is not an array if will be used to replace it the possible default child
56+
const Overridden = overriddenComponents[id];
57+
const element = React.createElement(Overridden, {...childProps, ...restProps});
58+
return <DevModeWrapper id={id}>{element}</DevModeWrapper>;
59+
}
4960
} else if (child) {
5061
// No override? Clone the Overridable component's original children
5162
const element = React.cloneElement(child, childProps);

src/store.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ export class OverriddenComponentRepository {
1111
this.components[id] = Component;
1212
};
1313

14+
append = (id, Component) => {
15+
if (!Array.isArray(this.components[id])) {
16+
this.components[id] = [];
17+
}
18+
this.components[id].push(Component);
19+
};
20+
1421
get = id => {
1522
return this.components[id];
1623
};

0 commit comments

Comments
 (0)