diff --git a/src/hooks/useOptions.ts b/src/hooks/useOptions.ts index f3ccb67d..ed4d4c13 100644 --- a/src/hooks/useOptions.ts +++ b/src/hooks/useOptions.ts @@ -6,13 +6,13 @@ import { convertChildrenToData } from '../utils/legacyUtil'; * Parse `children` to `options` if `options` is not provided. * Then flatten the `options`. */ -export default function useOptions( +const useOptions = ( options: OptionType[], children: React.ReactNode, fieldNames: FieldNames, optionFilterProp: string, optionLabelProp: string, -) { +) => { return React.useMemo(() => { let mergedOptions = options; const childrenAsData = !options; @@ -24,13 +24,17 @@ export default function useOptions( const valueOptions = new Map(); const labelOptions = new Map(); - const setLabelOptions = (labelOptionsMap, option, key) => { + const setLabelOptions = ( + labelOptionsMap: Map, + option: OptionType, + key: string | number, + ) => { if (key && typeof key === 'string') { labelOptionsMap.set(option[key], option); } }; - function dig(optionList: OptionType[], isChildren = false) { + const dig = (optionList: OptionType[], isChildren = false) => { // for loop to speed up collection speed for (let i = 0; i < optionList.length; i += 1) { const option = optionList[i]; @@ -44,7 +48,8 @@ export default function useOptions( dig(option[fieldNames.options], true); } } - } + }; + dig(mergedOptions); return { @@ -53,4 +58,6 @@ export default function useOptions( labelOptions, }; }, [options, children, fieldNames, optionFilterProp, optionLabelProp]); -} +}; + +export default useOptions;