-
-
Notifications
You must be signed in to change notification settings - Fork 282
feat: add getFieldsName API to form instance #787
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ## getFieldsName | ||
|
|
||
| <code src="../examples/getFieldsName.tsx"></code> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import Form, { Field } from 'rc-field-form'; | ||
| import React from 'react'; | ||
| import Input from './components/Input'; | ||
|
|
||
| export default () => { | ||
| const [form] = Form.useForm(); | ||
|
|
||
| return ( | ||
| <Form form={form}> | ||
| <Field name="username"> | ||
| <Input placeholder="Username" /> | ||
| </Field> | ||
| <Field name={['profile', 'email']}> | ||
| <Input placeholder="profile.email" /> | ||
| </Field> | ||
| <Field shouldUpdate> | ||
| {() => ( | ||
| <pre style={{ marginTop: 16, fontSize: 12 }}> | ||
| {JSON.stringify(form.getFieldsName(), null, 2)} | ||
| </pre> | ||
| )} | ||
| </Field> | ||
| </Form> | ||
| ); | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -87,6 +87,7 @@ export class FormStore { | |
| getFieldsValue: this.getFieldsValue, | ||
| getFieldError: this.getFieldError, | ||
| getFieldWarning: this.getFieldWarning, | ||
| getFieldsName: this.getFieldsName, | ||
| getFieldsError: this.getFieldsError, | ||
| isFieldsTouched: this.isFieldsTouched, | ||
| isFieldTouched: this.isFieldTouched, | ||
|
|
@@ -345,6 +346,11 @@ export class FormStore { | |
| return mergedValues; | ||
| }; | ||
|
|
||
| private getFieldsName = (): InternalNamePath[] => { | ||
| this.warningUnhooked(); | ||
| return this.getFieldEntities(true).map(entity => entity.getNamePath()); | ||
| }; | ||
|
Comment on lines
+349
to
+352
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current implementation of |
||
|
|
||
| private getFieldValue = (name: NamePath) => { | ||
| this.warningUnhooked(); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,98 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { render } from '@testing-library/react'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import React from 'react'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import Form, { Field, List } from '../src'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { FormInstance } from '../src'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { Input } from './common/InfoField'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| describe('getFieldsName', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('returns empty array when no named fields', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const formRef = React.createRef<FormInstance>(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| render(<Form ref={formRef} />); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(formRef.current!.getFieldsName()).toEqual([]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('returns name paths of registered fields', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const formRef = React.createRef<FormInstance>(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| render( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Form ref={formRef}> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Field name="username"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Input /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Field> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Field name={['profile', 'email']}> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Input /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Field> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Form>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(formRef.current!.getFieldsName()).toEqual([['username'], ['profile', 'email']]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('excludes field without name', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const formRef = React.createRef<FormInstance>(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| render( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Form ref={formRef}> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Field name="a"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Input /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Field> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Field>{() => null}</Field> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Form>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(formRef.current!.getFieldsName()).toEqual([['a']]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('includes one entry per Field with the same name', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const formRef = React.createRef<FormInstance>(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| render( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Form ref={formRef}> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Field name="x"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Input /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Field> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Field name="x"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Input /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Field> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Form>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(formRef.current!.getFieldsName()).toEqual([['x'], ['x']]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+42
to
+55
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 测试描述存在歧义,建议澄清
✏️ 建议的描述改法- it('includes one entry per Field with the same name', () => {
+ it('does not deduplicate Fields sharing the same name', () => {📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('updates when field unmounts', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const formRef = React.createRef<FormInstance>(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const Demo = ({ show }: { show: boolean }) => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Form ref={formRef}> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Field name="keep"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Input /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Field> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {show ? ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Field name="toggle"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Input /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Field> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) : null} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Form> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { rerender } = render(<Demo show />); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(formRef.current!.getFieldsName()).toEqual([['keep'], ['toggle']]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| rerender(<Demo show={false} />); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(formRef.current!.getFieldsName()).toEqual([['keep']]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('includes Form.List item fields', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const formRef = React.createRef<FormInstance>(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| render( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Form ref={formRef} initialValues={{ list: ['a', 'b'] }}> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <List name="list"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {fields => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fields.map(f => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Field {...f} key={f.key}> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Input /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Field> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| )) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </List> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Form>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(formRef.current!.getFieldsName()).toEqual([ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ['list', 0], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ['list', 1], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ['list'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个true看看是不是也是外面传进来更好?这个别着急改哈,以 @zombieJ @afc163 意见为准