react-ifz
Version:
React wrapper conditions. Simple wrapper
2 lines (1 loc) • 2.79 kB
JavaScript
import n from"react";var e=function(){return e=Object.assign||function(n){for(var e,t=1,r=arguments.length;t<r;t++)for(var l in e=arguments[t])Object.prototype.hasOwnProperty.call(e,l)&&(n[l]=e[l]);return n},e.apply(this,arguments)},t=function(e){var t=e.condition,r=e.children;return i(t)?n.createElement(n.Fragment,null,r):null},r=function(e){var t=e.condition,r=e.children;if(!r)return null;var l=i(t),a=function(e,t){var r=[],l=[],a=[],s=!0;return n.Children.forEach(e,(function(e){if(n.isValidElement(e)){var o=e.props.condition;switch(e.type){case f:return void(t&&r.push(e));case c:return s=!1,void(!t&&i(o)&&l.push(e));case u:return s=!1,void(t||(s=!1,a.push(e)))}}})),o(r.length>1||l.length>1||a.length>1,"Multiple <Then /> or <ElseIf /> or <Else /> blocks found in <If />. Only the first will be rendered."),{tag:r[0]||l[0]||a[0],isSingle:s}}(r,l),s=a.tag,d=a.isSingle;return l&&d?n.createElement(n.Fragment,null,r):n.createElement(n.Fragment,null,s)},l=function(e){var t=e.children,r=e.condition;if(!t)return null;var l=function(e,t){var r=[],l=[];a(t)?n.Children.forEach(e,(function(e){if(n.isValidElement(e)){var t=i(e.props.condition);switch(e.type){case d:return void(t&&r.push(e));case s:return void l.push(e)}}})):n.Children.forEach(e,(function(e){if(n.isValidElement(e)){var a=i(e.props.condition)===t;switch(e.type){case d:return void(a&&r.push(e));case s:return void l.push(e)}}}));return o(r.length>1||l.length>1,"Must contain exactly one <Case /> or <Default /> block."),{tagCase:r[0],tagDefault:l[0]}}(t,i(r)),c=l.tagCase,u=l.tagDefault;return n.createElement(n.Fragment,null,c||u)};function i(n){return Boolean("function"==typeof n?n():n)}function a(n){return null==n}function o(n,e){if(n){"undefined"!=typeof console&&console.warn(e);try{throw new Error(e)}catch(n){}}}var c=function(t){return n.createElement(r,e({},t))},u=function(e){var t=e.children;return n.createElement(n.Fragment,null,t)},f=function(e){var t=e.children;return n.createElement(n.Fragment,null,t)},s=function(e){var t=e.children;return n.createElement(n.Fragment,null,t)},d=function(e){var t=e.children;return n.createElement(n.Fragment,null,t)},h=function(e){var r=e.condition,l=e.children;return n.createElement(t,{condition:r},l)},m=function(e){var t=e.condition,r=e.children,l=e.elseChildren;return i(t)?n.createElement(n.Fragment,null,r):n.createElement(n.Fragment,null,l)},g=function(e){var t=e.condition,r=e.children,l=e.elseIfChildren,a=e.elseIfCondition,o=e.elseChildren;return i(t)?n.createElement(n.Fragment,null,r):i(a)?n.createElement(n.Fragment,null,l):n.createElement(n.Fragment,null,o)};export{d as Case,s as Default,u as Else,c as ElseIf,r as If,h as IfCondition,m as IfElseCondition,g as IfElseIfCondition,l as Switch,f as Then,t as When,i as execConditions,a as isNil};