UNPKG

@wordpress/compose

Version:
8 lines (7 loc) 1.58 kB
{ "version": 3, "sources": ["../../../src/higher-order/if-condition/index.tsx"], "sourcesContent": ["/**\n * External dependencies\n */\nimport type { ComponentType } from 'react';\n\n/**\n * Internal dependencies\n */\nimport { createHigherOrderComponent } from '../../utils/create-higher-order-component';\n\n/**\n * Higher-order component creator, creating a new component which renders if\n * the given condition is satisfied or with the given optional prop name.\n *\n * @example\n * ```ts\n * type Props = { foo: string };\n * const Component = ( props: Props ) => <div>{ props.foo }</div>;\n * const ConditionalComponent = ifCondition( ( props: Props ) => props.foo.length !== 0 )( Component );\n * <ConditionalComponent foo=\"\" />; // => null\n * <ConditionalComponent foo=\"bar\" />; // => <div>bar</div>;\n * ```\n *\n * @param predicate Function to test condition.\n *\n * @return Higher-order component.\n */\nfunction ifCondition< Props extends {} >(\n\tpredicate: ( props: Props ) => boolean\n) {\n\treturn createHigherOrderComponent(\n\t\t( WrappedComponent: ComponentType< Props > ) => ( props: Props ) => {\n\t\t\tif ( ! predicate( props ) ) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\treturn <WrappedComponent { ...props } />;\n\t\t},\n\t\t'ifCondition'\n\t);\n}\n\nexport default ifCondition;\n"], "mappings": ";AAQA,SAAS,kCAAkC;AA4BjC;AATV,SAAS,YACR,WACC;AACD,SAAO;AAAA,IACN,CAAE,qBAA8C,CAAE,UAAkB;AACnE,UAAK,CAAE,UAAW,KAAM,GAAI;AAC3B,eAAO;AAAA,MACR;AAEA,aAAO,oBAAC,oBAAmB,GAAG,OAAQ;AAAA,IACvC;AAAA,IACA;AAAA,EACD;AACD;AAEA,IAAO,uBAAQ;", "names": [] }