Skip to content

Commit d03aa6c

Browse files
JIACHENG9zombieJ
authored andcommitted
support notContainForm (#13)
* feat: add notContainForm props to allow customize form * chore: add yarn.lock to .gitignore
1 parent 0a62768 commit d03aa6c

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ node_modules
44
coverage/
55
es/
66
lib/
7-
~*
7+
~*
8+
yarn.lock
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import React from 'react';
2+
import Form, { Field, useForm } from '../src/';
3+
import Input from './components/Input';
4+
5+
6+
export default () => {
7+
const [form] = useForm();
8+
return (
9+
<form onSubmit={event => {
10+
event.preventDefault();
11+
event.stopPropagation();
12+
form.validateFields().then(function (values) {
13+
console.log(values);
14+
}) // Do nothing about submit catch
15+
.catch(function (e) {
16+
return e;
17+
});
18+
}}>
19+
<Form notContainForm form={form}>
20+
<Field name="username">
21+
<Input placeholder="Username" />
22+
</Field>
23+
<Field name="password">
24+
<Input placeholder="Password" />
25+
</Field>
26+
</Form>
27+
<button type="submit">submit</button>
28+
</form>
29+
);
30+
}

src/Form.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ export interface FormProps extends BaseFormProps {
2222
fields?: FieldData[];
2323
name?: string;
2424
validateMessages?: ValidateMessages;
25+
__COMPATIBILITY_USAGE_OR_YOU_WILL_BE_FIRED__?: {
26+
NOT_CONTAIN_FORM?: boolean;
27+
HOOK_MARK: string;
28+
};
2529
onValuesChange?: Callbacks['onValuesChange'];
2630
onFieldsChange?: Callbacks['onFieldsChange'];
2731
onFinish?: (values: Store) => void;
@@ -38,6 +42,7 @@ const Form: React.FunctionComponent<FormProps> = (
3842
onValuesChange,
3943
onFieldsChange,
4044
onFinish,
45+
__COMPATIBILITY_USAGE_OR_YOU_WILL_BE_FIRED__,
4146
...restProps
4247
}: FormProps,
4348
ref,
@@ -105,6 +110,19 @@ const Form: React.FunctionComponent<FormProps> = (
105110
}
106111
prevFieldsRef.current = fields;
107112

113+
if (
114+
__COMPATIBILITY_USAGE_OR_YOU_WILL_BE_FIRED__ &&
115+
__COMPATIBILITY_USAGE_OR_YOU_WILL_BE_FIRED__.NOT_CONTAIN_FORM &&
116+
__COMPATIBILITY_USAGE_OR_YOU_WILL_BE_FIRED__.HOOK_MARK ===
117+
'asdihasiodhaohdioahfoihsoefhisihifhsiofhiosfd'
118+
) {
119+
return (
120+
<FieldContext.Provider value={formInstance as InternalFormInstance}>
121+
{childrenNode}
122+
</FieldContext.Provider>
123+
);
124+
}
125+
108126
return (
109127
<form
110128
{...restProps}

tests/index.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ describe('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);
41+
});
42+
2843
describe('render props', () => {
2944
it('normal', () => {
3045
const wrapper = mount(<Form>{renderContent}</Form>);

0 commit comments

Comments
 (0)