UNPKG

@vnmfify/core

Version:

```shell npm i @vnmfify/core -S ```

66 lines (50 loc) 3.73 kB
import _isPlainObject from "lodash/isPlainObject"; var _excluded = ["value", "label", "children"]; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } import { isValidElement, useMemo } from "react"; import Children from "../utils/children"; import { isTextElement } from "../utils/validate"; function elementToObject(element, index, depth, maxDepth) { if ( /*#__PURE__*/isValidElement(element)) { var _ref; var { props } = element; var { value, label, children } = props, restProps = _objectWithoutProperties(props, _excluded); var textChildren = isTextElement(children) ? children : undefined; return _objectSpread({ index, value: (_ref = value !== null && value !== void 0 ? value : label) !== null && _ref !== void 0 ? _ref : textChildren, label: label !== null && label !== void 0 ? label : textChildren, children: isTextElement(children) || depth === maxDepth ? children : mapToChildrenOptions(children, depth + 1, maxDepth) }, restProps); } if (_isPlainObject(element)) { return element; } } function mapToOption(nodeOrObject, index, depth, maxDepth) { return elementToObject(nodeOrObject, index, depth, maxDepth); } export function mapToChildrenOptions(children, depth, maxDepth) { return Children.map(children, (child, index) => mapToOption(child, index, depth, maxDepth)); } function usePickerOptions() { var children = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined; var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var { depth = 1 } = options; return useMemo(() => mapToChildrenOptions(children, 0, depth), [depth, children]); } export default usePickerOptions; //# sourceMappingURL=use-picker-options.js.map