Add multiple form fields but show only one at a time











up vote
0
down vote

favorite












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:



enter image description here



I need it to look like this:
enter image description here










share|improve this question




























    up vote
    0
    down vote

    favorite












    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:



    enter image description here



    I need it to look like this:
    enter image description here










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      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:



      enter image description here



      I need it to look like this:
      enter image description here










      share|improve this question















      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:



      enter image description here



      I need it to look like this:
      enter image description here







      reactjs react-final-form react-final-form-arrays






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited yesterday

























      asked yesterday









      Pardeep Dhingra

      3,06662243




      3,06662243
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote













          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.






          share|improve this answer








          New contributor




          Ratnajeet Shyamkunwar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















          • Thanks for answer. Isn't it going to leave multiple unused fields on the page?
            – Pardeep Dhingra
            yesterday










          • 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
            yesterday


















          up vote
          0
          down vote













          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






          share|improve this answer










          New contributor




          Praveen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















            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',
            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
            });


            }
            });














             

            draft saved


            draft discarded


















            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








            up vote
            0
            down vote













            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.






            share|improve this answer








            New contributor




            Ratnajeet Shyamkunwar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.


















            • Thanks for answer. Isn't it going to leave multiple unused fields on the page?
              – Pardeep Dhingra
              yesterday










            • 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
              yesterday















            up vote
            0
            down vote













            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.






            share|improve this answer








            New contributor




            Ratnajeet Shyamkunwar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.


















            • Thanks for answer. Isn't it going to leave multiple unused fields on the page?
              – Pardeep Dhingra
              yesterday










            • 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
              yesterday













            up vote
            0
            down vote










            up vote
            0
            down vote









            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.






            share|improve this answer








            New contributor




            Ratnajeet Shyamkunwar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            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.







            share|improve this answer








            New contributor




            Ratnajeet Shyamkunwar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            share|improve this answer



            share|improve this answer






            New contributor




            Ratnajeet Shyamkunwar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            answered yesterday









            Ratnajeet Shyamkunwar

            1




            1




            New contributor




            Ratnajeet Shyamkunwar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.





            New contributor





            Ratnajeet Shyamkunwar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






            Ratnajeet Shyamkunwar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.












            • Thanks for answer. Isn't it going to leave multiple unused fields on the page?
              – Pardeep Dhingra
              yesterday










            • 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
              yesterday


















            • Thanks for answer. Isn't it going to leave multiple unused fields on the page?
              – Pardeep Dhingra
              yesterday










            • 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
              yesterday
















            Thanks for answer. Isn't it going to leave multiple unused fields on the page?
            – Pardeep Dhingra
            yesterday




            Thanks for answer. Isn't it going to leave multiple unused fields on the page?
            – Pardeep Dhingra
            yesterday












            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
            yesterday




            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
            yesterday












            up vote
            0
            down vote













            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






            share|improve this answer










            New contributor




            Praveen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






















              up vote
              0
              down vote













              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






              share|improve this answer










              New contributor




              Praveen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.




















                up vote
                0
                down vote










                up vote
                0
                down vote









                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






                share|improve this answer










                New contributor




                Praveen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                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







                share|improve this answer










                New contributor




                Praveen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                share|improve this answer



                share|improve this answer








                edited yesterday





















                New contributor




                Praveen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                answered yesterday









                Praveen

                13




                13




                New contributor




                Praveen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.





                New contributor





                Praveen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






                Praveen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    Contact image not getting when fetch all contact list from iPhone by CNContact

                    count number of partitions of a set with n elements into k subsets

                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks