Skip to content

Commit

Permalink
fix(form): onInit use ProFormInstance
Browse files Browse the repository at this point in the history
close #7226
  • Loading branch information
chenshuai2144 committed Jun 17, 2023
1 parent b412ab9 commit ad537a5
Show file tree
Hide file tree
Showing 19 changed files with 188 additions and 76 deletions.
12 changes: 8 additions & 4 deletions packages/field/src/components/Cascader/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ const FieldCascader: ProFieldFC<GroupProps> = (
const cascaderRef = useRef();
const [open, setOpen] = useState(false);

useImperativeHandle(ref, () => ({
...(cascaderRef.current || {}),
fetchData: (keyWord: string) => fetchData(keyWord),
}));
useImperativeHandle(
ref,
() => ({
...(cascaderRef.current || {}),
fetchData: (keyWord: string) => fetchData(keyWord),
}),
[fetchData],
);

const optionsValueEnum = useMemo(() => {
if (mode !== 'read') return;
Expand Down
12 changes: 8 additions & 4 deletions packages/field/src/components/Checkbox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,14 @@ const FieldCheckbox: ProFieldFC<GroupProps> = (
};
});
const checkBoxRef = useRef();
useImperativeHandle(ref, () => ({
...(checkBoxRef.current || {}),
fetchData: (keyWord: string) => fetchData(keyWord),
}));
useImperativeHandle(
ref,
() => ({
...(checkBoxRef.current || {}),
fetchData: (keyWord: string) => fetchData(keyWord),
}),
[fetchData],
);

if (loading) {
return <Spin size="small" />;
Expand Down
12 changes: 8 additions & 4 deletions packages/field/src/components/Radio/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@ const FieldRadio: ProFieldFC<GroupProps> = (
const [loading, options, fetchData] = useFieldFetchData(rest);
const radioRef = useRef();

useImperativeHandle(ref, () => ({
...(radioRef.current || {}),
fetchData: (keyWord: string) => fetchData(keyWord),
}));
useImperativeHandle(
ref,
() => ({
...(radioRef.current || {}),
fetchData: (keyWord: string) => fetchData(keyWord),
}),
[fetchData],
);

// css
const { wrapSSR, hashId } = useStyle('FieldRadioRadio', (token) => {
Expand Down
12 changes: 8 additions & 4 deletions packages/field/src/components/Segmented/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ const FieldSegmented: ProFieldFC<

const [loading, options, fetchData] = useFieldFetchData(rest);

useImperativeHandle(ref, () => ({
...(inputRef.current || {}),
fetchData: (keyWord: string) => fetchData(keyWord),
}));
useImperativeHandle(
ref,
() => ({
...(inputRef.current || {}),
fetchData: (keyWord: string) => fetchData(keyWord),
}),
[fetchData],
);

if (loading) {
return <Spin size="small" />;
Expand Down
12 changes: 8 additions & 4 deletions packages/field/src/components/Select/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -520,10 +520,14 @@ const FieldSelect: ProFieldFC<
const { componentSize } = ConfigProvider?.useConfig?.() || {
componentSize: 'middle',
};
useImperativeHandle(ref, () => ({
...(inputRef.current || {}),
fetchData: (keyWord: string) => fetchData(keyWord),
}));
useImperativeHandle(
ref,
() => ({
...(inputRef.current || {}),
fetchData: (keyWord: string) => fetchData(keyWord),
}),
[fetchData],
);

const optionsValueEnum = useMemo(() => {
if (mode !== 'read') return;
Expand Down
4 changes: 3 additions & 1 deletion packages/field/src/components/Text/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ const FieldText: ProFieldFC<{

const intl = useIntl();
const inputRef = useRef<HTMLInputElement>();
useImperativeHandle(ref, () => inputRef.current);

useImperativeHandle(ref, () => inputRef.current, []);

useEffect(() => {
if (autoFocus) {
inputRef.current?.focus();
Expand Down
15 changes: 11 additions & 4 deletions packages/form/src/BaseForm/BaseForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,6 @@ function BaseFormComponents<T = Record<string, any>>(
const transformedKey = transformKey(values, omitNil);
return transformedKey ? transformedKey : {};
},
formRef,
}),
[omitNil, transformKey],
);
Expand Down Expand Up @@ -447,18 +446,26 @@ function BaseFormComponents<T = Record<string, any>>(
...formatValues,
};
},
[],
[formatValues, formRef.current],
);
useEffect(() => {
const finalValues = transformKey(
formRef.current?.getFieldsValue?.(true),
omitNil,
);
onInit?.(finalValues, formRef.current);
onInit?.(finalValues, {
...formRef.current,
...formatValues,
});
}, []);

return (
<ProFormContext.Provider value={formatValues}>
<ProFormContext.Provider
value={{
...formatValues,
formRef,
}}
>
<ConfigProvider componentSize={rest.size || componentSize}>
<GridContext.Provider value={{ grid, colProps }}>
{rest.component !== false && (
Expand Down
10 changes: 7 additions & 3 deletions packages/form/src/layouts/DrawerForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,13 @@ function DrawerForm<T = Record<string, any>>({
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [propVisible, open]);

useImperativeHandle(rest.formRef, () => {
return formRef.current;
});
useImperativeHandle(
rest.formRef,
() => {
return formRef.current;
},
[formRef.current],
);

const triggerDom = useMemo(() => {
if (!trigger) {
Expand Down
2 changes: 1 addition & 1 deletion packages/form/src/layouts/LightFilter/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ function LightFilter<T = Record<string, any>>(props: LightFilterProps<T>) {
});
const formRef = useRef<ProFormInstance>();

useImperativeHandle(userFormRef, () => formRef.current);
useImperativeHandle(userFormRef, () => formRef.current, [formRef.current]);

return (
<BaseForm
Expand Down
10 changes: 7 additions & 3 deletions packages/form/src/layouts/ModalForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,13 @@ function ModalForm<T = Record<string, any>>({
}
}, [modalProps?.destroyOnClose, rest.form, rest.formRef]);

useImperativeHandle(rest.formRef, () => {
return formRef.current;
});
useImperativeHandle(
rest.formRef,
() => {
return formRef.current;
},
[formRef.current],
);

useEffect(() => {
if (open && (propsOpen || propVisible)) {
Expand Down
5 changes: 4 additions & 1 deletion packages/form/src/layouts/StepsForm/StepForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ function StepForm<T = Record<string, any>>(props: StepFormProps<T>) {

//@ts-expect-error
noteOnce(!restProps.submitter, 'StepForm 不包含提交按钮,请在 StepsForm 上');

/** 重置 formRef */
useImperativeHandle(propFormRef, () => formRef.current);
useImperativeHandle(propFormRef, () => formRef.current, [
propFormRef?.current,
]);

/** Dom 不存在的时候解除挂载 */
useEffect(() => {
Expand Down
6 changes: 4 additions & 2 deletions packages/form/src/layouts/StepsForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,16 @@ function StepsForm<T = Record<string, any>>(
formDataRef.current.delete(name);
}, []);

useImperativeHandle(propsFormMapRef, () => formArrayRef.current);
useImperativeHandle(propsFormMapRef, () => formArrayRef.current, [
formArrayRef.current,
]);

useImperativeHandle(
formRef,
() => {
return formArrayRef.current[step || 0]?.current;
},
[step],
[step, formArrayRef.current],
);

/**
Expand Down
4 changes: 3 additions & 1 deletion packages/list/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ function NoProVideProList<

const actionRef = useRef<ActionType>();

useImperativeHandle(rest.actionRef, () => actionRef.current);
useImperativeHandle(rest.actionRef, () => actionRef.current, [
actionRef.current,
]);

const { getPrefixCls } = useContext(ConfigProvider.ConfigContext);

Expand Down
4 changes: 3 additions & 1 deletion packages/table/src/components/EditableTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ function EditableTable<
const formRef = useRef<ProFormInstance>();

// 设置 ref
useImperativeHandle(rest.actionRef, () => actionRef.current);
useImperativeHandle(rest.actionRef, () => actionRef.current, [
actionRef.current,
]);

const [value, setValue] = useMergedState<readonly DataType[]>(
() => props.value || defaultValue || [],
Expand Down
10 changes: 7 additions & 3 deletions packages/table/src/components/Form/FormRender.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import type { BaseQueryFilterProps, ProFormProps } from '@ant-design/pro-form';
import type {
BaseQueryFilterProps,
ProFormInstance,
ProFormProps,
} from '@ant-design/pro-form';
import { BetaSchemaForm } from '@ant-design/pro-form';
import { ProProvider } from '@ant-design/pro-provider';
import type { ProSchemaComponentTypes } from '@ant-design/pro-utils';
import type { FormInstance, FormItemProps } from 'antd';
import type { FormItemProps } from 'antd';
import { ConfigProvider, Table } from 'antd';
import classNames from 'classnames';
import omit from 'omit.js';
Expand Down Expand Up @@ -99,7 +103,7 @@ export type TableFormItem<T, U = any> = {
dateFormatter?: ProTableProps<T, U, any>['dateFormatter'];
search?: false | SearchConfig;
columns: ProColumns<U, any>[];
formRef: React.MutableRefObject<FormInstance | undefined>;
formRef: React.MutableRefObject<ProFormInstance | undefined>;
submitButtonLoading?: boolean;
manualRequest?: boolean;
bordered?: boolean;
Expand Down
12 changes: 9 additions & 3 deletions packages/utils/src/useEditableArray/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,15 @@ export function SaveEditableAction<T>(
throw error;
}
});
useImperativeHandle(ref, () => ({
save,
}));

// 保存数据
useImperativeHandle(
ref,
() => ({
save,
}),
[save],
);

return (
<a
Expand Down
Loading

0 comments on commit ad537a5

Please sign in to comment.