react-if-then-else-switch
Version:
A React library for declarative conditional rendering using If/Else and Switch/Case components for cleaner JSX.
3 lines (2 loc) • 1.38 kB
JavaScript
var e=require("react/jsx-runtime"),r=require("react");const IfElse=({children:e})=>{const l=r.Children.toArray(e);let s;const t=[];let n;if(l.forEach((e=>{r.isValidElement(e)&&("If"===e.type.displayName?s||(s=e):"ElIf"===e.type.displayName?t.push(e):"Else"===e.type.displayName&&(n||(n=e)))})),!s)return null;if(s.props.condition)return s.props.children;for(const e of t)if(e.props.condition)return e.props.children;return n?n.props.children:null};IfElse.displayName="IfElse";const If=({children:r})=>e.jsx(e.Fragment,{children:r});If.displayName="If";const ElIf=({children:r})=>e.jsx(e.Fragment,{children:r});ElIf.displayName="ElIf";const Else=({children:r})=>e.jsx(e.Fragment,{children:r});Else.displayName="Else";const l=r.createContext(null),Switch=({value:s,children:t})=>{let n=null,i=null;return r.Children.forEach(t,(e=>{r.isValidElement(e)&&("Case"!==e.type.displayName||e.props.when!==s||n?"Default"!==e.type.displayName||i||(i=e):n=e)})),e.jsx(l.Provider,{value:s,children:n||i})};Switch.displayName="Switch";const Case=({children:r})=>e.jsx(e.Fragment,{children:r});Case.displayName="Case";const Default=({children:r})=>e.jsx(e.Fragment,{children:r});Default.displayName="Default",exports.Case=Case,exports.Default=Default,exports.ElIf=ElIf,exports.Else=Else,exports.If=If,exports.IfElse=IfElse,exports.Switch=Switch;
//# sourceMappingURL=index.js.map
;