Skip to content

Commit

Permalink
feat: 新增动态表单能力 (#602)
Browse files Browse the repository at this point in the history
* feat: 新增动态表单能力

* feat: 优化 form 样式 & 默认渲染 extra 节点

* feat: 新增动态表单能力
  • Loading branch information
honkinglin authored Apr 13, 2022
1 parent 6860d01 commit 3b82c6d
Show file tree
Hide file tree
Showing 14 changed files with 900 additions and 230 deletions.
2 changes: 1 addition & 1 deletion site/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default ({ mode }) => {
react(),
tdocPlugin(),
VitePWA(pwaConfig),
replace({ __DATE__: new Date().toISOString() }),
replace({ preventAssignment: false, __DATE__: new Date().toISOString() }),
// istanbul({
// cwd: resolvePath('../'),
// include: ['src/**/*'],
Expand Down
15 changes: 7 additions & 8 deletions src/form/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import useInstance from './hooks/useInstance';
import { StyledProps } from '../common';
import FormContext from './FormContext';
import FormItem from './FormItem';
import FormList from './FormList';

export interface FormProps extends TdFormProps, StyledProps {
children?: React.ReactNode;
Expand All @@ -28,7 +29,6 @@ const Form = forwardRefWithStatics(
statusIcon,
labelAlign = 'right',
layout = 'vertical',
size = 'medium',
colon = false,
requiredMark = globalFormConfig.requiredMark,
scrollToFirstError,
Expand All @@ -48,7 +48,7 @@ const Form = forwardRefWithStatics(
});

const formRef: React.RefObject<HTMLFormElement> = useRef();
const formItemsRef = useRef([]);
const formMapRef = useRef(new Map()); // 收集所有 formItem 实例

const {
submit,
Expand All @@ -60,7 +60,7 @@ const Form = forwardRefWithStatics(
validate,
clearValidate,
setValidateMessage,
} = useInstance(props, formRef, formItemsRef);
} = useInstance(props, formRef, formMapRef);

useImperativeHandle(ref as FormRefInterface, () => ({
currentElement: formRef.current,
Expand All @@ -80,8 +80,8 @@ const Form = forwardRefWithStatics(
e.preventDefault?.();
e.stopPropagation?.();
}
formItemsRef.current.forEach(({ current: formItemRef }) => {
formItemRef && formItemRef?.resetField();
[...formMapRef.current.values()].forEach((formItemRef) => {
formItemRef?.current.resetField();
});
onReset?.({ e });
}
Expand All @@ -105,7 +105,6 @@ const Form = forwardRefWithStatics(
statusIcon,
labelAlign,
layout,
size,
colon,
requiredMark,
errorMessage,
Expand All @@ -114,7 +113,7 @@ const Form = forwardRefWithStatics(
resetType,
rules,
disabled,
formItemsRef,
formMapRef,
onFormItemValueChange,
}}
>
Expand All @@ -131,7 +130,7 @@ const Form = forwardRefWithStatics(
</FormContext.Provider>
);
},
{ FormItem },
{ FormItem, FormList },
);

Form.displayName = 'Form';
Expand Down
23 changes: 17 additions & 6 deletions src/form/FormContext.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React from 'react';
import { TdFormProps } from './type';
import { TdFormProps, TdFormListProps } from './type';
import { FormItemInstance } from './FormItem';

const FormContext = React.createContext<{
labelWidth?: TdFormProps['labelWidth'];
statusIcon?: TdFormProps['statusIcon'];
labelAlign: TdFormProps['labelAlign'];
layout: TdFormProps['layout'];
size: TdFormProps['size'];
colon: TdFormProps['colon'];
requiredMark: TdFormProps['requiredMark'];
scrollToFirstError: TdFormProps['scrollToFirstError'];
Expand All @@ -15,13 +15,12 @@ const FormContext = React.createContext<{
disabled: TdFormProps['disabled'];
rules: TdFormProps['rules'];
errorMessage: TdFormProps['errorMessage'];
formItemsRef: React.RefObject<Array<React.RefObject<HTMLElement>>>;
formMapRef: React.RefObject<Map<any, React.RefObject<FormItemInstance>>>;
onFormItemValueChange: (changedValue: Record<string, unknown>) => void;
}>({
labelWidth: 'calc(1 / 12 * 100%)',
labelWidth: '100px',
labelAlign: 'right',
layout: 'vertical',
size: 'medium',
colon: false,
requiredMark: true,
scrollToFirstError: undefined,
Expand All @@ -32,9 +31,21 @@ const FormContext = React.createContext<{
errorMessage: undefined,
statusIcon: false,
onFormItemValueChange: undefined,
formItemsRef: null,
formMapRef: undefined,
});

export const useFormContext = () => React.useContext(FormContext);

export default FormContext;

export const FormListContext = React.createContext<{
name: string | number;
rules: TdFormListProps['rules'];
formListMapRef: React.RefObject<Map<any, React.RefObject<FormItemInstance>>>;
}>({
name: undefined,
rules: undefined,
formListMapRef: undefined,
});

export const useFormListContext = () => React.useContext(FormListContext);
Loading

0 comments on commit 3b82c6d

Please sign in to comment.