UNPKG

@ariakit/react-core

Version:

Ariakit React core

108 lines (88 loc) 3.38 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); var _4R2KFV2Fcjs = require('../__chunks/4R2KFV2F.cjs'); var _6UDM3WWOcjs = require('../__chunks/6UDM3WWO.cjs'); var _YLCVWFRAcjs = require('../__chunks/YLCVWFRA.cjs'); require('../__chunks/ZVJRPAXY.cjs'); require('../__chunks/YDPERDKF.cjs'); require('../__chunks/KVXNVDJK.cjs'); require('../__chunks/D6FV6EYS.cjs'); var _25BPIGZHcjs = require('../__chunks/25BPIGZH.cjs'); require('../__chunks/6HKL3JR2.cjs'); var _WULEED4Qcjs = require('../__chunks/WULEED4Q.cjs'); require('../__chunks/OZM4QA2V.cjs'); require('../__chunks/FDRJDQ5Y.cjs'); var _7EQBAZ46cjs = require('../__chunks/7EQBAZ46.cjs'); // src/select/select-renderer.tsx var _array = require('@ariakit/core/utils/array'); var _react = require('react'); var TagName = "div"; function getItemObject(item) { if (!item || typeof item !== "object") { return { value: `${item}` }; } return item; } function findIndicesByValue(items, value) { var _a; const values = _array.toArray.call(void 0, value); const indices = []; for (const [index, item] of items.entries()) { if (indices.length === values.length) break; const object = getItemObject(item); if (object.value != null && values.includes(object.value)) { indices.push(index); } else if ((_a = object.items) == null ? void 0 : _a.length) { const childIndices = findIndicesByValue(object.items, value); if (childIndices.length) { indices.push(index); } } } return indices; } function useSelectRenderer(_a) { var _b = _a, { store, orientation: orientationProp, persistentIndices: persistentIndicesProp, items: itemsProp, value: valueProp } = _b, props = _7EQBAZ46cjs.__objRest.call(void 0, _b, [ "store", "orientation", "persistentIndices", "items", "value" ]); const context = _4R2KFV2Fcjs.useSelectContext.call(void 0, ); store = store || context; const items = _25BPIGZHcjs.useStoreState.call(void 0, store, (state) => { if (!state) return itemsProp; if (!state.mounted) return 0; return itemsProp != null ? itemsProp : state.items; }); const value = _25BPIGZHcjs.useStoreState.call(void 0, store, (state) => valueProp != null ? valueProp : state == null ? void 0 : state.value); const valueIndices = _react.useMemo.call(void 0, () => { if (!items) return []; if (value == null) return []; if (typeof items === "number") return []; if (!items.length) return []; return findIndicesByValue(items, value); }, [items, value]); const persistentIndices = _react.useMemo.call(void 0, () => { if (persistentIndicesProp) { return [...persistentIndicesProp, ...valueIndices]; } return valueIndices; }, [valueIndices, persistentIndicesProp]); return _6UDM3WWOcjs.useCompositeRenderer.call(void 0, _7EQBAZ46cjs.__spreadValues.call(void 0, { store, items, persistentIndices }, props)); } var SelectRenderer = _WULEED4Qcjs.forwardRef.call(void 0, function SelectRenderer2(props) { const htmlProps = useSelectRenderer(props); return _WULEED4Qcjs.createElement.call(void 0, TagName, htmlProps); }); exports.SelectRenderer = SelectRenderer; exports.getSelectRendererItem = _YLCVWFRAcjs.getCollectionRendererItem; exports.getSelectRendererItemId = _YLCVWFRAcjs.getCollectionRendererItemId;