From e35e4af5e715fde67ed79d1d3ac51422529f6362 Mon Sep 17 00:00:00 2001 From: katherinehhh Date: Fri, 27 Oct 2023 09:45:34 +0800 Subject: [PATCH] refactor: reset form values after create action (#2905) * refactor: reset form after create action * fix: association select support reset * fix: map field support form reset * fix: formula field support form reset * fix: josn field support form reset * fix: attachement field support form reset * fix: map field support form reset * fix: map field support form reset * fix: map field support form reset * fix: console --- packages/core/client/src/block-provider/hooks/index.ts | 5 ++++- .../antd/association-field/AssociationSelect.tsx | 7 +++++-- .../antd/association-field/FileManager.tsx | 2 ++ .../client/src/schema-component/antd/input/Json.tsx | 2 ++ .../client/src/schema-component/antd/upload/Upload.tsx | 6 ++++-- .../src/client/components/Formula/Result.tsx | 6 +++--- .../plugin-map/src/client/components/AMap/Map.tsx | 10 +++++++++- .../src/client/components/GoogleMaps/Map.tsx | 5 +++++ 8 files changed, 34 insertions(+), 9 deletions(-) diff --git a/packages/core/client/src/block-provider/hooks/index.ts b/packages/core/client/src/block-provider/hooks/index.ts index ce246fe35..28c498082 100644 --- a/packages/core/client/src/block-provider/hooks/index.ts +++ b/packages/core/client/src/block-provider/hooks/index.ts @@ -112,7 +112,7 @@ export const useCreateActionProps = () => { const variables = useVariables(); const localVariables = useLocalVariables({ currentForm: form }); const { getActiveFieldsName } = useFormActiveFields() || {}; - + const { t } = useTranslation(); const action = actionField.componentProps.saveMode || 'create'; const filterKeys = actionField.componentProps.filterKeys?.checked || []; return { @@ -187,6 +187,8 @@ export const useCreateActionProps = () => { __parent?.service?.refresh?.(); setVisible?.(false); if (!onSuccess?.successMessage) { + message.success(t('Saved successfully')); + await form.reset(); return; } if (onSuccess?.manualClose) { @@ -205,6 +207,7 @@ export const useCreateActionProps = () => { }); } else { message.success(compile(onSuccess?.successMessage)); + await form.reset(); if (onSuccess?.redirecting && onSuccess?.redirectTo) { if (isURL(onSuccess.redirectTo)) { window.location.href = onSuccess.redirectTo; diff --git a/packages/core/client/src/schema-component/antd/association-field/AssociationSelect.tsx b/packages/core/client/src/schema-component/antd/association-field/AssociationSelect.tsx index d97737223..59acbb697 100644 --- a/packages/core/client/src/schema-component/antd/association-field/AssociationSelect.tsx +++ b/packages/core/client/src/schema-component/antd/association-field/AssociationSelect.tsx @@ -58,6 +58,11 @@ const InternalAssociationSelect = observer((props: AssociationSelectProps) => { const api = useAPIClient(); const resource = api.resource(collectionField.target); const linkageFields = filterAnalyses(field.componentProps?.service?.params?.filter); + useEffect(() => { + const initValue = isVariable(field.value) ? undefined : field.value; + const value = Array.isArray(initValue) ? initValue.filter(Boolean) : initValue; + setInnerValue(value); + }, [field.value]); useEffect(() => { const id = uid(); form.addEffects(id, () => { @@ -111,7 +116,6 @@ const InternalAssociationSelect = observer((props: AssociationSelectProps) => { ); }; - return (
@@ -124,7 +128,6 @@ const InternalAssociationSelect = observer((props: AssociationSelectProps) => { service={service} onChange={(value) => { const val = value?.length !== 0 ? value : null; - setInnerValue(val); props.onChange?.(val); }} CustomDropdownRender={addMode === 'quickAdd' && QuickAddContent} diff --git a/packages/core/client/src/schema-component/antd/association-field/FileManager.tsx b/packages/core/client/src/schema-component/antd/association-field/FileManager.tsx index c81517e33..d7a935b55 100644 --- a/packages/core/client/src/schema-component/antd/association-field/FileManager.tsx +++ b/packages/core/client/src/schema-component/antd/association-field/FileManager.tsx @@ -96,6 +96,8 @@ const InternalFileManager = (props) => { }; }); setOptions(opts); + } else { + setOptions([]); } }, [value, fieldNames?.label]); diff --git a/packages/core/client/src/schema-component/antd/input/Json.tsx b/packages/core/client/src/schema-component/antd/input/Json.tsx index 0af77e35a..b4f841a33 100644 --- a/packages/core/client/src/schema-component/antd/input/Json.tsx +++ b/packages/core/client/src/schema-component/antd/input/Json.tsx @@ -15,6 +15,8 @@ export const Json = React.forwardRef( try { if (value != null) { setText(JSON.stringify(value, null, space)); + } else { + setText(undefined); } } catch (ex) { // diff --git a/packages/core/client/src/schema-component/antd/upload/Upload.tsx b/packages/core/client/src/schema-component/antd/upload/Upload.tsx index f27e178ff..c156a2929 100644 --- a/packages/core/client/src/schema-component/antd/upload/Upload.tsx +++ b/packages/core/client/src/schema-component/antd/upload/Upload.tsx @@ -33,7 +33,6 @@ Upload.Attachment = connect((props: UploadProps) => { const { t } = useTranslation(); const uploadProps = useUploadProps({ ...props }); const { wrapSSR, hashId, componentCls: prefixCls } = useStyles(); - const internalFileList = useRef([]); function closeIFrameModal() { @@ -169,6 +168,7 @@ Upload.Attachment = connect((props: UploadProps) => { onChange?.(toValue(list)); } setFileList(list.map(toItem)); + setSync(true); } else { if (info.file.status === 'done') { // TODO(BUG): object 的联动有问题,不响应,折中的办法先置空再赋值 @@ -176,6 +176,7 @@ Upload.Attachment = connect((props: UploadProps) => { onChange?.(info.file?.response?.data); } setFileList([toItem(info.file)]); + setSync(true); } }} showUploadList={false} @@ -229,6 +230,7 @@ Upload.Attachment = connect((props: UploadProps) => { onCancel={closeIFrameModal} footer={[ , - , ]} diff --git a/packages/plugins/@nocobase/plugin-formula-field/src/client/components/Formula/Result.tsx b/packages/plugins/@nocobase/plugin-formula-field/src/client/components/Formula/Result.tsx index 7a09d1e3f..cb0b46620 100644 --- a/packages/plugins/@nocobase/plugin-formula-field/src/client/components/Formula/Result.tsx +++ b/packages/plugins/@nocobase/plugin-formula-field/src/client/components/Formula/Result.tsx @@ -1,4 +1,4 @@ -import { onFormInputChange } from '@formily/core'; +import { onFormValuesChange } from '@formily/core'; import { useField, useFieldSchema, useFormEffects } from '@formily/react'; import { toJS } from '@formily/reactive'; import type { CollectionOptions } from '@nocobase/client'; @@ -82,7 +82,7 @@ export function Result(props) { }, [value]); useFormEffects(() => { - onFormInputChange((form) => { + onFormValuesChange((form) => { if ( (fieldSchema.name as string).indexOf('.') >= 0 || !formBlockContext?.form || @@ -100,7 +100,7 @@ export function Result(props) { v = null; } if (v == null && editingValue == null) { - return; + setEditingValue(v); } setEditingValue(v); }); diff --git a/packages/plugins/@nocobase/plugin-map/src/client/components/AMap/Map.tsx b/packages/plugins/@nocobase/plugin-map/src/client/components/AMap/Map.tsx index 657322d2b..14a7ce077 100644 --- a/packages/plugins/@nocobase/plugin-map/src/client/components/AMap/Map.tsx +++ b/packages/plugins/@nocobase/plugin-map/src/client/components/AMap/Map.tsx @@ -1,7 +1,7 @@ import AMapLoader from '@amap/amap-jsapi-loader'; import '@amap/amap-jsapi-types'; import { SyncOutlined } from '@ant-design/icons'; -import { useFieldSchema } from '@formily/react'; +import { useField, useFieldSchema } from '@formily/react'; import { css, useCollection } from '@nocobase/client'; import { useMemoizedFn } from 'ahooks'; import { Alert, App, Button, Spin } from 'antd'; @@ -293,6 +293,14 @@ export const AMapComponent = React.forwardRef { + if (!value && (mouseTool.current || editor.current)) { + toRemoveOverlay(); + if (editor.current) { + editor.current.setTarget(); + editor.current.close(); + } + onChange?.(null); + } if (!mouseTool.current || !editor.current) return; const target = editor.current.getTarget(); if (target) { diff --git a/packages/plugins/@nocobase/plugin-map/src/client/components/GoogleMaps/Map.tsx b/packages/plugins/@nocobase/plugin-map/src/client/components/GoogleMaps/Map.tsx index 980e83b61..5cf0f9231 100644 --- a/packages/plugins/@nocobase/plugin-map/src/client/components/GoogleMaps/Map.tsx +++ b/packages/plugins/@nocobase/plugin-map/src/client/components/GoogleMaps/Map.tsx @@ -268,6 +268,11 @@ export const GoogleMapsComponent = React.forwardRef { + if (!value && map.current) { + toRemoveOverlay(); + drawingManagerRef?.current?.setDrawingMode?.(drawingMode.current); + onChange?.(null); + } if (!map.current) return; if (!value || (!readonly && overlayRef.current)) { return;