UNPKG

react-if-vz

Version:

Declarative conditional rendering for React — logic-first, TypeScript-native.

3 lines (2 loc) 3.96 kB
import n,{isValidElement as e,useRef as r}from"react";function t(n,e){"undefined"!=typeof console&&console.log(n,e)}var l="undefined"!=typeof process&&"production"!==process.env.NODE_ENV;function o(e,r){if(!r.wrapper)return e;var t=r.wrapper;return n.createElement(t,{className:r.className,style:r.style},e)}var u=function(){function e(n,e){void 0===n&&(n=t),void 0===e&&(e=!1),this.logger=n,this.traced=e,this.entries=[],this.fallback=null}return e.prototype.when=function(n,e){return this.entries.push({condition:n,node:e}),this},e.prototype.else=function(n){return this.fallback=n,this.render()},e.prototype.render=function(){var e=this,r=this.fallback;return this.entries.some((function(n,t){var o=h(n.condition,[]);return e.traced&&l&&e.logger("[Ifz.chain] when[".concat(t,"] → ").concat(o)),!!o&&(r=n.node,!0)})),n.createElement(n.Fragment,null,r)},e}(),c={logger:t,trace:!1};function i(n){n.logger&&(c.logger=n.logger),void 0!==n.trace&&(c.trace=n.trace)}function a(n){return null}function d(n){return null}function f(n){return n.children,null}function s(n){return n.children,null}function g(n,e){return"function"==typeof n?n.apply(void 0,e):n}function h(n,e){return Boolean(g(n,e))}function v(n,e){return"function"==typeof n?n.apply(void 0,e):n}function m(r,t){if(void 0===t&&(t=[]),!e(r))return r;var u=r,i=u.type,f=u.props;return i===a||i===d?h(f.condition,f.args||t)?(f.debug&&c.trace&&l&&c.logger("[Ifz] Rendered",{label:f.debug}),o(v(f.children,f.args||t),f)):null:i===p||i===s?n.createElement(p,null,f.children):r}function p(r){var t=r.children;if(!t)return null;for(var l=null,o=0,u=n.Children.toArray(t);o<u.length;o++){var c=u[o],i=m(c);if(null!=i)return n.createElement(n.Fragment,null,i);e(c)&&c.type===f&&null===l&&(l=v(c.props.children,[]))}return n.createElement(n.Fragment,null,l)}function E(e){var r=e.condition,t=e.children,l=e.args,o=null!=l?l:[];return h(r,o)?n.createElement(n.Fragment,null,v(t,o)):null}function y(e){var r=e.conditions,t=e.children;return r.every(Boolean)?n.createElement(n.Fragment,null,t):null}function F(e){var r=e.conditions,t=e.children;return r.some(Boolean)?n.createElement(n.Fragment,null,t):null}function w(e){var r=e.condition,t=e.children;return r?null:n.createElement(n.Fragment,null,t)}function b(r){for(var t,l=r.value,u=r.children,i=r.args,a=void 0===i?[]:i,d=n.Children.toArray(u),f=!(null==l),s=f?g(l,a):void 0,h=null,m=0,p=0,E=d;p<E.length;p++){var y=E[p];if(e(y)){var F=y,w=F.type,b=F.props;if(w===I){var B=g(b.value,a);if(b.match?b.match(s,B):f?s===B:Boolean(B))return c.trace&&(null===(t=c.logger)||void 0===t||t.call(c,"[Switch] case[".concat(m,"] matched"))),o(v(b.children,b.args||a),b);m++}if(w===z&&null==h)h=o(v(b.children,b.args||a),b)}}return n.createElement(n.Fragment,null,h)}function I(e){return n.createElement(n.Fragment,null,e.children)}function z(e){return n.createElement(n.Fragment,null,e.children)}function B(e,o){var u,c;void 0===e&&(e=[]);var i=r([]),a=r(!1),d=null!==(u=null==o?void 0:o.logger)&&void 0!==u?u:t,f=null!==(c=null==o?void 0:o.trace)&&void 0!==c&&c,s=function(r){if(a.current)return r;a.current=!0;var t=i.current;i.current=[];var o=r;return t.some((function(n,r){var t=h(n.condition,e);return f&&l&&d("[useIfz] when[".concat(r,"] → ").concat(t)),!!t&&(o=n.node,!0)})),n.createElement(n.Fragment,null,o)},g={when:function(n,e){return i.current.push({condition:n,node:e}),g},else:s,otherwise:s};return g}p.If=a,p.ElseIf=d,p.Else=f,p.ElseIfz=s,p.chain=function(n){return(null==n?void 0:n.logger)&&(c.logger=n.logger),void 0!==(null==n?void 0:n.trace)&&(c.trace=n.trace),new u(c.logger,c.trace)},b.Case=I,b.Default=z;export{I as Case,z as Default,f as Else,d as ElseIf,s as ElseIfz,a as If,p as Ifz,u as IfzChain,c as IfzConfig,b as Switch,E as When,y as WhenAll,F as WhenAny,w as WhenNot,i as configureIfz,t as defaultLogger,h as execIfCondition,g as execValue,l as isDev,v as renderChildren,B as useIfz,o as wrapper}; //# sourceMappingURL=index.esm.js.map