UNPKG

@yookue/react-condition

Version:

Render components conditionally for react

32 lines 1.25 kB
import React from 'react'; export var If = function If(props) { if (!props.children) { return undefined; } if (props.validation !== false) { var thenCount = 0, elseCount = 0; React.Children.forEach(props.children, function (item) { var childType = item === null || item === void 0 ? void 0 : item.type; if (childType === If.Then) { thenCount++; } if (childType === If.Else) { elseCount++; } }); if (thenCount > 1 || elseCount > 1) { throw SyntaxError("Each statement of 'If.Then/If.Else' for [If condition='".concat(props.condition, "'] must be a single one!")); } } return React.Children.map(props.children, function (item) { var isElse = (item === null || item === void 0 ? void 0 : item.type) === If.Else; return props.condition && !isElse || !props.condition && isElse ? item : undefined; }); }; If.Then = function (props) { return props !== null && props !== void 0 && props.render ? props.render() : props === null || props === void 0 ? void 0 : props.children; }; If.Else = function (props) { return props !== null && props !== void 0 && props.render ? props.render() : props === null || props === void 0 ? void 0 : props.children; };