45 lines
1.7 KiB
JavaScript
45 lines
1.7 KiB
JavaScript
import * as React from 'react';
|
|
import { convertChildrenToData } from "../utils/legacyUtil";
|
|
|
|
/**
|
|
* Parse `children` to `options` if `options` is not provided.
|
|
* Then flatten the `options`.
|
|
*/
|
|
export default function useOptions(options, children, fieldNames, optionFilterProp, optionLabelProp) {
|
|
return React.useMemo(function () {
|
|
var mergedOptions = options;
|
|
var childrenAsData = !options;
|
|
if (childrenAsData) {
|
|
mergedOptions = convertChildrenToData(children);
|
|
}
|
|
var valueOptions = new Map();
|
|
var labelOptions = new Map();
|
|
var setLabelOptions = function setLabelOptions(labelOptionsMap, option, key) {
|
|
if (key && typeof key === 'string') {
|
|
labelOptionsMap.set(option[key], option);
|
|
}
|
|
};
|
|
function dig(optionList) {
|
|
var isChildren = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
// for loop to speed up collection speed
|
|
for (var i = 0; i < optionList.length; i += 1) {
|
|
var option = optionList[i];
|
|
if (!option[fieldNames.options] || isChildren) {
|
|
valueOptions.set(option[fieldNames.value], option);
|
|
setLabelOptions(labelOptions, option, fieldNames.label);
|
|
// https://github.com/ant-design/ant-design/issues/35304
|
|
setLabelOptions(labelOptions, option, optionFilterProp);
|
|
setLabelOptions(labelOptions, option, optionLabelProp);
|
|
} else {
|
|
dig(option[fieldNames.options], true);
|
|
}
|
|
}
|
|
}
|
|
dig(mergedOptions);
|
|
return {
|
|
options: mergedOptions,
|
|
valueOptions: valueOptions,
|
|
labelOptions: labelOptions
|
|
};
|
|
}, [options, children, fieldNames, optionFilterProp, optionLabelProp]);
|
|
} |