UNPKG

@wordpress/compose

Version:
44 lines (41 loc) 1.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _createHigherOrderComponent = require("../../utils/create-higher-order-component"); var _jsxRuntime = require("react/jsx-runtime"); /** * External dependencies */ /** * Internal dependencies */ /** * Higher-order component creator, creating a new component which renders if * the given condition is satisfied or with the given optional prop name. * * @example * ```ts * type Props = { foo: string }; * const Component = ( props: Props ) => <div>{ props.foo }</div>; * const ConditionalComponent = ifCondition( ( props: Props ) => props.foo.length !== 0 )( Component ); * <ConditionalComponent foo="" />; // => null * <ConditionalComponent foo="bar" />; // => <div>bar</div>; * ``` * * @param predicate Function to test condition. * * @return Higher-order component. */function ifCondition(predicate) { return (0, _createHigherOrderComponent.createHigherOrderComponent)(WrappedComponent => props => { if (!predicate(props)) { return null; } return /*#__PURE__*/(0, _jsxRuntime.jsx)(WrappedComponent, { ...props }); }, 'ifCondition'); } var _default = exports.default = ifCondition; //# sourceMappingURL=index.js.map