Add multiple form fields but show only one at a time
I need final-form
to be able to add array of form field records. But want to display only one field of array at a time. Like on left side I will have customer id
or index
which user will select and on right side i have to show customer corresponding to that index
. I am able to add reac-final-form-array
, but it always show all array elements. What should be the right approach to show only selected customer.
Please check the below code for reference. Hope my question is clear, if not please let me know, will add more explanation.
<FieldArray name="customer">
{({ fields }) => (
fields.map((name, index) => (
<div key={index}>
<Field name={`${name}.firstName`} />
<Field name={`${name}.lastName`} />
</div>
))
)}
</FieldArray>
To add new customer:
<div className="buttons">
<button
type="button"
onClick={() => push('customers', undefined)}>
Add Customer
</button>
</div>
Currently its looking like:
I need it to look like this:
reactjs react-final-form react-final-form-arrays
add a comment |
I need final-form
to be able to add array of form field records. But want to display only one field of array at a time. Like on left side I will have customer id
or index
which user will select and on right side i have to show customer corresponding to that index
. I am able to add reac-final-form-array
, but it always show all array elements. What should be the right approach to show only selected customer.
Please check the below code for reference. Hope my question is clear, if not please let me know, will add more explanation.
<FieldArray name="customer">
{({ fields }) => (
fields.map((name, index) => (
<div key={index}>
<Field name={`${name}.firstName`} />
<Field name={`${name}.lastName`} />
</div>
))
)}
</FieldArray>
To add new customer:
<div className="buttons">
<button
type="button"
onClick={() => push('customers', undefined)}>
Add Customer
</button>
</div>
Currently its looking like:
I need it to look like this:
reactjs react-final-form react-final-form-arrays
add a comment |
I need final-form
to be able to add array of form field records. But want to display only one field of array at a time. Like on left side I will have customer id
or index
which user will select and on right side i have to show customer corresponding to that index
. I am able to add reac-final-form-array
, but it always show all array elements. What should be the right approach to show only selected customer.
Please check the below code for reference. Hope my question is clear, if not please let me know, will add more explanation.
<FieldArray name="customer">
{({ fields }) => (
fields.map((name, index) => (
<div key={index}>
<Field name={`${name}.firstName`} />
<Field name={`${name}.lastName`} />
</div>
))
)}
</FieldArray>
To add new customer:
<div className="buttons">
<button
type="button"
onClick={() => push('customers', undefined)}>
Add Customer
</button>
</div>
Currently its looking like:
I need it to look like this:
reactjs react-final-form react-final-form-arrays
I need final-form
to be able to add array of form field records. But want to display only one field of array at a time. Like on left side I will have customer id
or index
which user will select and on right side i have to show customer corresponding to that index
. I am able to add reac-final-form-array
, but it always show all array elements. What should be the right approach to show only selected customer.
Please check the below code for reference. Hope my question is clear, if not please let me know, will add more explanation.
<FieldArray name="customer">
{({ fields }) => (
fields.map((name, index) => (
<div key={index}>
<Field name={`${name}.firstName`} />
<Field name={`${name}.lastName`} />
</div>
))
)}
</FieldArray>
To add new customer:
<div className="buttons">
<button
type="button"
onClick={() => push('customers', undefined)}>
Add Customer
</button>
</div>
Currently its looking like:
I need it to look like this:
reactjs react-final-form react-final-form-arrays
reactjs react-final-form react-final-form-arrays
edited Nov 21 at 9:47
asked Nov 21 at 9:39
Pardeep Dhingra
3,12062243
3,12062243
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Find the below code.
import React from "react";
import { Form, Field } from "react-final-form";
import arrayMutators from "final-form-arrays";
import { FieldArray } from "react-final-form-arrays";
const onSubmit = () => {
console.log("submitted");
};
const validate = () => {
// console.log("validated");
};
const MyForm = () => (
<Form
onSubmit={onSubmit}
mutators={{
// potentially other mutators could be merged here
...arrayMutators
}}
validate={validate}
render={({ handleSubmit, pristine, invalid }) => (
<form onSubmit={handleSubmit}>
<FieldArray name="customers">
{({ fields }) => (
<div>
<button
type="button"
onClick={() =>
fields.push({ firstName: "", lastName: "", isVisible: true })
}
>
Add Customer
</button>
{fields.map((name, index) => (
<div key={name}>
<a
onClick={() =>
(fields.value[index].isVisible = !fields.value[index]
.isVisible)
}
>{`Cust #${index}`}</a>
{fields.value[index].isVisible ? (
<div>
<div>
<Field name={`${name}.firstName`} component="input" />
</div>
<div>
<Field name={`${name}.lastName`} component="input" />
</div>
</div>
) : null}
<button type="button" onClick={() => fields.remove(index)}>
Remove
</button>
</div>
))}
</div>
)}
</FieldArray>
</form>
)}
/>
);
export default MyForm;
Check the codesandbox link here
add a comment |
In the fields array, you can add one more key isVisible.
It will look like this:
fields = [
{
firstName: 'John',
lastName: 'Doe',
isVisible: true,
},
{
firstName: 'Jane',
lastName: 'Doe',
isVisible: false,
}
];
Now while showing, only render the field when isVisible is true,
<FieldArray name="customer">
{({ fields }) => (
fields.map((name, index) => {
if(name.isVisible){
return (
<div key={index}>
<Field name={`${name}.firstName`} />
<Field name={`${name}.lastName`} />
</div>
);
))
)}
</FieldArray>
You can control isVisible key by clicking Cust # button.
Thanks for answer. Isn't it going to leave multiple unused fields on the page?
– Pardeep Dhingra
Nov 21 at 10:32
you can change all the other isVisible to false. For example, when you click on CUST #1, you can make it's isVisible true and all other isVisible false. That way only one will be visible at a time.
– Ratnajeet Shyamkunwar
Nov 21 at 10:53
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409104%2fadd-multiple-form-fields-but-show-only-one-at-a-time%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Find the below code.
import React from "react";
import { Form, Field } from "react-final-form";
import arrayMutators from "final-form-arrays";
import { FieldArray } from "react-final-form-arrays";
const onSubmit = () => {
console.log("submitted");
};
const validate = () => {
// console.log("validated");
};
const MyForm = () => (
<Form
onSubmit={onSubmit}
mutators={{
// potentially other mutators could be merged here
...arrayMutators
}}
validate={validate}
render={({ handleSubmit, pristine, invalid }) => (
<form onSubmit={handleSubmit}>
<FieldArray name="customers">
{({ fields }) => (
<div>
<button
type="button"
onClick={() =>
fields.push({ firstName: "", lastName: "", isVisible: true })
}
>
Add Customer
</button>
{fields.map((name, index) => (
<div key={name}>
<a
onClick={() =>
(fields.value[index].isVisible = !fields.value[index]
.isVisible)
}
>{`Cust #${index}`}</a>
{fields.value[index].isVisible ? (
<div>
<div>
<Field name={`${name}.firstName`} component="input" />
</div>
<div>
<Field name={`${name}.lastName`} component="input" />
</div>
</div>
) : null}
<button type="button" onClick={() => fields.remove(index)}>
Remove
</button>
</div>
))}
</div>
)}
</FieldArray>
</form>
)}
/>
);
export default MyForm;
Check the codesandbox link here
add a comment |
Find the below code.
import React from "react";
import { Form, Field } from "react-final-form";
import arrayMutators from "final-form-arrays";
import { FieldArray } from "react-final-form-arrays";
const onSubmit = () => {
console.log("submitted");
};
const validate = () => {
// console.log("validated");
};
const MyForm = () => (
<Form
onSubmit={onSubmit}
mutators={{
// potentially other mutators could be merged here
...arrayMutators
}}
validate={validate}
render={({ handleSubmit, pristine, invalid }) => (
<form onSubmit={handleSubmit}>
<FieldArray name="customers">
{({ fields }) => (
<div>
<button
type="button"
onClick={() =>
fields.push({ firstName: "", lastName: "", isVisible: true })
}
>
Add Customer
</button>
{fields.map((name, index) => (
<div key={name}>
<a
onClick={() =>
(fields.value[index].isVisible = !fields.value[index]
.isVisible)
}
>{`Cust #${index}`}</a>
{fields.value[index].isVisible ? (
<div>
<div>
<Field name={`${name}.firstName`} component="input" />
</div>
<div>
<Field name={`${name}.lastName`} component="input" />
</div>
</div>
) : null}
<button type="button" onClick={() => fields.remove(index)}>
Remove
</button>
</div>
))}
</div>
)}
</FieldArray>
</form>
)}
/>
);
export default MyForm;
Check the codesandbox link here
add a comment |
Find the below code.
import React from "react";
import { Form, Field } from "react-final-form";
import arrayMutators from "final-form-arrays";
import { FieldArray } from "react-final-form-arrays";
const onSubmit = () => {
console.log("submitted");
};
const validate = () => {
// console.log("validated");
};
const MyForm = () => (
<Form
onSubmit={onSubmit}
mutators={{
// potentially other mutators could be merged here
...arrayMutators
}}
validate={validate}
render={({ handleSubmit, pristine, invalid }) => (
<form onSubmit={handleSubmit}>
<FieldArray name="customers">
{({ fields }) => (
<div>
<button
type="button"
onClick={() =>
fields.push({ firstName: "", lastName: "", isVisible: true })
}
>
Add Customer
</button>
{fields.map((name, index) => (
<div key={name}>
<a
onClick={() =>
(fields.value[index].isVisible = !fields.value[index]
.isVisible)
}
>{`Cust #${index}`}</a>
{fields.value[index].isVisible ? (
<div>
<div>
<Field name={`${name}.firstName`} component="input" />
</div>
<div>
<Field name={`${name}.lastName`} component="input" />
</div>
</div>
) : null}
<button type="button" onClick={() => fields.remove(index)}>
Remove
</button>
</div>
))}
</div>
)}
</FieldArray>
</form>
)}
/>
);
export default MyForm;
Check the codesandbox link here
Find the below code.
import React from "react";
import { Form, Field } from "react-final-form";
import arrayMutators from "final-form-arrays";
import { FieldArray } from "react-final-form-arrays";
const onSubmit = () => {
console.log("submitted");
};
const validate = () => {
// console.log("validated");
};
const MyForm = () => (
<Form
onSubmit={onSubmit}
mutators={{
// potentially other mutators could be merged here
...arrayMutators
}}
validate={validate}
render={({ handleSubmit, pristine, invalid }) => (
<form onSubmit={handleSubmit}>
<FieldArray name="customers">
{({ fields }) => (
<div>
<button
type="button"
onClick={() =>
fields.push({ firstName: "", lastName: "", isVisible: true })
}
>
Add Customer
</button>
{fields.map((name, index) => (
<div key={name}>
<a
onClick={() =>
(fields.value[index].isVisible = !fields.value[index]
.isVisible)
}
>{`Cust #${index}`}</a>
{fields.value[index].isVisible ? (
<div>
<div>
<Field name={`${name}.firstName`} component="input" />
</div>
<div>
<Field name={`${name}.lastName`} component="input" />
</div>
</div>
) : null}
<button type="button" onClick={() => fields.remove(index)}>
Remove
</button>
</div>
))}
</div>
)}
</FieldArray>
</form>
)}
/>
);
export default MyForm;
Check the codesandbox link here
edited Nov 21 at 17:06
answered Nov 21 at 12:35
Praveen
163
163
add a comment |
add a comment |
In the fields array, you can add one more key isVisible.
It will look like this:
fields = [
{
firstName: 'John',
lastName: 'Doe',
isVisible: true,
},
{
firstName: 'Jane',
lastName: 'Doe',
isVisible: false,
}
];
Now while showing, only render the field when isVisible is true,
<FieldArray name="customer">
{({ fields }) => (
fields.map((name, index) => {
if(name.isVisible){
return (
<div key={index}>
<Field name={`${name}.firstName`} />
<Field name={`${name}.lastName`} />
</div>
);
))
)}
</FieldArray>
You can control isVisible key by clicking Cust # button.
Thanks for answer. Isn't it going to leave multiple unused fields on the page?
– Pardeep Dhingra
Nov 21 at 10:32
you can change all the other isVisible to false. For example, when you click on CUST #1, you can make it's isVisible true and all other isVisible false. That way only one will be visible at a time.
– Ratnajeet Shyamkunwar
Nov 21 at 10:53
add a comment |
In the fields array, you can add one more key isVisible.
It will look like this:
fields = [
{
firstName: 'John',
lastName: 'Doe',
isVisible: true,
},
{
firstName: 'Jane',
lastName: 'Doe',
isVisible: false,
}
];
Now while showing, only render the field when isVisible is true,
<FieldArray name="customer">
{({ fields }) => (
fields.map((name, index) => {
if(name.isVisible){
return (
<div key={index}>
<Field name={`${name}.firstName`} />
<Field name={`${name}.lastName`} />
</div>
);
))
)}
</FieldArray>
You can control isVisible key by clicking Cust # button.
Thanks for answer. Isn't it going to leave multiple unused fields on the page?
– Pardeep Dhingra
Nov 21 at 10:32
you can change all the other isVisible to false. For example, when you click on CUST #1, you can make it's isVisible true and all other isVisible false. That way only one will be visible at a time.
– Ratnajeet Shyamkunwar
Nov 21 at 10:53
add a comment |
In the fields array, you can add one more key isVisible.
It will look like this:
fields = [
{
firstName: 'John',
lastName: 'Doe',
isVisible: true,
},
{
firstName: 'Jane',
lastName: 'Doe',
isVisible: false,
}
];
Now while showing, only render the field when isVisible is true,
<FieldArray name="customer">
{({ fields }) => (
fields.map((name, index) => {
if(name.isVisible){
return (
<div key={index}>
<Field name={`${name}.firstName`} />
<Field name={`${name}.lastName`} />
</div>
);
))
)}
</FieldArray>
You can control isVisible key by clicking Cust # button.
In the fields array, you can add one more key isVisible.
It will look like this:
fields = [
{
firstName: 'John',
lastName: 'Doe',
isVisible: true,
},
{
firstName: 'Jane',
lastName: 'Doe',
isVisible: false,
}
];
Now while showing, only render the field when isVisible is true,
<FieldArray name="customer">
{({ fields }) => (
fields.map((name, index) => {
if(name.isVisible){
return (
<div key={index}>
<Field name={`${name}.firstName`} />
<Field name={`${name}.lastName`} />
</div>
);
))
)}
</FieldArray>
You can control isVisible key by clicking Cust # button.
answered Nov 21 at 10:19
Ratnajeet Shyamkunwar
111
111
Thanks for answer. Isn't it going to leave multiple unused fields on the page?
– Pardeep Dhingra
Nov 21 at 10:32
you can change all the other isVisible to false. For example, when you click on CUST #1, you can make it's isVisible true and all other isVisible false. That way only one will be visible at a time.
– Ratnajeet Shyamkunwar
Nov 21 at 10:53
add a comment |
Thanks for answer. Isn't it going to leave multiple unused fields on the page?
– Pardeep Dhingra
Nov 21 at 10:32
you can change all the other isVisible to false. For example, when you click on CUST #1, you can make it's isVisible true and all other isVisible false. That way only one will be visible at a time.
– Ratnajeet Shyamkunwar
Nov 21 at 10:53
Thanks for answer. Isn't it going to leave multiple unused fields on the page?
– Pardeep Dhingra
Nov 21 at 10:32
Thanks for answer. Isn't it going to leave multiple unused fields on the page?
– Pardeep Dhingra
Nov 21 at 10:32
you can change all the other isVisible to false. For example, when you click on CUST #1, you can make it's isVisible true and all other isVisible false. That way only one will be visible at a time.
– Ratnajeet Shyamkunwar
Nov 21 at 10:53
you can change all the other isVisible to false. For example, when you click on CUST #1, you can make it's isVisible true and all other isVisible false. That way only one will be visible at a time.
– Ratnajeet Shyamkunwar
Nov 21 at 10:53
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409104%2fadd-multiple-form-fields-but-show-only-one-at-a-time%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown