-
-
Notifications
You must be signed in to change notification settings - Fork 280
feat: support useWatch #413
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
Merged
Changes from 75 commits
Commits
Show all changes
80 commits
Select commit
Hold shift + click to select a range
3a1c5e9
feat: init
crazyair 73feed0
feat: watch
crazyair 9833b8e
feat: watch
crazyair e8b6e22
feat: watch
crazyair 595029f
feat: watch
crazyair 06e74ef
feat: watch
crazyair 2470933
feat: watch
crazyair 25c182a
feat: watch
crazyair ab0c98a
fix: resetFields
crazyair 806017b
feat: watch
crazyair cdd5b00
feat: watch
crazyair 35bb4e1
feat: watch
crazyair 01ada2b
feat: watch
crazyair deaf6b8
feat: add test
crazyair df922c6
feat: add test
crazyair fc0aa8a
feat: add test
crazyair d425c47
feat: add test
crazyair d6bd249
feat: add test
crazyair 64af139
feat: add test
crazyair 72d6b7f
feat: add test
crazyair d387c82
feat: test
crazyair 63fe68b
feat: test
crazyair ab9541b
feat: test
crazyair f091a8d
feat: add list
crazyair 8601a85
feat: add list
crazyair 77b6e71
feat: add test
crazyair 73f42e1
feat: add demo
crazyair d585905
feat: add demo
crazyair 4be1562
feat: api
crazyair eda6659
feat: api
crazyair 19218df
feat: review
crazyair ee50f0c
feat: watchId
crazyair 966980e
feat: watch
crazyair bc11e1e
feat: review
crazyair a1f62bc
feat: review
crazyair 1d1e31d
feat: all values
crazyair 5334673
feat: all values
crazyair ccdc2d0
feat: all values
crazyair c73a943
feat: all values
crazyair bb1c045
feat: test
crazyair d338c52
feat: watch
crazyair e74b6c1
feat: watch
crazyair 0c41cc1
feat: watch
crazyair c9fb129
feat: watch
crazyair 8b0acda
feat: watch
crazyair 04885af
feat: watch
crazyair 8b0373e
feat: file name
crazyair ac43148
feat: watch
crazyair 71f2331
feat: watch
crazyair 3639a17
feat: watch
crazyair b5ad67a
feat: watch
crazyair 40a83ea
feat: watch
crazyair 048086c
feat: watch
crazyair 02b4a71
feat: watch
crazyair 2d947bd
feat: watch
crazyair ce7f7cb
feat: watch
crazyair cd20496
feat: watch
crazyair 38e094e
feat: review
crazyair 2bf2011
feat: review
crazyair f59a635
feat: review
crazyair 8e85500
feat: review
crazyair 309fcdd
feat: review
crazyair 04c631d
feat: review
crazyair ce1714f
feat: review
crazyair aabbca1
feat: add demo
crazyair 3dc64f2
feat: values
crazyair d6c0a2a
feat: remove getRegisterFieldsValue
crazyair 785e07d
feat: watch
crazyair 8d2aa07
feat: remove setWatchCallbacks
crazyair 6272d97
feat: submit demo
crazyair 455b38a
feat: remove ref id
crazyair 807e6e4
feat: ts
crazyair e79f670
feat: rename
crazyair 1ba2e80
feat: init map
crazyair 0f0bb0e
refactor: Internal namePath logic adjust
zombieJ d254e8a
chore: misc update
zombieJ 420f0e4
fix: not crash if form is not exist
zombieJ 9c876c5
fix: form warning check
zombieJ 9a78c38
chore: adjust init logic
zombieJ f6b27b6
chore: clean up
zombieJ File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ## useWatch-list | ||
|
|
||
| <code src="../examples/useWatch-list.tsx" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ## useWatch | ||
|
|
||
| <code src="../examples/useWatch.tsx" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import React from 'react'; | ||
| import Form, { Field } from 'rc-field-form'; | ||
| import Input from './components/Input'; | ||
|
|
||
| const { List, useForm } = Form; | ||
|
|
||
| const Demo = () => { | ||
| const [form] = useForm(); | ||
| const list = Form.useWatch(['users'], form); | ||
| const values = Form.useWatch( | ||
| list?.users?.map((_, index) => ['users', index]), | ||
| form, | ||
| ); | ||
|
|
||
| console.log('values', values); | ||
|
|
||
| return ( | ||
| <div> | ||
| <Form form={form} style={{ border: '1px solid red', padding: 15 }}> | ||
| list length:{list?.users?.length} | ||
| <br /> | ||
| values: {JSON.stringify(values, null, 2)} | ||
| <Field name="main"> | ||
| <Input /> | ||
| </Field> | ||
| <List name="users" initialValue={['bamboo', 'light']}> | ||
| {(fields, { add, remove }) => { | ||
| return ( | ||
| <div> | ||
| {fields.map((field, index) => ( | ||
| <Field key={field.key} {...field} rules={[{ required: true }]}> | ||
| {control => ( | ||
| <div style={{ display: 'flex', alignItems: 'center' }}> | ||
| {index + 1} | ||
| <Input {...control} /> | ||
| <a onClick={() => remove(index)}>Remove</a> | ||
| </div> | ||
| )} | ||
| </Field> | ||
| ))} | ||
| <button type="button" onClick={() => add()}> | ||
| + New User | ||
| </button> | ||
| </div> | ||
| ); | ||
| }} | ||
| </List> | ||
| </Form> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default Demo; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import React, { useState } from 'react'; | ||
| import Form, { Field } from 'rc-field-form'; | ||
| import Input from './components/Input'; | ||
|
|
||
| let x = 0; | ||
|
|
||
| const Demo = React.memo(() => { | ||
| const values = Form.useWatch(['demo']); | ||
| console.log('demo watch', values); | ||
| return ( | ||
| <Field name="demo"> | ||
| <Input /> | ||
| </Field> | ||
| ); | ||
| }); | ||
| const Demo2 = React.memo(() => { | ||
| const values = Form.useWatch(['demo2']); | ||
| console.log('demo2 watch', values); | ||
| return ( | ||
| <Field name="demo2"> | ||
| <Input /> | ||
| </Field> | ||
| ); | ||
| }); | ||
|
|
||
| export default () => { | ||
| const [form] = Form.useForm(); | ||
| const [visible, setVisible] = useState(true); | ||
| const [visible2, setVisible2] = useState(true); | ||
| const [visible3, setVisible3] = useState(true); | ||
| const values = Form.useWatch(['name', 'age', 'initialValue'], form); | ||
| console.log('main watch', values); | ||
| return ( | ||
| <> | ||
| <Form | ||
| form={form} | ||
| initialValues={{ id: 1, age: '10', name: 'default' }} | ||
| onFinish={v => console.log('submit values', v)} | ||
| > | ||
| no render | ||
| <Field name="main"> | ||
| <Input /> | ||
| </Field> | ||
| name | ||
| {visible && ( | ||
| <Field name="name"> | ||
| <Input /> | ||
| </Field> | ||
| )} | ||
| age | ||
| <Field name="age"> | ||
| <Input /> | ||
| </Field> | ||
| initialValue | ||
| {visible3 && ( | ||
| <Field name="initialValue" initialValue="initialValue"> | ||
| <Input /> | ||
| </Field> | ||
| )} | ||
| name、age 改变 render | ||
| <Field dependencies={['field_1']}> | ||
| {() => { | ||
| x += 1; | ||
| return ` ${x}`; | ||
| }} | ||
| </Field> | ||
| <br /> | ||
| demo1 | ||
| <Demo /> | ||
| demo2 | ||
| {visible2 && <Demo2 />} | ||
| <button type="submit">submit</button> | ||
| </Form> | ||
| <button | ||
| onClick={() => { | ||
| console.log('values', form.getFieldsValue()); | ||
| console.log('values all', form.getFieldsValue(true)); | ||
| }} | ||
| > | ||
| getFieldsValue | ||
| </button> | ||
| <button | ||
| onClick={() => { | ||
| form.setFields([ | ||
| { name: 'name', value: 'name' }, | ||
| { name: 'age', value: 'age' }, | ||
| ]); | ||
| }} | ||
| > | ||
| setFields | ||
| </button> | ||
| <button onClick={() => form.resetFields()}>resetFields</button> | ||
| <button onClick={() => form.setFieldsValue({ name: `${form.getFieldValue('name') || ''}1` })}> | ||
| setFieldsValue | ||
| </button> | ||
| <button onClick={() => setVisible(c => !c)}>isShow name</button> | ||
| <button onClick={() => setVisible3(c => !c)}>isShow initialValue</button> | ||
| <button onClick={() => setVisible2(c => !c)}>isShow demo2</button> | ||
| </> | ||
| ); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
这里的
notifyWatch感觉位置不太对。。是不是该跟着updateStore后面?或者有没有可能可以把
notifyWatchnotifyObserversupdateStore合成一个方法?逻辑上应该是串联的Uh oh!
There was an error while loading. Please reload this page.
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.
本来可以把 notifyWatch 放到 notifyObservers 里,但是还有一点点不同,所以区分开了。不然也容易混乱
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.
@MadCcc OK 不?~
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.
感觉上问题不大,setState 相同 value 也不会触发 render
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.
会有一次缓存问题(会多 render 一次),明天再看看