Skip to content

Commit a2403ed

Browse files
authored
feat: Support component on Form (#19)
* support component * update father * remove never hit
1 parent 525bf35 commit a2403ed

File tree

5 files changed

+39
-39
lines changed

5 files changed

+39
-39
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ We use typescript to create the Type definition. You can view directly in IDE. B
6363

6464
| Prop | Description | Type | Default |
6565
| ---------------- | -------------------------------------------------- | ------------------------------------- | ---------------- |
66+
| component | Customize Form render component | string \| Component \| false | form |
6667
| fields | Control Form fields status. Only use when in Redux | [FieldData](#fielddata)[] | - |
6768
| form | Set form instance created by `useForm` | [FormInstance](#useform) | `Form.useForm()` |
6869
| initialValues | Initial value of Form | Object | - |

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"enzyme": "^3.1.0",
4848
"enzyme-adapter-react-16": "^1.0.2",
4949
"enzyme-to-json": "^3.1.4",
50-
"father": "^2.8.0",
50+
"father": "^2.13.2",
5151
"np": "^5.0.3",
5252
"react": "^v16.9.0-alpha.0",
5353
"react-dom": "^v16.9.0-alpha.0",

src/Form.tsx

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@ export interface FormProps extends BaseFormProps {
2020
initialValues?: Store;
2121
form?: FormInstance;
2222
children?: RenderProps | React.ReactNode;
23+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
24+
component?: false | string | React.FC<any> | React.ComponentClass<any>;
2325
fields?: FieldData[];
2426
name?: string;
2527
validateMessages?: ValidateMessages;
26-
__COMPATIBILITY_USAGE_OR_YOU_WILL_BE_FIRED__?: {
27-
NOT_CONTAIN_FORM?: boolean;
28-
HOOK_MARK: string;
29-
};
3028
onValuesChange?: Callbacks['onValuesChange'];
3129
onFieldsChange?: Callbacks['onFieldsChange'];
3230
onFinish?: (values: Store) => void;
@@ -39,11 +37,11 @@ const Form: React.FunctionComponent<FormProps> = (
3937
fields,
4038
form,
4139
children,
40+
component: Component = 'form',
4241
validateMessages,
4342
onValuesChange,
4443
onFieldsChange,
4544
onFinish,
46-
__COMPATIBILITY_USAGE_OR_YOU_WILL_BE_FIRED__,
4745
...restProps
4846
}: FormProps,
4947
ref,
@@ -113,21 +111,18 @@ const Form: React.FunctionComponent<FormProps> = (
113111
prevFieldsRef.current = fields;
114112
}, [fields, formInstance]);
115113

116-
if (
117-
__COMPATIBILITY_USAGE_OR_YOU_WILL_BE_FIRED__ &&
118-
__COMPATIBILITY_USAGE_OR_YOU_WILL_BE_FIRED__.NOT_CONTAIN_FORM &&
119-
__COMPATIBILITY_USAGE_OR_YOU_WILL_BE_FIRED__.HOOK_MARK ===
120-
'asdihasiodhaohdioahfoihsoefhisihifhsiofhiosfd'
121-
) {
122-
return (
123-
<FieldContext.Provider value={formInstance as InternalFormInstance}>
124-
{childrenNode}
125-
</FieldContext.Provider>
126-
);
114+
const wrapperNode = (
115+
<FieldContext.Provider value={formInstance as InternalFormInstance}>
116+
{childrenNode}
117+
</FieldContext.Provider>
118+
);
119+
120+
if (Component === false) {
121+
return wrapperNode;
127122
}
128123

129124
return (
130-
<form
125+
<Component
131126
{...restProps}
132127
onSubmit={event => {
133128
event.preventDefault();
@@ -144,10 +139,8 @@ const Form: React.FunctionComponent<FormProps> = (
144139
.catch(e => e);
145140
}}
146141
>
147-
<FieldContext.Provider value={formInstance as InternalFormInstance}>
148-
{childrenNode}
149-
</FieldContext.Provider>
150-
</form>
142+
{wrapperNode}
143+
</Component>
151144
);
152145
};
153146

src/utils/validateUtil.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,8 @@ function convertMessages(
3838
enum: (rule.enum || []).join(', '),
3939
};
4040

41-
const replaceFunc = (template: string, additionalKV?: Record<string, string>) => {
42-
if (!template) return null;
43-
return () => replaceMessage(template, { ...kv, ...additionalKV });
44-
};
41+
const replaceFunc = (template: string, additionalKV?: Record<string, string>) => () =>
42+
replaceMessage(template, { ...kv, ...additionalKV });
4543

4644
/* eslint-disable no-param-reassign */
4745
type Template =

tests/index.test.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,27 @@ describe('Form.Basic', () => {
2525
expect(wrapper.find('input').length).toBe(2);
2626
});
2727

28-
it('notContainForm', () => {
29-
const wrapper = mount(
30-
<Form
31-
__COMPATIBILITY_USAGE_OR_YOU_WILL_BE_FIRED__={{
32-
NOT_CONTAIN_FORM: true,
33-
HOOK_MARK: 'asdihasiodhaohdioahfoihsoefhisihifhsiofhiosfd',
34-
}}
35-
>
36-
{renderContent()}
37-
</Form>,
38-
);
39-
expect(wrapper.find('form').length).toBe(0);
40-
expect(wrapper.find('input').length).toBe(2);
28+
describe('component', () => {
29+
it('without dom', () => {
30+
const wrapper = mount(<Form component={false}>{renderContent()}</Form>);
31+
expect(wrapper.find('form').length).toBe(0);
32+
expect(wrapper.find('input').length).toBe(2);
33+
});
34+
35+
it('use string', () => {
36+
const wrapper = mount(<Form component="pre">{renderContent()}</Form>);
37+
expect(wrapper.find('form').length).toBe(0);
38+
expect(wrapper.find('pre').length).toBe(1);
39+
expect(wrapper.find('input').length).toBe(2);
40+
});
41+
42+
it('use component', () => {
43+
const MyComponent = ({ children }) => <div>{children}</div>;
44+
const wrapper = mount(<Form component={MyComponent}>{renderContent()}</Form>);
45+
expect(wrapper.find('form').length).toBe(0);
46+
expect(wrapper.find(MyComponent).length).toBe(1);
47+
expect(wrapper.find('input').length).toBe(2);
48+
});
4149
});
4250

4351
describe('render props', () => {

0 commit comments

Comments
 (0)