UNPKG

@wordpress/components

Version:
83 lines (82 loc) • 2.3 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); let _wordpress_element = require("@wordpress/element"); let _wordpress_html_entities = require("@wordpress/html-entities"); let ___select_control_index_tsx = require("../select-control/index.cjs"); let react_jsx_runtime = require("react/jsx-runtime"); // packages/components/src/tree-select/index.tsx function getSelectOptions(tree, level = 0) { return tree.flatMap((treeNode) => [{ value: treeNode.id, label: "\xA0".repeat(level * 3) + (0, _wordpress_html_entities.decodeEntities)(treeNode.name) }, ...getSelectOptions(treeNode.children || [], level + 1)]); } /** * Generates a hierarchical select input. * * ```jsx * import { useState } from 'react'; * import { TreeSelect } from '@wordpress/components'; * * const MyTreeSelect = () => { * const [ page, setPage ] = useState( 'p21' ); * * return ( * <TreeSelect * label="Parent page" * noOptionLabel="No parent page" * onChange={ ( newPage ) => setPage( newPage ) } * selectedId={ page } * tree={ [ * { * name: 'Page 1', * id: 'p1', * children: [ * { name: 'Descend 1 of page 1', id: 'p11' }, * { name: 'Descend 2 of page 1', id: 'p12' }, * ], * }, * { * name: 'Page 2', * id: 'p2', * children: [ * { * name: 'Descend 1 of page 2', * id: 'p21', * children: [ * { * name: 'Descend 1 of Descend 1 of page 2', * id: 'p211', * }, * ], * }, * ], * }, * ] } * /> * ); * } * ``` */ function TreeSelect(props) { const { __nextHasNoMarginBottom: _, __next40pxDefaultSize: _next40pxDefaultSize, label, noOptionLabel, onChange, selectedId, tree = [], ...restProps } = props; const options = (0, _wordpress_element.useMemo)(() => { return [noOptionLabel && { value: "", label: noOptionLabel }, ...getSelectOptions(tree)].filter((option) => !!option); }, [noOptionLabel, tree]); return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___select_control_index_tsx.SelectControl, { label, options, onChange, value: selectedId, ...restProps }); } exports.TreeSelect = TreeSelect; exports.default = TreeSelect; //# sourceMappingURL=index.cjs.map