Skip to content

Commit 0565d12

Browse files
authored
fix: onValuesChange should not return store (#199)
* test: test driven * fix: onValueChange logic * fix ts
1 parent 2576399 commit 0565d12

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/useForm.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ export class FormStore {
566566

567567
if (onValuesChange) {
568568
const changedValues = cloneByNamePathList(this.store, [namePath]);
569-
onValuesChange(changedValues, this.store);
569+
onValuesChange(changedValues, this.getFieldsValue());
570570
}
571571

572572
this.triggerOnFieldsChange([namePath, ...childrenFields]);
@@ -780,7 +780,7 @@ export class FormStore {
780780

781781
function useForm<Values = any>(form?: FormInstance<Values>): [FormInstance<Values>] {
782782
const formRef = React.useRef<FormInstance>();
783-
const [, forceUpdate] = React.useState();
783+
const [, forceUpdate] = React.useState({});
784784

785785
if (!formRef.current) {
786786
if (form) {

tests/index.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,35 @@ describe('Form.Basic', () => {
212212
expect(onChange).toHaveBeenCalledWith(expect.objectContaining({ target: { value: 'Bamboo' } }));
213213
});
214214

215+
it('onValuesChange should not return fully value', async () => {
216+
const onValuesChange = jest.fn();
217+
218+
const Demo = ({ showField = true }) => (
219+
<Form onValuesChange={onValuesChange} initialValues={{ light: 'little' }}>
220+
{showField && (
221+
<Field name="light">
222+
<Input />
223+
</Field>
224+
)}
225+
<Field name="bamboo">
226+
<Input />
227+
</Field>
228+
</Form>
229+
);
230+
231+
const wrapper = mount(<Demo />);
232+
await changeValue(getField(wrapper, 'bamboo'), 'cute');
233+
expect(onValuesChange).toHaveBeenCalledWith(expect.anything(), {
234+
light: 'little',
235+
bamboo: 'cute',
236+
});
237+
238+
onValuesChange.mockReset();
239+
wrapper.setProps({ showField: false });
240+
await changeValue(getField(wrapper, 'bamboo'), 'beauty');
241+
expect(onValuesChange).toHaveBeenCalledWith(expect.anything(), { bamboo: 'beauty' });
242+
});
243+
215244
it('submit', async () => {
216245
const onFinish = jest.fn();
217246
const onFinishFailed = jest.fn();

0 commit comments

Comments
 (0)