Skip to content

Commit 54544a3

Browse files
committed
feat: flatten support additional props to support treeSelect
1 parent 8c27b40 commit 54544a3

File tree

2 files changed

+28
-28
lines changed

2 files changed

+28
-28
lines changed

src/generate.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ export interface GenerateConfig<OptionsType extends object[]> {
141141
/** Convert jsx tree into `OptionsType` */
142142
convertChildrenToData: (children: React.ReactNode) => OptionsType;
143143
/** Flatten nest options into raw option list */
144-
flattenOptions: (options: OptionsType) => FlattenOptionsType<OptionsType>;
144+
flattenOptions: (options: OptionsType, props: any) => FlattenOptionsType<OptionsType>;
145145
/** Convert single raw value into { label, value } format. Will be called by each value */
146146
getLabeledValue: GetLabeledValue<FlattenOptionsType<OptionsType>>;
147147
filterOptions: FilterOptions<OptionsType>;
@@ -357,7 +357,7 @@ export default function generateSelector<
357357
}, [options, children, mode, baseValue]);
358358

359359
const mergedFlattenOptions: FlattenOptionsType<OptionsType> = React.useMemo(
360-
() => flattenOptions(mergedOptions),
360+
() => flattenOptions(mergedOptions, props),
361361
[mergedOptions],
362362
);
363363

@@ -382,7 +382,7 @@ export default function generateSelector<
382382
}, [mergedOptions, mergedSearchValue, mode]);
383383

384384
const displayFlattenOptions: FlattenOptionsType<OptionsType> = React.useMemo(
385-
() => flattenOptions(displayOptions),
385+
() => flattenOptions(displayOptions, props),
386386
[displayOptions],
387387
);
388388

src/utils/valueUtil.ts

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,32 +37,32 @@ function getKey(data: OptionData | OptionGroupData, index: number) {
3737
* We use `optionOnly` here is aim to avoid user use nested option group.
3838
* Here is simply set `key` to the index if not provided.
3939
*/
40-
export function flattenOptions(
41-
options: SelectOptionsType,
42-
rootFlattenList?: FlattenOptionData[],
43-
): FlattenOptionData[] {
44-
const flattenList: FlattenOptionData[] = rootFlattenList || [];
45-
const isGroupOption = !!rootFlattenList;
46-
47-
options.forEach(data => {
48-
if (isGroupOption || !('options' in data)) {
49-
// Option
50-
flattenList.push({
51-
key: getKey(data, flattenList.length),
52-
groupOption: isGroupOption,
53-
data,
54-
});
55-
} else {
56-
// Option Group
57-
flattenList.push({
58-
key: getKey(data, flattenList.length),
59-
group: true,
60-
data,
61-
});
40+
export function flattenOptions(options: SelectOptionsType): FlattenOptionData[] {
41+
const flattenList: FlattenOptionData[] = [];
42+
43+
function dig(list: SelectOptionsType, isGroupOption: boolean) {
44+
list.forEach(data => {
45+
if (isGroupOption || !('options' in data)) {
46+
// Option
47+
flattenList.push({
48+
key: getKey(data, flattenList.length),
49+
groupOption: isGroupOption,
50+
data,
51+
});
52+
} else {
53+
// Option Group
54+
flattenList.push({
55+
key: getKey(data, flattenList.length),
56+
group: true,
57+
data,
58+
});
59+
60+
dig(data.options, true);
61+
}
62+
});
63+
}
6264

63-
flattenOptions(data.options, flattenList);
64-
}
65-
});
65+
dig(options, false);
6666

6767
return flattenList;
6868
}

0 commit comments

Comments
 (0)