mirror of
https://github.com/nocobase/nocobase
synced 2024-11-15 18:26:41 +00:00
refactor: select options
This commit is contained in:
parent
59edb252f8
commit
9f7faf1366
@ -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(() => {
|
||||||
|
@ -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) {
|
||||||
|
Loading…
Reference in New Issue
Block a user