MCPcopy Create free account
hub / github.com/react-component/select / flattenOptions

Function flattenOptions

src/utils/valueUtil.ts:44–96  ·  view source on GitHub ↗
(
  options: OptionType[],
  { fieldNames, childrenAsData }: { fieldNames?: FieldNames; childrenAsData?: boolean } = {},
)

Source from the content-addressed store, hash-verified

42 * Here is simply set `key` to the index if not provided.
43 */
44export 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

Callers 3

Select.tsxFile · 0.90
utils.test.jsxFile · 0.90
generateListFunction · 0.90

Calls 2

fillFieldNamesFunction · 0.85
digFunction · 0.70

Tested by 1

generateListFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…