🚀 Big News:Socket Has Acquired Secure Annex.Learn More
Socket
Book a DemoSign in
Socket

react-overridable

Package Overview
Dependencies
Maintainers
3
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-overridable - npm Package Compare versions

Comparing version
0.0.2
to
0.0.3
+13
CHANGELOG.md
## 0.0.3 (May 15, 2020)
* Remove 'use strict' directive from the built CommonJS module dist,
which was wrong and breaking Babel runtime helper. Sites using
Content-Security-Policies headers were reporting 'unsafe-eval' error.
## 0.0.2 (May 12, 2020)
* Add missing 'main' field to package.json.
## 0.0.1 (May 12, 2020)
* Initial public release.
+4
-9

@@ -1,3 +0,1 @@

'use strict';
Object.defineProperty(exports, '__esModule', { value: true });

@@ -59,10 +57,7 @@

const overriddenComponents = React.useContext(OverridableContext);
const child = children ? React__default.Children.only(children) : null; // throws an error if multiple children
const child = children ? React__default.Children.only(children) : null;
const childProps = child ? child.props : {};
const hasOverriddenComponent = (id in overriddenComponents);
if (hasOverriddenComponent) {
// If there's an override, we replace the component's content with
// the override + props
if (id in overriddenComponents) {
// If there's an override, we replace the component's content with the override + props
const Overridden = overriddenComponents[id];

@@ -145,6 +140,6 @@ return React__default.createElement(Overridden, _objectSpread(_objectSpread({}, childProps), restProps));

}
const overrideStore = new OverriddenComponentRepository();
exports.OverridableContext = OverridableContext;
exports.OverriddenComponentRepository = OverriddenComponentRepository;
exports.default = Overridable;

@@ -151,0 +146,0 @@ exports.overrideStore = overrideStore;

@@ -1,1 +0,1 @@

{"version":3,"file":"index.js","sources":["../../src/overridable.js","../../src/store.js"],"sourcesContent":["import React, {useContext} from 'react';\nimport PropTypes from 'prop-types';\n\n// create a new context with an empty map of overridden components as default value.\nexport const OverridableContext = React.createContext({});\n\n/**\n * Function to wrap a React component and override default props.\n * @param Component the component to wrap\n * @param extraProps the new props that will override or will be added to the wrapped component\n * @returns the wrapper component\n */\nexport function parametrize(Component, extraProps) {\n const ParametrizedComponent = props => {\n // handle deferred prop calculation\n if (typeof extraProps === 'function') {\n extraProps = extraProps(props);\n }\n\n // Store the original component in an attribute\n if (Component.originalComponent) {\n Component = Component.originalComponent;\n }\n\n // overrideProps override props if there is a name collision\n const {children, ...attrProps} = {...props, ...extraProps};\n return React.createElement(Component, attrProps, children);\n };\n\n const name = Component.displayName || Component.name;\n ParametrizedComponent.displayName = `Parametrized(${name})`;\n return ParametrizedComponent;\n}\n\n/**\n * React component to enable overriding children when rendering.\n */\nfunction Overridable({id, children, ...restProps}) {\n const overriddenComponents = useContext(OverridableContext);\n const child = children ? React.Children.only(children) : null; // throws an error if multiple children\n const childProps = child ? child.props : {};\n\n const hasOverriddenComponent = id in overriddenComponents;\n if (hasOverriddenComponent) {\n // If there's an override, we replace the component's content with\n // the override + props\n const Overridden = overriddenComponents[id];\n return React.createElement(Overridden, {...childProps, ...restProps});\n } else if (child) {\n // No override? Clone the Overridable component's original children\n return React.cloneElement(child, childProps);\n } else {\n return null;\n }\n}\n\nOverridable.propTypes = {\n /** The children of the component */\n children: PropTypes.node,\n /** The id that the component will be bound to (normally component's name) */\n id: PropTypes.string,\n};\n\nOverridable.defaultProps = {\n id: null,\n children: null,\n};\n\n/**\n * High-order component to override an existing React component and provide a new component instead.\n */\nOverridable.component = (id, Component) => {\n const Overridden = ({children, ...props}) => {\n const overriddenComponents = useContext(OverridableContext);\n const overriddenComponent = overriddenComponents[id];\n return React.createElement(overriddenComponent || Component, props, children);\n };\n Overridden.propTypes = {\n children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),\n };\n Overridden.defaultProps = {\n children: null,\n };\n const name = Component.displayName || Component.name;\n Overridden.displayName = `Overridable(${name})`;\n Overridden.originalComponent = Component;\n return Overridden;\n};\n\nexport default Overridable;\n","/**\n * Simple utility class responsible of keeping track of all overridden components.\n * @constructor object containing the initial map `id: Component` of overridden components\n */\nclass OverriddenComponentRepository {\n constructor(overriddenComponents) {\n this.components = overriddenComponents || {};\n }\n\n add = (id, Component) => {\n this.components[id] = Component;\n };\n\n get = id => {\n return this.components[id];\n };\n\n getAll = () => {\n return {...this.components};\n };\n\n clear = () => {\n this.components = {};\n };\n}\n\nexport const overrideStore = new OverriddenComponentRepository();\n"],"names":["OverridableContext","React","createContext","parametrize","Component","extraProps","ParametrizedComponent","props","originalComponent","children","attrProps","createElement","name","displayName","Overridable","id","restProps","overriddenComponents","useContext","child","Children","only","childProps","hasOverriddenComponent","Overridden","cloneElement","propTypes","PropTypes","node","string","defaultProps","component","overriddenComponent","oneOfType","func","OverriddenComponentRepository","constructor","components","overrideStore"],"mappings":";;;;;;;;;;;;;;;;MAIaA,kBAAkB,GAAGC,cAAK,CAACC,aAAN,CAAoB,EAApB;AAElC;;;;;;;AAMO,SAASC,WAAT,CAAqBC,SAArB,EAAgCC,UAAhC,EAA4C;AACjD,QAAMC,qBAAqB,GAAGC,KAAK,IAAI;AACrC;AACA,QAAI,OAAOF,UAAP,KAAsB,UAA1B,EAAsC;AACpCA,MAAAA,UAAU,GAAGA,UAAU,CAACE,KAAD,CAAvB;AACD,KAJoC;;;AAOrC,QAAIH,SAAS,CAACI,iBAAd,EAAiC;AAC/BJ,MAAAA,SAAS,GAAGA,SAAS,CAACI,iBAAtB;AACD,KAToC;;;AAAA,8DAYAD,KAZA,GAYUF,UAZV;AAAA,UAY9BI,QAZ8B,qBAY9BA,QAZ8B;AAAA,UAYjBC,SAZiB;;AAarC,WAAOT,cAAK,CAACU,aAAN,CAAoBP,SAApB,EAA+BM,SAA/B,EAA0CD,QAA1C,CAAP;AACD,GAdD;;AAgBA,QAAMG,IAAI,GAAGR,SAAS,CAACS,WAAV,IAAyBT,SAAS,CAACQ,IAAhD;AACAN,EAAAA,qBAAqB,CAACO,WAAtB,0BAAoDD,IAApD;AACA,SAAON,qBAAP;AACD;AAED;;;;AAGA,SAASQ,WAAT,OAAmD;AAAA,MAA7BC,EAA6B,QAA7BA,EAA6B;AAAA,MAAzBN,QAAyB,QAAzBA,QAAyB;AAAA,MAAZO,SAAY;;AAC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AI5B,SAA3C,EAAsDG,KAAtD,EAA6DE,QAA7D,CAAP;AACD,GAJD;;AAKAe,EAAAA,UAAU,CAACE,SAAX,GAAuB;AACrBjB,IAAAA,QAAQ,EAAEkB,SAAS,CAACM,SAAV,CAAoB,CAACN,SAAS,CAACC,IAAX,EAAiBD,SAAS,CAACO,IAA3B,CAApB;AADW,GAAvB;AAGAV,EAAAA,UAAU,CAACM,YAAX,GAA0B;AACxBrB,IAAAA,QAAQ,EAAE;AADc,GAA1B;AAGA,QAAMG,IAAI,GAAGR,SAAS,CAACS,WAAV,IAAyBT,SAAS,CAACQ,IAAhD;AACAY,EAAAA,UAAU,CAACX,WAAX,yBAAwCD,IAAxC;AACAY,EAAAA,UAAU,CAAChB,iBAAX,GAA+BJ,SAA/B;AACA,SAAOoB,UAAP;AACD,CAhBD;;;;;;ACvEA;;;;AAIA,MAAMW,6BAAN,CAAoC;AAClCC,EAAAA,WAAW,CAACnB,oBAAD,EAAuB;AAAA,iCAI5B,CAACF,EAAD,EAAKX,SAAL,KAAmB;AACvB,WAAKiC,UAAL,CAAgBtB,EAAhB,IAAsBX,SAAtB;AACD,KANiC;;AAAA,iCAQ5BW,EAAE,IAAI;AACV,aAAO,KAAKsB,UAAL,CAAgBtB,EAAhB,CAAP;AACD,KAViC;;AAAA,oCAYzB,MAAM;AACb,iCAAW,KAAKsB,UAAhB;AACD,KAdiC;;AAAA,mCAgB1B,MAAM;AACZ,WAAKA,UAAL,GAAkB,EAAlB;AACD,KAlBiC;;AAChC,SAAKA,UAAL,GAAkBpB,oBAAoB,IAAI,EAA1C;AACD;;AAHiC;;MAsBvBqB,aAAa,GAAG,IAAIH,6BAAJ;;;;;;;"}
{"version":3,"file":"index.js","sources":["../../src/overridable.js","../../src/store.js"],"sourcesContent":["import React, {useContext} from 'react';\nimport PropTypes from 'prop-types';\n\n// create a new context with an empty map of overridden components as default value.\nexport const OverridableContext = React.createContext({});\n\n/**\n * Function to wrap a React component and override default props.\n * @param Component the component to wrap\n * @param extraProps the new props that will override or will be added to the wrapped component\n * @returns the wrapper component\n */\nexport function parametrize(Component, extraProps) {\n const ParametrizedComponent = props => {\n // handle deferred prop calculation\n if (typeof extraProps === 'function') {\n extraProps = extraProps(props);\n }\n\n // Store the original component in an attribute\n if (Component.originalComponent) {\n Component = Component.originalComponent;\n }\n\n // overrideProps override props if there is a name collision\n const {children, ...attrProps} = {...props, ...extraProps};\n return React.createElement(Component, attrProps, children);\n };\n\n const name = Component.displayName || Component.name;\n ParametrizedComponent.displayName = `Parametrized(${name})`;\n return ParametrizedComponent;\n}\n\n/**\n * React component to enable overriding children when rendering.\n */\nfunction Overridable({id, children, ...restProps}) {\n const overriddenComponents = useContext(OverridableContext);\n const child = children ? React.Children.only(children) : null;\n const childProps = child ? child.props : {};\n\n if (id in overriddenComponents) {\n // If there's an override, we replace the component's content with the override + props\n const Overridden = overriddenComponents[id];\n return React.createElement(Overridden, {...childProps, ...restProps});\n } else if (child) {\n // No override? Clone the Overridable component's original children\n return React.cloneElement(child, childProps);\n } else {\n return null;\n }\n}\n\nOverridable.propTypes = {\n /** The children of the component */\n children: PropTypes.node,\n /** The id that the component will be bound to (normally component's name) */\n id: PropTypes.string,\n};\n\nOverridable.defaultProps = {\n id: null,\n children: null,\n};\n\n/**\n * High-order component to override an existing React component and provide a new component instead.\n */\nOverridable.component = (id, Component) => {\n const Overridden = ({children, ...props}) => {\n const overriddenComponents = useContext(OverridableContext);\n const overriddenComponent = overriddenComponents[id];\n return React.createElement(overriddenComponent || Component, props, children);\n };\n Overridden.propTypes = {\n children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),\n };\n Overridden.defaultProps = {\n children: null,\n };\n const name = Component.displayName || Component.name;\n Overridden.displayName = `Overridable(${name})`;\n Overridden.originalComponent = Component;\n return Overridden;\n};\n\nexport default Overridable;\n","/**\n * Simple utility class responsible of keeping track of all overridden components.\n * @constructor object containing the initial map `id: Component` of overridden components\n */\nexport class OverriddenComponentRepository {\n constructor(overriddenComponents) {\n this.components = overriddenComponents || {};\n }\n\n add = (id, Component) => {\n this.components[id] = Component;\n };\n\n get = id => {\n return this.components[id];\n };\n\n getAll = () => {\n return {...this.components};\n };\n\n clear = () => {\n this.components = {};\n };\n}\n\nexport const overrideStore = new OverriddenComponentRepository();\n"],"names":["OverridableContext","React","createContext","parametrize","Component","extraProps","ParametrizedComponent","props","originalComponent","children","attrProps","createElement","name","displayName","Overridable","id","restProps","overriddenComponents","useContext","child","Children","only","childProps","Overridden","cloneElement","propTypes","PropTypes","node","string","defaultProps","component","overriddenComponent","oneOfType","func","OverriddenComponentRepository","constructor","components","overrideStore"],"mappings":";;;;;;;;;;;;;;MAIaA,kBAAkB,GAAGC,cAAK,CAACC,aAAN,CAAoB,EAApB;AAElC;;;;;;;AAMO,SAASC,WAAT,CAAqBC,SAArB,EAAgCC,UAAhC,EAA4C;AACjD,QAAMC,qBAAqB,GAAGC,KAAK,IAAI;AACrC;AACA,QAAI,OAAOF,UAAP,KAAsB,UAA1B,EAAsC;AACpCA,MAAAA,UAAU,GAAGA,UAAU,CAACE,KAAD,CAAvB;AACD,KAJoC;;;AAOrC,QAAIH,SAAS,CAACI,iBAAd,EAAiC;AAC/BJ,MAAAA,SAAS,GAAGA,SAAS,CAACI,iBAAtB;AACD,KAToC;;;AAAA,8DAYAD,KAZA,GAYUF,UAZV;AAAA,UAY9BI,QAZ8B,qBAY9BA,QAZ8B;AAAA,UAYjBC,SAZiB;;AAarC,WAAOT,cAAK,CAACU,aAAN,CAAoBP,SAApB,EAA+BM,SAA/B,EAA0CD,QAA1C,CAAP;AACD,GAdD;;AAgBA,QAAMG,IAAI,GAAGR,SAAS,CAACS,WAAV,IAAyBT,SAAS,CAACQ,IAAhD;AACAN,EAAAA,qBAAqB,CAACO,WAAtB,0BAAoDD,IAApD;AACA,SAAON,qBAAP;AACD;AAED;;;;AAGA,SAASQ,WAAT,OAAmD;AAAA,MAA7BC,EAA6B,QAA7BA,EAA6B;AAAA,MAAzBN,QAAyB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G,KAAtD,EAA6DE,QAA7D,CAAP;AACD,GAJD;;AAKAc,EAAAA,UAAU,CAACE,SAAX,GAAuB;AACrBhB,IAAAA,QAAQ,EAAEiB,SAAS,CAACM,SAAV,CAAoB,CAACN,SAAS,CAACC,IAAX,EAAiBD,SAAS,CAACO,IAA3B,CAApB;AADW,GAAvB;AAGAV,EAAAA,UAAU,CAACM,YAAX,GAA0B;AACxBpB,IAAAA,QAAQ,EAAE;AADc,GAA1B;AAGA,QAAMG,IAAI,GAAGR,SAAS,CAACS,WAAV,IAAyBT,SAAS,CAACQ,IAAhD;AACAW,EAAAA,UAAU,CAACV,WAAX,yBAAwCD,IAAxC;AACAW,EAAAA,UAAU,CAACf,iBAAX,GAA+BJ,SAA/B;AACA,SAAOmB,UAAP;AACD,CAhBD;;;;;;ACrEA;;;;AAIO,MAAMW,6BAAN,CAAoC;AACzCC,EAAAA,WAAW,CAAClB,oBAAD,EAAuB;AAAA,iCAI5B,CAACF,EAAD,EAAKX,SAAL,KAAmB;AACvB,WAAKgC,UAAL,CAAgBrB,EAAhB,IAAsBX,SAAtB;AACD,KANiC;;AAAA,iCAQ5BW,EAAE,IAAI;AACV,aAAO,KAAKqB,UAAL,CAAgBrB,EAAhB,CAAP;AACD,KAViC;;AAAA,oCAYzB,MAAM;AACb,iCAAW,KAAKqB,UAAhB;AACD,KAdiC;;AAAA,mCAgB1B,MAAM;AACZ,WAAKA,UAAL,GAAkB,EAAlB;AACD,KAlBiC;;AAChC,SAAKA,UAAL,GAAkBnB,oBAAoB,IAAI,EAA1C;AACD;;AAHwC;MAsB9BoB,aAAa,GAAG,IAAIH,6BAAJ;;;;;;;;"}

@@ -52,10 +52,7 @@ import _defineProperty from '@babel/runtime/helpers/defineProperty';

const overriddenComponents = useContext(OverridableContext);
const child = children ? React.Children.only(children) : null; // throws an error if multiple children
const child = children ? React.Children.only(children) : null;
const childProps = child ? child.props : {};
const hasOverriddenComponent = (id in overriddenComponents);
if (hasOverriddenComponent) {
// If there's an override, we replace the component's content with
// the override + props
if (id in overriddenComponents) {
// If there's an override, we replace the component's content with the override + props
const Overridden = overriddenComponents[id];

@@ -138,7 +135,6 @@ return React.createElement(Overridden, _objectSpread(_objectSpread({}, childProps), restProps));

}
const overrideStore = new OverriddenComponentRepository();
export default Overridable;
export { OverridableContext, overrideStore, parametrize };
export { OverridableContext, OverriddenComponentRepository, overrideStore, parametrize };
//# sourceMappingURL=index.js.map

@@ -1,1 +0,1 @@

{"version":3,"file":"index.js","sources":["../../src/overridable.js","../../src/store.js"],"sourcesContent":["import React, {useContext} from 'react';\nimport PropTypes from 'prop-types';\n\n// create a new context with an empty map of overridden components as default value.\nexport const OverridableContext = React.createContext({});\n\n/**\n * Function to wrap a React component and override default props.\n * @param Component the component to wrap\n * @param extraProps the new props that will override or will be added to the wrapped component\n * @returns the wrapper component\n */\nexport function parametrize(Component, extraProps) {\n const ParametrizedComponent = props => {\n // handle deferred prop calculation\n if (typeof extraProps === 'function') {\n extraProps = extraProps(props);\n }\n\n // Store the original component in an attribute\n if (Component.originalComponent) {\n Component = Component.originalComponent;\n }\n\n // overrideProps override props if there is a name collision\n const {children, ...attrProps} = {...props, ...extraProps};\n return React.createElement(Component, attrProps, children);\n };\n\n const name = Component.displayName || Component.name;\n ParametrizedComponent.displayName = `Parametrized(${name})`;\n return ParametrizedComponent;\n}\n\n/**\n * React component to enable overriding children when rendering.\n */\nfunction Overridable({id, children, ...restProps}) {\n const overriddenComponents = useContext(OverridableContext);\n const child = children ? React.Children.only(children) : null; // throws an error if multiple children\n const childProps = child ? child.props : {};\n\n const hasOverriddenComponent = id in overriddenComponents;\n if (hasOverriddenComponent) {\n // If there's an override, we replace the component's content with\n // the override + props\n const Overridden = overriddenComponents[id];\n return React.createElement(Overridden, {...childProps, ...restProps});\n } else if (child) {\n // No override? Clone the Overridable component's original children\n return React.cloneElement(child, childProps);\n } else {\n return null;\n }\n}\n\nOverridable.propTypes = {\n /** The children of the component */\n children: PropTypes.node,\n /** The id that the component will be bound to (normally component's name) */\n id: PropTypes.string,\n};\n\nOverridable.defaultProps = {\n id: null,\n children: null,\n};\n\n/**\n * High-order component to override an existing React component and provide a new component instead.\n */\nOverridable.component = (id, Component) => {\n const Overridden = ({children, ...props}) => {\n const overriddenComponents = useContext(OverridableContext);\n const overriddenComponent = overriddenComponents[id];\n return React.createElement(overriddenComponent || Component, props, children);\n };\n Overridden.propTypes = {\n children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),\n };\n Overridden.defaultProps = {\n children: null,\n };\n const name = Component.displayName || Component.name;\n Overridden.displayName = `Overridable(${name})`;\n Overridden.originalComponent = Component;\n return Overridden;\n};\n\nexport default Overridable;\n","/**\n * Simple utility class responsible of keeping track of all overridden components.\n * @constructor object containing the initial map `id: Component` of overridden components\n */\nclass OverriddenComponentRepository {\n constructor(overriddenComponents) {\n this.components = overriddenComponents || {};\n }\n\n add = (id, Component) => {\n this.components[id] = Component;\n };\n\n get = id => {\n return this.components[id];\n };\n\n getAll = () => {\n return {...this.components};\n };\n\n clear = () => {\n this.components = {};\n };\n}\n\nexport const overrideStore = new OverriddenComponentRepository();\n"],"names":["OverridableContext","React","createContext","parametrize","Component","extraProps","ParametrizedComponent","props","originalComponent","children","attrProps","createElement","name","displayName","Overridable","id","restProps","overriddenComponents","useContext","child","Children","only","childProps","hasOverriddenComponent","Overridden","cloneElement","propTypes","PropTypes","node","string","defaultProps","component","overriddenComponent","oneOfType","func","OverriddenComponentRepository","constructor","components","overrideStore"],"mappings":";;;;;;;;;MAIaA,kBAAkB,GAAGC,KAAK,CAACC,aAAN,CAAoB,EAApB;AAElC;;;;;;;AAMO,SAASC,WAAT,CAAqBC,SAArB,EAAgCC,UAAhC,EAA4C;AACjD,QAAMC,qBAAqB,GAAGC,KAAK,IAAI;AACrC;AACA,QAAI,OAAOF,UAAP,KAAsB,UAA1B,EAAsC;AACpCA,MAAAA,UAAU,GAAGA,UAAU,CAACE,KAAD,CAAvB;AACD,KAJoC;;;AAOrC,QAAI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AgBtB,EAAhB,IAAsBX,SAAtB;AACD,KANiC;;AAAA,iCAQ5BW,EAAE,IAAI;AACV,aAAO,KAAKsB,UAAL,CAAgBtB,EAAhB,CAAP;AACD,KAViC;;AAAA,oCAYzB,MAAM;AACb,iCAAW,KAAKsB,UAAhB;AACD,KAdiC;;AAAA,mCAgB1B,MAAM;AACZ,WAAKA,UAAL,GAAkB,EAAlB;AACD,KAlBiC;;AAChC,SAAKA,UAAL,GAAkBpB,oBAAoB,IAAI,EAA1C;AACD;;AAHiC;;MAsBvBqB,aAAa,GAAG,IAAIH,6BAAJ;;;;;"}
{"version":3,"file":"index.js","sources":["../../src/overridable.js","../../src/store.js"],"sourcesContent":["import React, {useContext} from 'react';\nimport PropTypes from 'prop-types';\n\n// create a new context with an empty map of overridden components as default value.\nexport const OverridableContext = React.createContext({});\n\n/**\n * Function to wrap a React component and override default props.\n * @param Component the component to wrap\n * @param extraProps the new props that will override or will be added to the wrapped component\n * @returns the wrapper component\n */\nexport function parametrize(Component, extraProps) {\n const ParametrizedComponent = props => {\n // handle deferred prop calculation\n if (typeof extraProps === 'function') {\n extraProps = extraProps(props);\n }\n\n // Store the original component in an attribute\n if (Component.originalComponent) {\n Component = Component.originalComponent;\n }\n\n // overrideProps override props if there is a name collision\n const {children, ...attrProps} = {...props, ...extraProps};\n return React.createElement(Component, attrProps, children);\n };\n\n const name = Component.displayName || Component.name;\n ParametrizedComponent.displayName = `Parametrized(${name})`;\n return ParametrizedComponent;\n}\n\n/**\n * React component to enable overriding children when rendering.\n */\nfunction Overridable({id, children, ...restProps}) {\n const overriddenComponents = useContext(OverridableContext);\n const child = children ? React.Children.only(children) : null;\n const childProps = child ? child.props : {};\n\n if (id in overriddenComponents) {\n // If there's an override, we replace the component's content with the override + props\n const Overridden = overriddenComponents[id];\n return React.createElement(Overridden, {...childProps, ...restProps});\n } else if (child) {\n // No override? Clone the Overridable component's original children\n return React.cloneElement(child, childProps);\n } else {\n return null;\n }\n}\n\nOverridable.propTypes = {\n /** The children of the component */\n children: PropTypes.node,\n /** The id that the component will be bound to (normally component's name) */\n id: PropTypes.string,\n};\n\nOverridable.defaultProps = {\n id: null,\n children: null,\n};\n\n/**\n * High-order component to override an existing React component and provide a new component instead.\n */\nOverridable.component = (id, Component) => {\n const Overridden = ({children, ...props}) => {\n const overriddenComponents = useContext(OverridableContext);\n const overriddenComponent = overriddenComponents[id];\n return React.createElement(overriddenComponent || Component, props, children);\n };\n Overridden.propTypes = {\n children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),\n };\n Overridden.defaultProps = {\n children: null,\n };\n const name = Component.displayName || Component.name;\n Overridden.displayName = `Overridable(${name})`;\n Overridden.originalComponent = Component;\n return Overridden;\n};\n\nexport default Overridable;\n","/**\n * Simple utility class responsible of keeping track of all overridden components.\n * @constructor object containing the initial map `id: Component` of overridden components\n */\nexport class OverriddenComponentRepository {\n constructor(overriddenComponents) {\n this.components = overriddenComponents || {};\n }\n\n add = (id, Component) => {\n this.components[id] = Component;\n };\n\n get = id => {\n return this.components[id];\n };\n\n getAll = () => {\n return {...this.components};\n };\n\n clear = () => {\n this.components = {};\n };\n}\n\nexport const overrideStore = new OverriddenComponentRepository();\n"],"names":["OverridableContext","React","createContext","parametrize","Component","extraProps","ParametrizedComponent","props","originalComponent","children","attrProps","createElement","name","displayName","Overridable","id","restProps","overriddenComponents","useContext","child","Children","only","childProps","Overridden","cloneElement","propTypes","PropTypes","node","string","defaultProps","component","overriddenComponent","oneOfType","func","OverriddenComponentRepository","constructor","components","overrideStore"],"mappings":";;;;;;;;;MAIaA,kBAAkB,GAAGC,KAAK,CAACC,aAAN,CAAoB,EAApB;AAElC;;;;;;;AAMO,SAASC,WAAT,CAAqBC,SAArB,EAAgCC,UAAhC,EAA4C;AACjD,QAAMC,qBAAqB,GAAGC,KAAK,IAAI;AACrC;AACA,QAAI,OAAOF,UAAP,KAAsB,UAA1B,EAAsC;AACpCA,MAAAA,UAAU,GAAGA,UAAU,CAACE,KAAD,CAAv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ACD,KANiC;;AAAA,iCAQ5BW,EAAE,IAAI;AACV,aAAO,KAAKqB,UAAL,CAAgBrB,EAAhB,CAAP;AACD,KAViC;;AAAA,oCAYzB,MAAM;AACb,iCAAW,KAAKqB,UAAhB;AACD,KAdiC;;AAAA,mCAgB1B,MAAM;AACZ,WAAKA,UAAL,GAAkB,EAAlB;AACD,KAlBiC;;AAChC,SAAKA,UAAL,GAAkBnB,oBAAoB,IAAI,EAA1C;AACD;;AAHwC;MAsB9BoB,aAAa,GAAG,IAAIH,6BAAJ;;;;;"}
{
"name": "react-overridable",
"version": "0.0.2",
"version": "0.0.3",
"description": "Makes React components overridable.",

@@ -5,0 +5,0 @@ "keywords": [