@wordpress/data
Version:
Data module for WordPress.
8 lines (7 loc) • 2.46 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../../src/components/with-select/index.js"],
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { createHigherOrderComponent, pure } from '@wordpress/compose';\n\n/**\n * Internal dependencies\n */\nimport useSelect from '../use-select';\n\n/** @typedef {import('react').ComponentType} ComponentType */\n\n/**\n * Higher-order component used to inject state-derived props using registered\n * selectors.\n *\n * @param {Function} mapSelectToProps Function called on every state change,\n * expected to return object of props to\n * merge with the component's own props.\n *\n * @example\n * ```js\n * import { withSelect } from '@wordpress/data';\n * import { store as myCustomStore } from 'my-custom-store';\n *\n * function PriceDisplay( { price, currency } ) {\n * \treturn new Intl.NumberFormat( 'en-US', {\n * \t\tstyle: 'currency',\n * \t\tcurrency,\n * \t} ).format( price );\n * }\n *\n * const HammerPriceDisplay = withSelect( ( select, ownProps ) => {\n * \tconst { getPrice } = select( myCustomStore );\n * \tconst { currency } = ownProps;\n *\n * \treturn {\n * \t\tprice: getPrice( 'hammer', currency ),\n * \t};\n * } )( PriceDisplay );\n *\n * // Rendered in the application:\n * //\n * // <HammerPriceDisplay currency=\"USD\" />\n * ```\n * In the above example, when `HammerPriceDisplay` is rendered into an\n * application, it will pass the price into the underlying `PriceDisplay`\n * component and update automatically if the price of a hammer ever changes in\n * the store.\n *\n * @return {ComponentType} Enhanced component with merged state data props.\n */\nconst withSelect = ( mapSelectToProps ) =>\n\tcreateHigherOrderComponent(\n\t\t( WrappedComponent ) =>\n\t\t\tpure( ( ownProps ) => {\n\t\t\t\tconst mapSelect = ( select, registry ) =>\n\t\t\t\t\tmapSelectToProps( select, ownProps, registry );\n\t\t\t\tconst mergeProps = useSelect( mapSelect );\n\t\t\t\treturn <WrappedComponent { ...ownProps } { ...mergeProps } />;\n\t\t\t} ),\n\t\t'withSelect'\n\t);\n\nexport default withSelect;\n"],
"mappings": ";AAGA,SAAS,4BAA4B,YAAY;AAKjD,OAAO,eAAe;AAmDX;AAPX,IAAM,aAAa,CAAE,qBACpB;AAAA,EACC,CAAE,qBACD,KAAM,CAAE,aAAc;AACrB,UAAM,YAAY,CAAE,QAAQ,aAC3B,iBAAkB,QAAQ,UAAU,QAAS;AAC9C,UAAM,aAAa,UAAW,SAAU;AACxC,WAAO,oBAAC,oBAAmB,GAAG,UAAa,GAAG,YAAa;AAAA,EAC5D,CAAE;AAAA,EACH;AACD;AAED,IAAO,sBAAQ;",
"names": []
}