fix(数据表): 树数据表-级联修复,修复级联选择编辑无显示 (#1583)

Reviewed-on: daoyoucloud/tachybase#1583
Co-authored-by: wjh <wwwjh0710@163.com>
Co-committed-by: wjh <wwwjh0710@163.com>
This commit is contained in:
wjh 2024-10-09 15:57:06 +08:00 committed by bai.zixv
parent 4cda546880
commit 725e31871c
2 changed files with 39 additions and 25 deletions

View File

@ -45,6 +45,7 @@ const CascadeSelect = connect((props) => {
const resource = api.resource(collectionField.target); const resource = api.resource(collectionField.target);
const { getCollectionJoinField, getInterface } = useCollectionManager_deprecated(); const { getCollectionJoinField, getInterface } = useCollectionManager_deprecated();
const fieldNames = associationField?.componentProps?.fieldNames; const fieldNames = associationField?.componentProps?.fieldNames;
const [change, setChange] = useState(false);
const targetField = const targetField =
collectionField?.target && collectionField?.target &&
fieldNames?.label && fieldNames?.label &&
@ -57,7 +58,7 @@ const CascadeSelect = connect((props) => {
}, [targetField]); }, [targetField]);
const field: any = useField(); const field: any = useField();
useEffect(() => { useEffect(() => {
if (value) { if (value && !change) {
const values = Array.isArray(value) const values = Array.isArray(value)
? extractLastNonNullValueObjects( ? extractLastNonNullValueObjects(
value?.filter((v) => v.value), value?.filter((v) => v.value),
@ -73,7 +74,7 @@ const CascadeSelect = connect((props) => {
}); });
setSelectedOptions(options); setSelectedOptions(options);
} }
}, []); }, [value]);
const mapOptionsToTags = useCallback( const mapOptionsToTags = useCallback(
(options) => { (options) => {
try { try {
@ -140,6 +141,7 @@ const CascadeSelect = connect((props) => {
}; };
const handleSelect = async (value, option, index) => { const handleSelect = async (value, option, index) => {
setChange(true);
const data = await handleGetOptions({ parentId: option?.id }); const data = await handleGetOptions({ parentId: option?.id });
const options = [...selectedOptions]; const options = [...selectedOptions];
options.splice(index + 1); options.splice(index + 1);
@ -272,6 +274,13 @@ export const InternalCascadeSelect = observer(
return [{}]; return [{}];
}; };
const defaultValue = toValue(); const defaultValue = toValue();
useEffect(() => {
const isChange = selectForm.values?.select_array?.filter((value) => value && Object.keys(value).length);
if (collectionField.interface !== 'm2o' && props.value && !isChange.length) {
const value = extractLastNonNullValueObjects(defaultValue).filter((v) => v && Object.keys(v).length > 0);
selectForm.setValues({ select_array: value });
}
}, [props.value]);
const schema = { const schema = {
type: 'object', type: 'object',
properties: { properties: {
@ -315,7 +324,6 @@ export const InternalCascadeSelect = observer(
}, },
}; };
return ( return (
props.value !== null && (
<FormProvider form={selectForm}> <FormProvider form={selectForm}>
{collectionField.interface === 'm2o' ? ( {collectionField.interface === 'm2o' ? (
<SchemaComponent <SchemaComponent
@ -334,7 +342,6 @@ export const InternalCascadeSelect = observer(
<SchemaField schema={schema} /> <SchemaField schema={schema} />
)} )}
</FormProvider> </FormProvider>
)
); );
}, },
{ displayName: 'InternalCascadeSelect' }, { displayName: 'InternalCascadeSelect' },

View File

@ -56,12 +56,12 @@ const Cascade = connect((props) => {
const changOnSelect = fieldSchema['x-component-props']?.changOnSelect || false; const changOnSelect = fieldSchema['x-component-props']?.changOnSelect || false;
const field: any = useField(); const field: any = useField();
const [selectedOptions, setSelectedOptions] = useState([]); const [selectedOptions, setSelectedOptions] = useState([]);
const [chang, setChang] = useState(false); const [change, setChange] = useState(false);
const [dataList, setDataList] = useState({}); const [dataList, setDataList] = useState({});
const { styles } = useStyles(); const { styles } = useStyles();
useEffect(() => { useEffect(() => {
const propsValue = props.value || fieldSchema['x-component-props'].value; const propsValue = props.value || fieldSchema['x-component-props'].value;
if (!chang && propsValue) { if (!change && propsValue) {
if (!propsValue.parent && !Object.keys(dataList).length) { if (!propsValue.parent && !Object.keys(dataList).length) {
resource resource
.list({ .list({
@ -116,7 +116,7 @@ const Cascade = connect((props) => {
}; };
const handleSelect = async (option) => { const handleSelect = async (option) => {
setChang(true); setChange(true);
if (option) { if (option) {
if (['o2m', 'm2m'].includes(collectionField.interface)) { if (['o2m', 'm2m'].includes(collectionField.interface)) {
const fieldValue = Array.isArray(associationField.fieldValue) ? associationField.fieldValue : []; const fieldValue = Array.isArray(associationField.fieldValue) ? associationField.fieldValue : [];
@ -243,6 +243,13 @@ export const InternalCascader = observer(
return [{}]; return [{}];
}; };
const defaultValue = toValue(); const defaultValue = toValue();
useEffect(() => {
const isChange = selectForm.values?.select_array?.filter((value) => value && Object.keys(value).length);
if (collectionField.interface !== 'm2o' && props.value && !isChange.length) {
selectForm.setInitialValues({ select_array: defaultValue });
}
}, [props.value]);
const schema = { const schema = {
type: 'object', type: 'object',
properties: { properties: {