Skip to content

Commit ba82356

Browse files
authored
feat: Add setFieldValue API (#453)
* feat: Set field value * test: test case * chore: fix compile
1 parent 1c46bac commit ba82356

File tree

7 files changed

+63
-3
lines changed

7 files changed

+63
-3
lines changed

jest.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
setupFilesAfterEnv: ['<rootDir>/tests/setupAfterEnv.ts']
3+
};

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
"rc-util": "^5.8.0"
5555
},
5656
"devDependencies": {
57+
"@testing-library/jest-dom": "^5.16.4",
58+
"@testing-library/react": "^13.0.0",
5759
"@types/enzyme": "^3.10.5",
5860
"@types/jest": "^26.0.20",
5961
"@types/lodash": "^4.14.135",

src/FieldContext.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as React from 'react';
21
import warning from 'rc-util/lib/warning';
2+
import * as React from 'react';
33
import type { InternalFormInstance } from './interface';
44

55
export const HOOK_MARK = 'RC_FORM_INTERNAL_HOOKS';
@@ -21,6 +21,7 @@ const Context = React.createContext<InternalFormInstance>({
2121
isFieldsValidating: warningFunc,
2222
resetFields: warningFunc,
2323
setFields: warningFunc,
24+
setFieldValue: warningFunc,
2425
setFieldsValue: warningFunc,
2526
validateFields: warningFunc,
2627
submit: warningFunc,

src/interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ export interface FormInstance<Values = any> {
236236
isFieldsValidating: (nameList: NamePath[]) => boolean;
237237
resetFields: (fields?: NamePath[]) => void;
238238
setFields: (fields: FieldData[]) => void;
239+
setFieldValue: (name: NamePath, value: any) => void;
239240
setFieldsValue: (values: RecursivePartial<Values>) => void;
240241
validateFields: ValidateFields<Values>;
241242

src/useForm.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ export class FormStore {
9191
isFieldsValidating: this.isFieldsValidating,
9292
resetFields: this.resetFields,
9393
setFields: this.setFields,
94+
setFieldValue: this.setFieldValue,
9495
setFieldsValue: this.setFieldsValue,
9596
validateFields: this.validateFields,
9697
submit: this.submit,
@@ -752,6 +753,15 @@ export class FormStore {
752753
this.notifyWatch();
753754
};
754755

756+
private setFieldValue = (name: NamePath, value: any) => {
757+
this.setFields([
758+
{
759+
name,
760+
value,
761+
},
762+
]);
763+
};
764+
755765
private getDependencyChildrenFields = (rootNamePath: InternalNamePath): InternalNamePath[] => {
756766
const children: Set<FieldEntity> = new Set();
757767
const childrenFields: InternalNamePath[] = [];

tests/index.test.js

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import React from 'react';
21
import { mount } from 'enzyme';
32
import { resetWarned } from 'rc-util/lib/warning';
3+
import React from 'react';
44
import Form, { Field, useForm } from '../src';
5-
import InfoField, { Input } from './common/InfoField';
65
import { changeValue, getField, matchError } from './common';
6+
import InfoField, { Input } from './common/InfoField';
77
import timeout from './common/timeout';
88

99
describe('Form.Basic', () => {
@@ -838,4 +838,46 @@ describe('Form.Basic', () => {
838838

839839
expect(wrapper.find('input').prop('value')).toEqual('bamboo');
840840
});
841+
842+
it('setFieldValue', () => {
843+
const formRef = React.createRef();
844+
845+
const Demo = () => (
846+
<Form ref={formRef} initialValues={{ list: ['bamboo', 'little', 'light'] }}>
847+
<Form.List name="list">
848+
{fields =>
849+
fields.map(field => (
850+
<Field key={field.key} {...field}>
851+
<Input />
852+
</Field>
853+
))
854+
}
855+
</Form.List>
856+
857+
<Field name={['nest', 'target']} initialValue="nested">
858+
<Input />
859+
</Field>
860+
</Form>
861+
);
862+
863+
const wrapper = mount(<Demo />);
864+
expect(wrapper.find('input').map(input => input.prop('value'))).toEqual([
865+
'bamboo',
866+
'little',
867+
'light',
868+
'nested',
869+
]);
870+
871+
// Set
872+
formRef.current.setFieldValue(['list', 1], 'tiny');
873+
formRef.current.setFieldValue(['nest', 'target'], 'match');
874+
wrapper.update();
875+
876+
expect(wrapper.find('input').map(input => input.prop('value'))).toEqual([
877+
'bamboo',
878+
'tiny',
879+
'light',
880+
'match',
881+
]);
882+
});
841883
});

tests/setupAfterEnv.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import '@testing-library/jest-dom';

0 commit comments

Comments
 (0)