Skip to content

Commit 3ddb3aa

Browse files
authored
fix: ListContext makes nest Form event bubble out of range (#593)
* test: test driven * fix: ListConext should not pass
1 parent b48c222 commit 3ddb3aa

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/Form.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import FieldContext, { HOOK_MARK } from './FieldContext';
1212
import type { FormContextProps } from './FormContext';
1313
import FormContext from './FormContext';
1414
import { isSimilar } from './utils/valueUtil';
15+
import ListContext from './ListContext';
1516

1617
type BaseFormProps = Omit<React.FormHTMLAttributes<HTMLFormElement>, 'onSubmit' | 'children'>;
1718

@@ -147,7 +148,9 @@ const Form: React.ForwardRefRenderFunction<FormInstance, FormProps> = (
147148
);
148149

149150
const wrapperNode = (
150-
<FieldContext.Provider value={formContextValue}>{childrenNode}</FieldContext.Provider>
151+
<ListContext.Provider value={null}>
152+
<FieldContext.Provider value={formContextValue}>{childrenNode}</FieldContext.Provider>
153+
</ListContext.Provider>
151154
);
152155

153156
if (Component === false) {

tests/list.test.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,4 +827,32 @@ describe('Form.List', () => {
827827
expect(wrapper.find('.internal-rest').text()).toEqual('user');
828828
expect(wrapper.find('input').prop('value')).toEqual('bamboo');
829829
});
830+
831+
it('list should not pass context', () => {
832+
const onValuesChange = jest.fn();
833+
834+
const InnerForm = () => (
835+
<Form onValuesChange={onValuesChange}>
836+
<Field name="name">
837+
<Input />
838+
</Field>
839+
<Field name="age" initialValue={2}>
840+
<Input />
841+
</Field>
842+
</Form>
843+
);
844+
845+
const wrapper = mount(
846+
<Form>
847+
<Form.List name="parent">{() => <InnerForm />}</Form.List>
848+
</Form>,
849+
);
850+
851+
wrapper
852+
.find('input')
853+
.first()
854+
.simulate('change', { target: { value: 'little' } });
855+
856+
expect(onValuesChange).toHaveBeenCalledWith({ name: 'little' }, { name: 'little', age: 2 });
857+
});
830858
});

0 commit comments

Comments
 (0)