(
options: OptionType[],
{ fieldNames, childrenAsData }: { fieldNames?: FieldNames; childrenAsData?: boolean } = {},
)
| 42 | * Here is simply set `key` to the index if not provided. |
| 43 | */ |
| 44 | export function flattenOptions<OptionType extends BaseOptionType = DefaultOptionType>( |
| 45 | options: OptionType[], |
| 46 | { fieldNames, childrenAsData }: { fieldNames?: FieldNames; childrenAsData?: boolean } = {}, |
| 47 | ): FlattenOptionData<OptionType>[] { |
| 48 | const flattenList: FlattenOptionData<OptionType>[] = []; |
| 49 | |
| 50 | const { |
| 51 | label: fieldLabel, |
| 52 | value: fieldValue, |
| 53 | options: fieldOptions, |
| 54 | groupLabel, |
| 55 | } = fillFieldNames(fieldNames, false); |
| 56 | |
| 57 | function dig(list: OptionType[], isGroupOption: boolean) { |
| 58 | if (!Array.isArray(list)) { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | list.forEach((data) => { |
| 63 | if (isGroupOption || !(fieldOptions in data)) { |
| 64 | const value = data[fieldValue]; |
| 65 | |
| 66 | // Option |
| 67 | flattenList.push({ |
| 68 | key: getKey(data, flattenList.length), |
| 69 | groupOption: isGroupOption, |
| 70 | data, |
| 71 | label: data[fieldLabel], |
| 72 | value, |
| 73 | }); |
| 74 | } else { |
| 75 | let grpLabel = data[groupLabel]; |
| 76 | if (grpLabel === undefined && childrenAsData) { |
| 77 | grpLabel = data.label; |
| 78 | } |
| 79 | |
| 80 | // Option Group |
| 81 | flattenList.push({ |
| 82 | key: getKey(data, flattenList.length), |
| 83 | group: true, |
| 84 | data, |
| 85 | label: grpLabel, |
| 86 | }); |
| 87 | |
| 88 | dig(data[fieldOptions], true); |
| 89 | } |
| 90 | }); |
| 91 | } |
| 92 | |
| 93 | dig(options, false); |
| 94 | |
| 95 | return flattenList; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Inject `props` into `option` for legacy usage |
searching dependent graphs…