feat: option readPretty optimization (#1138)
* feat: option readPretty optimization * feat: option readPretty optimization * feat: getCurrentOptions fix
This commit is contained in:
parent
024cc102ea
commit
9fc634ecf2
@ -7,22 +7,31 @@ export const defaultFieldNames = {
|
|||||||
options: 'children',
|
options: 'children',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export const getCurrentOptions = (values, dataSource, fieldNames) => {
|
export const getCurrentOptions = (values, dataSource, fieldNames) => {
|
||||||
|
function flatData(data) {
|
||||||
|
let newArr = [];
|
||||||
|
for (let i = 0; i < data.length; i++) {
|
||||||
|
const children = data[i][fieldNames.options];
|
||||||
|
if (Array.isArray(children)) {
|
||||||
|
newArr.push(...flatData(children))
|
||||||
|
}
|
||||||
|
newArr.push({ ...data[i] });
|
||||||
|
}
|
||||||
|
return newArr;
|
||||||
|
}
|
||||||
|
const result = flatData(dataSource);
|
||||||
values = castArray(values)
|
values = castArray(values)
|
||||||
.filter(item => item != null)
|
.filter((item) => item != null)
|
||||||
.map((val) => (typeof val === 'object' ? val[fieldNames.value] : val));
|
.map((val) => (typeof val === 'object' ? val[fieldNames.value] : val));
|
||||||
const findOptions = (options: any[]) => {
|
const findOptions = (options: any[]) => {
|
||||||
let current = [];
|
let current = [];
|
||||||
for (const option of options) {
|
for (const value of values) {
|
||||||
if (values.includes(option[fieldNames.value])) {
|
const option = options.find((v) => v[fieldNames.value] === value) || { value: value, label: value };
|
||||||
current.push(option);
|
current.push(option);
|
||||||
}
|
}
|
||||||
const children = option[fieldNames.options];
|
|
||||||
if (Array.isArray(children)) {
|
|
||||||
current.push(...findOptions(children));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return current;
|
return current;
|
||||||
};
|
};
|
||||||
return findOptions(dataSource);
|
return findOptions(result);
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user