Skip to content

Commit 9af332b

Browse files
authored
fix onFinish get full value instead of filtered one (#90)
* fix onFinish * add compile ci * fix ts
1 parent 73c24c8 commit 9af332b

File tree

4 files changed

+84
-24
lines changed

4 files changed

+84
-24
lines changed

.circleci/config.yml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,24 @@ jobs:
2929
- node_modules
3030
key: v1-dependencies-{{ checksum "package.json" }}
3131
- run: npm test -- --coverage && bash <(curl -s https://codecov.io/bash)
32+
compile:
33+
docker:
34+
- image: circleci/node:latest
35+
steps:
36+
- checkout
37+
- restore_cache:
38+
keys:
39+
- v1-dependencies-{{ checksum "package.json" }}
40+
- run: npm install
41+
- save_cache:
42+
paths:
43+
- node_modules
44+
key: v1-dependencies-{{ checksum "package.json" }}
45+
- run: npm run compile
3246
workflows:
3347
version: 2
3448
build_and_test:
3549
jobs:
3650
- lint
37-
- test
51+
- test
52+
- compile

src/Form.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export interface FormProps extends BaseFormProps {
3131
onFinishFailed?: Callbacks['onFinishFailed'];
3232
}
3333

34-
const Form: React.FunctionComponent<FormProps> = (
34+
const Form: React.ForwardRefRenderFunction<FormInstance, FormProps> = (
3535
{
3636
name,
3737
initialValues,

src/useForm.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ export class FormStore {
517517
errors: string[];
518518
}>[] = [];
519519

520-
this.getFieldEntities().forEach((field: FieldEntity) => {
520+
this.getFieldEntities(true).forEach((field: FieldEntity) => {
521521
// Add field if not provide `nameList`
522522
if (!provideNameList) {
523523
namePathList.push(field.getNamePath());

tests/validate.test.js

Lines changed: 66 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -272,35 +272,80 @@ describe('Form.Validate', () => {
272272
expect(form.getFieldError('title')).toEqual(['Title should be 3+ characters']);
273273
});
274274

275-
it('validate only accept exist fields', async () => {
276-
let form;
277-
const onFinish = jest.fn();
275+
describe('validate only accept exist fields', () => {
276+
it('skip init value', async () => {
277+
let form;
278+
const onFinish = jest.fn();
278279

279-
const wrapper = mount(
280-
<div>
280+
const wrapper = mount(
281+
<div>
282+
<Form
283+
onFinish={onFinish}
284+
ref={instance => {
285+
form = instance;
286+
}}
287+
initialValues={{ user: 'light', pass: 'bamboo' }}
288+
>
289+
<InfoField name="user">
290+
<Input />
291+
</InfoField>
292+
<button type="submit">submit</button>
293+
</Form>
294+
</div>,
295+
);
296+
297+
// Validate callback
298+
expect(await form.validateFields(['user'])).toEqual({ user: 'light' });
299+
expect(await form.validateFields()).toEqual({ user: 'light' });
300+
301+
// Submit callback
302+
wrapper.find('button').simulate('submit');
303+
await timeout();
304+
expect(onFinish).toHaveBeenCalledWith({ user: 'light' });
305+
});
306+
307+
it('remove from fields', async () => {
308+
const onFinish = jest.fn();
309+
const wrapper = mount(
281310
<Form
282311
onFinish={onFinish}
283-
ref={instance => {
284-
form = instance;
312+
initialValues={{
313+
switch: true,
314+
ignore: 'test',
285315
}}
286-
initialValues={{ user: 'light', pass: 'bamboo' }}
287316
>
288-
<InfoField name="user">
289-
<Input />
317+
<InfoField name="switch" valuePropName="checked">
318+
<Input type="checkbox" className="switch" />
290319
</InfoField>
320+
<Field shouldUpdate>
321+
{(_, __, { getFieldValue }) =>
322+
getFieldValue('switch') && (
323+
<InfoField name="ignore">
324+
<Input className="ignore" />
325+
</InfoField>
326+
)
327+
}
328+
</Field>
291329
<button type="submit">submit</button>
292-
</Form>
293-
</div>,
294-
);
295-
296-
// Validate callback
297-
expect(await form.validateFields(['user'])).toEqual({ user: 'light' });
298-
expect(await form.validateFields()).toEqual({ user: 'light' });
330+
</Form>,
331+
);
299332

300-
// Submit callback
301-
wrapper.find('button').simulate('submit');
302-
await timeout();
303-
expect(onFinish).toHaveBeenCalledWith({ user: 'light' });
333+
// Submit callback
334+
wrapper.find('button').simulate('submit');
335+
await timeout();
336+
expect(onFinish).toHaveBeenCalledWith({ switch: true, ignore: 'test' });
337+
onFinish.mockReset();
338+
339+
// Hide one
340+
wrapper.find('input.switch').simulate('change', {
341+
target: {
342+
checked: false,
343+
},
344+
});
345+
wrapper.find('button').simulate('submit');
346+
await timeout();
347+
expect(onFinish).toHaveBeenCalledWith({ switch: false });
348+
});
304349
});
305350

306351
it('should error in console if user script failed', async () => {

0 commit comments

Comments
 (0)