Skip to content

Support nested fields and arrays #48

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
merged 1 commit into from
Dec 28, 2016
Merged

Support nested fields and arrays #48

merged 1 commit into from
Dec 28, 2016

Conversation

williamkhshea
Copy link
Contributor

See #47

In input form

{getFieldDecorator('member[0].name.firstname', {})(<input/>)}
{getFieldDecorator('member[1].name.firstname', {})(<input/>)}

this.props.form.getFieldsValue() 返回数据结构为

members: [{ name: {firstname: "xxx"} }, { name: {firstname: "yyy"} }]

@coveralls
Copy link

Coverage Status

Coverage increased (+5.005%) to 93.061% when pulling 9be77bd on williamkhshea:nested-field into 616ddd3 on react-component:master.

@alexggordon
Copy link

@williamkhshea just saw this. Upon validate, does this also return the same nested object?

IE, on submit, will it return

{member: [
     {name: {firstname: 'fdsafdsa'}},
     {name: {firstname: 'firstname_two'}}
]}

If it's doing this, then I'll close my PR in favor of this one.

@williamkhshea
Copy link
Contributor Author

@alexggordon Yes, and errors will be nested object as well.

I have added an example called "nested-field" in the example folder. You can check it out and see what how the outputs look like.

@alexggordon
Copy link

Sweet job @williamkhshea.

@afc163
Copy link
Member

afc163 commented Dec 19, 2016

👍

@@ -62,6 +62,7 @@
"babel-runtime": "6.x",
"dom-scroll-into-view": "1.x",
"hoist-non-react-statics": "1.x",
"lodash": "^4.17.2",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do not use lodash(especially too big for mobile), or at least use lodash-xx

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yiminghe OK, I will update to use custom build of lodash

@williamkhshea
Copy link
Contributor Author

@yiminghe How can I ignore the lint style checking for the lodash custom build file before I commit?

@yiminghe
Copy link
Member

I mean something like https://www.npmjs.com/package/lodash.get

@coveralls
Copy link

Coverage Status

Coverage increased (+5.07%) to 93.131% when pulling 75a854c on williamkhshea:nested-field into 616ddd3 on react-component:master.

@williamkhshea
Copy link
Contributor Author

@yiminghe Dependencies updated. Please review.

@afc163
Copy link
Member

afc163 commented Dec 22, 2016

Use https://github.com/mariocasciaro/object-path ?

@williamkhshea
Copy link
Contributor Author

@afc163 Object-path uses only dot notation for array path (i.e. 'a.0'), unlike lodash which also supports bracket notation (i.e. 'a[0]'). It creates problems when you want to have numbers as keys of an object, says in case of IDs. It gives you an array instead of object with keys as IDs.

If you want to trim down the size of the libraries further, I would suggest you do a custom build on lodash get,has,set function and make an npm package, and I can use it as dependencies.

Copy link
Member

@yiminghe yiminghe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

last change. please squash into one commit. ready to merge

thank you

b: ['7', '8'],
})).to.be(true);
form.resetFields();
expect(isEqual(form.getFieldValue('foo'), {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expect(form.getFieldValue('foo')).to.eql({....})

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yiminghe expect(...).to.eql does not handle sparse arrays (e.g. [undefined, 123]). See screenshot below.

2016-12-22 16_15_58-will s imac - teamviewer - free license non-commercial use only

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, squash into one commit, and force push please

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yiminghe done

@coveralls
Copy link

Coverage Status

Coverage increased (+5.07%) to 93.131% when pulling 4aa1fab on williamkhshea:nested-field into 616ddd3 on react-component:master.

@yiminghe yiminghe merged commit ad01095 into react-component:master Dec 28, 2016
@williamkhshea williamkhshea deleted the nested-field branch December 28, 2016 12:27
@williamkhshea
Copy link
Contributor Author

@yiminghe I should have cleaned up my code before submitting. Thanks for helping out.

afc163 added a commit to ant-design/ant-design that referenced this pull request Dec 30, 2016
ddcat1115 pushed a commit to ant-design/ant-design that referenced this pull request Dec 31, 2016
* support nested form item name by upgrade rc-form

react-component/form#48

* Fix dynamic form demo
benjycui referenced this pull request in react-component/table Jan 5, 2017
@acmeid
Copy link

acmeid commented Mar 20, 2017

我在使用嵌套写法的时候,并没有生效:

<FormItem label="数量" colon={false}>
  {getFieldDecorator('services[0].num', {
      rules: [{ required: true, message: '请输入数量' }],
   })(
      <InputNumber />
   )}
</FormItem>

services: [{
    num: {value: 1}
}]

@mkrn
Copy link

mkrn commented Apr 23, 2017

@williamkhshea Can this bug be related to this PR?
ant-design/ant-design#5835
Can you please give me a hint if you have this situation working (array of objects)

@nanxiaobei
Copy link

then setFieldsValue not work with nested assign, anyone know how to solve?

@benjycui
Copy link
Member

benjycui commented Mar 8, 2018

@nanxiaobei please file a new issue and provide re-producible demo http://codepen.io/benjycui/pen/NdQdBw?editors=0011

@warmhug
Copy link
Contributor

warmhug commented Aug 29, 2018

isFieldTouched seems also not work with nested.

@jarforjs
Copy link

then setFieldsValue not work with nested assign, anyone know how to solve?

me too,then i use setFiledsValue(()=>{setTimeout...}),Nevertheless, it seems to be successful, but there are still problems

@17601617393
Copy link

参见#47

输入形式

{getFieldDecorator('member[0].name.firstname', {})(<input/>)}
{getFieldDecorator('member[1].name.firstname', {})(<input/>)}

this.props.form.getFieldsValue()返回数据结构为

members: [{ name: {firstname: "xxx"} }, { name: {firstname: "yyy"} }]

id再转一下string类型就OK了

@Jiang-Xuan
Copy link

如果有人想要 key 里面含有 . 的话, 参考 lodash/lodash#3529 (comment)

@pkuphy
Copy link

pkuphy commented May 19, 2023

楼上的方法可以,效果是这样的:

getFieldDecorator('["user.name"]', ...

Screenshot-2023-05-19_16_43_22

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.