UNPKG

2.78 kBJavaScriptView Raw
1"use strict";
2
3var _typeof = require("@babel/runtime/helpers/typeof");
4Object.defineProperty(exports, "__esModule", {
5 value: true
6});
7exports.default = void 0;
8var React = _interopRequireWildcard(require("react"));
9var _legacyUtil = require("../utils/legacyUtil");
10function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
11function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
12/**
13 * Parse `children` to `options` if `options` is not provided.
14 * Then flatten the `options`.
15 */
16var useOptions = function useOptions(options, children, fieldNames, optionFilterProp, optionLabelProp) {
17 return React.useMemo(function () {
18 var mergedOptions = options;
19 var childrenAsData = !options;
20 if (childrenAsData) {
21 mergedOptions = (0, _legacyUtil.convertChildrenToData)(children);
22 }
23 var valueOptions = new Map();
24 var labelOptions = new Map();
25 var setLabelOptions = function setLabelOptions(labelOptionsMap, option, key) {
26 if (key && typeof key === 'string') {
27 labelOptionsMap.set(option[key], option);
28 }
29 };
30 var dig = function dig(optionList) {
31 var isChildren = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
32 // for loop to speed up collection speed
33 for (var i = 0; i < optionList.length; i += 1) {
34 var option = optionList[i];
35 if (!option[fieldNames.options] || isChildren) {
36 valueOptions.set(option[fieldNames.value], option);
37 setLabelOptions(labelOptions, option, fieldNames.label);
38 // https://github.com/ant-design/ant-design/issues/35304
39 setLabelOptions(labelOptions, option, optionFilterProp);
40 setLabelOptions(labelOptions, option, optionLabelProp);
41 } else {
42 dig(option[fieldNames.options], true);
43 }
44 }
45 };
46 dig(mergedOptions);
47 return {
48 options: mergedOptions,
49 valueOptions: valueOptions,
50 labelOptions: labelOptions
51 };
52 }, [options, children, fieldNames, optionFilterProp, optionLabelProp]);
53};
54var _default = exports.default = useOptions;
\No newline at end of file