refactor: select options

This commit is contained in:
katherinehhh 2023-05-10 14:25:21 +08:00
parent 59edb252f8
commit 9f7faf1366
2 changed files with 6 additions and 20 deletions

View File

@ -43,11 +43,10 @@ const InternalAssociationSelect = memo((props: AssociationSelectProps) => {
if (props.value === undefined || props.value === null || !Object.keys(props.value).length) { if (props.value === undefined || props.value === null || !Object.keys(props.value).length) {
return; return;
} }
if (Array.isArray(props.value)) { if (Array.isArray(props.value)) {
return props.value.map(normalizeValues); return props.value;
} else { } else {
return normalizeValues(props.value); return props.value;
} }
}, [props.value, normalizeValues]); }, [props.value, normalizeValues]);
useEffect(() => { useEffect(() => {

View File

@ -95,6 +95,7 @@ const InternalRemoteSelect = connect(
}); });
} }
return { return {
...option,
[fieldNames.label]: label || option[fieldNames.value], [fieldNames.label]: label || option[fieldNames.value],
[fieldNames.value]: option[fieldNames.value], [fieldNames.value]: option[fieldNames.value],
}; };
@ -157,12 +158,6 @@ const InternalRemoteSelect = connect(
const getOptionsByFieldNames = useCallback( const getOptionsByFieldNames = useCallback(
(item) => { (item) => {
return Object.keys(fieldNames).reduce((obj, key) => { return Object.keys(fieldNames).reduce((obj, key) => {
const value = item?.[fieldNames[key]];
if (value) {
// support hidden, disabled, etc.
obj[['label', 'value', 'options'].includes(key) ? fieldNames[key] : key] =
key === 'label' ? compile(value) : value;
}
return obj; return obj;
}, {} as any); }, {} as any);
}, },
@ -180,18 +175,10 @@ const InternalRemoteSelect = connect(
const options = useMemo(() => { const options = useMemo(() => {
if (!data?.data?.length) { if (!data?.data?.length) {
return value !== undefined && value !== null return value !== undefined && value !== null ? (Array.isArray(value) ? value : [value]) : [];
? Array.isArray(value)
? value.map(normalizeOptions)
: [normalizeOptions(value)]
: [];
} }
const valueOptions = const valueOptions = (value !== undefined && value !== null && (Array.isArray(value) ? value : [value])) || [];
(value !== undefined && return uniqBy(data?.data?.concat(valueOptions) || [], fieldNames.value);
value !== null &&
(Array.isArray(value) ? value.map(normalizeOptions) : [normalizeOptions(value)])) ||
[];
return uniqBy(data?.data?.map(getOptionsByFieldNames).concat(valueOptions) || [], fieldNames.value);
}, [data?.data, getOptionsByFieldNames, normalizeOptions, value]); }, [data?.data, getOptionsByFieldNames, normalizeOptions, value]);
const onDropdownVisibleChange = () => { const onDropdownVisibleChange = () => {
if (firstRun.current) { if (firstRun.current) {