Skip to content

Commit cc326e9

Browse files
authored
fix(form):Bug when ProField uses fieldNames (#9158)
1 parent 851826d commit cc326e9

File tree

1 file changed

+33
-20
lines changed
  • packages/field/src/components/Select

1 file changed

+33
-20
lines changed

packages/field/src/components/Select/index.tsx

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ export const useFieldFetchData = (
271271

272272
if (children) traverseFieldKey(data, 'children');
273273
if (label) traverseFieldKey(data, 'label');
274-
if (value) traverseFieldKey(data, 'value');
274+
if (value) traverseFieldKey(data, 'value');
275275
return data;
276276
}, [fieldProps]);
277277

@@ -372,10 +372,23 @@ export const useFieldFetchData = (
372372

373373
return opt;
374374
}, [options, keyWords, props.fieldProps?.filterOption]);
375+
const applyFieldNamesMapping = (item: any) => {
376+
if (!fieldProps?.fieldNames) return item;
375377

378+
const { label: labelKey = 'label', value: valueKey = 'value' } = fieldProps.fieldNames;
379+
380+
return {
381+
...item,
382+
label: item[labelKey],
383+
value: item[valueKey],
384+
};
385+
};
386+
const finalData = props.request
387+
? (data as SelectOptionType)?.map((item) => applyFieldNamesMapping(item))
388+
: undefined;
376389
return [
377390
isValidating,
378-
props.request ? (data as SelectOptionType) : resOptions,
391+
finalData || resOptions,
379392
(fetchKeyWords?: string) => {
380393
setKeyWords(fetchKeyWords);
381394
},
@@ -417,8 +430,6 @@ const FieldSelect: ProFieldFC<
417430
const inputRef = useRef();
418431
const intl = useIntl();
419432
const keyWordsRef = useRef<string>('');
420-
const { fieldNames } = fieldProps;
421-
422433
useEffect(() => {
423434
keyWordsRef.current = fieldProps?.searchValue;
424435
}, [fieldProps?.searchValue]);
@@ -439,30 +450,32 @@ const FieldSelect: ProFieldFC<
439450
const optionsValueEnum = useMemo(() => {
440451
if (mode !== 'read') return;
441452

442-
const {
443-
label: labelPropsName = 'label',
444-
value: valuePropsName = 'value',
445-
options: optionsPropsName = 'options',
446-
} = fieldNames || {};
447-
448-
const valuesMap = new Map();
449-
450453
const traverseOptions = (_options: typeof options) => {
454+
const localMap = new Map();
455+
451456
if (!_options?.length) {
452-
return valuesMap;
457+
return localMap;
453458
}
459+
454460
const length = _options.length;
455-
let i = 0;
456-
while (i < length) {
457-
const cur = _options[i++];
458-
valuesMap.set(cur[valuePropsName], cur[labelPropsName]);
459-
traverseOptions(cur[optionsPropsName]);
461+
for (let i = 0; i < length; i++) {
462+
const cur = _options[i];
463+
464+
if (cur.value !== undefined && cur.label !== undefined) {
465+
localMap.set(cur.value, cur.label);
466+
}
467+
468+
if (cur.options?.length) {
469+
const childMap = traverseOptions(cur.options);
470+
childMap.forEach((v, k) => localMap.set(k, v));
471+
}
460472
}
461-
return valuesMap;
473+
474+
return localMap;
462475
};
463476

464477
return traverseOptions(options);
465-
}, [fieldNames, mode, options]);
478+
}, [mode, options]);
466479

467480
if (mode === 'read') {
468481
const dom = (

0 commit comments

Comments
 (0)