fix(cascadeSelect): cassadeSelect does not dislay data in edit form (#3649)

This commit is contained in:
katherinehhh 2024-03-07 19:22:34 +08:00 committed by GitHub
parent 263ee28c72
commit 16581a547c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -22,7 +22,7 @@ const SchemaField = createSchemaField({
}); });
const CascadeSelect = connect((props) => { const CascadeSelect = connect((props) => {
const { data, mapOptions, onChange } = props; const { data, mapOptions, onChange, value } = props;
const [selectedOptions, setSelectedOptions] = useState<{ key: string; children: any; value?: any }[]>([ const [selectedOptions, setSelectedOptions] = useState<{ key: string; children: any; value?: any }[]>([
{ key: undefined, children: [], value: null }, { key: undefined, children: [], value: null },
]); ]);
@ -47,10 +47,10 @@ const CascadeSelect = connect((props) => {
}, [targetField]); }, [targetField]);
const field: any = useField(); const field: any = useField();
useEffect(() => { useEffect(() => {
if (props.value) { if (value) {
const values = Array.isArray(props.value) const values = Array.isArray(value)
? extractLastNonNullValueObjects(props.value?.filter((v) => v.value), true) ? extractLastNonNullValueObjects(value?.filter((v) => v.value), true)
: transformNestedData(props.value); : transformNestedData(value);
const options = values?.map?.((v) => { const options = values?.map?.((v) => {
return { return {
key: v.parentId, key: v.parentId,
@ -302,24 +302,26 @@ export const InternalCascadeSelect = observer(
}, },
}; };
return ( return (
<FormProvider form={selectForm}> props.value !== null && (
{collectionField.interface === 'm2o' ? ( <FormProvider form={selectForm}>
<SchemaComponent {collectionField.interface === 'm2o' ? (
components={{ FormItem }} <SchemaComponent
schema={{ components={{ FormItem }}
...fieldSchema, schema={{
default: field.value, ...fieldSchema,
title: '', default: field.value,
'x-component': AssociationCascadeSelect, title: '',
'x-component-props': { 'x-component': AssociationCascadeSelect,
...props, 'x-component-props': {
}, ...props,
}} },
/> }}
) : ( />
<SchemaField schema={schema} /> ) : (
)} <SchemaField schema={schema} />
</FormProvider> )}
</FormProvider>
)
); );
}, },
{ displayName: 'InternalCascadeSelect' }, { displayName: 'InternalCascadeSelect' },