【问题标题】:How to preserve values of each toggle field in Formik?如何保留 Formik 中每个切换字段的值?
【发布时间】:2018-08-10 10:16:00
【问题描述】:


我正在使用 Formik 创建一个具有下拉和提交按钮的表单。单击下拉列表中的任何项目后,我会呈现一个切换选项。我面临的问题是,如果我有两个切换,第一个是关闭的,第二个是打开的。现在,如果我删除第一个,第二个的值也会更改为第一个的值。我如何解决它。任何帮助表示赞赏。 这是我的代码的链接 -
https://codesandbox.io/s/kw3w0xlqjv

【问题讨论】:

    标签: reactjs formik


    【解决方案1】:

    问题在于 key 属性。正确的解决方案-

    import React from "react";
    import { render } from "react-dom";
    import ReactDOM from "react-dom";
    import { Dropdown, DropdownItem } from "carbon-components-react";
    import { Formik, Field, Form, FieldArray } from "formik";
    const initialValues = {
      attributes: []
    };
    const App = () => (
      <div>
        <Formik
          initialValues={initialValues}
          onSubmit={values => console.log(values)}
          render={({ values }) => {
            return (
              <Form>
                <FieldArray
                  name="attributes"
                  render={({ remove, push }) => (
                    <div>
                      {values.attributes.length > 0 &&
                        values.attributes.map((eachAttribute, index) => (
                          <div className="row" key={eachAttribute.attributeName}>
                            <div className="col">
                              <label>{eachAttribute.attributeName}</label>
                              <Field
                                name={`attributes.${index}.values.in.${0}`}
                                type="checkbox"
                                id={`boolenField.${index}`}
                                className="bx--toggle"
                              />
                              <label
                                className="bx--toggle__label"
                                for={`boolenField.${index}`}
                              >
                                <span className="bx--toggle__text--left">Off</span>
                                <span className="bx--toggle__appearance" />
                                <span className="bx--toggle__text--right">On</span>
                              </label>
                              {/*<Field
                                name={`attributes.${index}.values.in.${0}`}
                                placeholder="attribute value"
                                type="text"
                              />*/}
                            </div>
                            <div className="col">
                              <button
                                type="button"
                                className="secondary"
                                onClick={() => remove(index)}
                              >
                                X
                              </button>
                            </div>
                          </div>
                        ))}
                      <Dropdown
                        className="some-class"
                        ariaLabel="dropdown menu label"
                        onChange={e =>
                          push({ attributeName: e.value, values: { in: [] } })
                        }
                        defaultText="Pick attribute"
                      >
                        <DropdownItem itemText="Option 1" value="option1" />
                        <DropdownItem itemText="Option 2" value="option2" />
                        <DropdownItem itemText="Option 3" value="option3" />
                        <DropdownItem itemText="Option 4" value="option4" />
                        <DropdownItem itemText="Option 5" value="option5" />
                      </Dropdown>
    
                      {/*<button
                        type="button"
                        className="secondary"
                        onClick={() =>
                          push({ attributeName: "", values: { in: [] } })
                        }
                      >
                        Add attribute
                      </button>*/}
                    </div>
                  )}
                />
                <button type="submit">Submit</button>
              </Form>
            );
          }}
        />
      </div>
    );
    render(<App />, document.getElementById("root"));
    

    谢谢里卡多·布兰比拉。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-19
      • 2014-06-15
      • 2020-08-04
      • 1970-01-01
      相关资源
      最近更新 更多