11

i am developing a form in reactjs using formik plugin plugin link. when i submit form i am getting text fields values but dropdown values are coming empty...

this is my dropdown select

<div className="form-group">                  
                <Field component="select" id="category" name="category" value={this.state.value} className={"form-control"} onChange={ this.handleChange }>
                   <option value="lokaler">Lokaler</option>
                   <option value="jobb">Jobb</option>
                   <option value="saker-ting">Saker & ting</option>
                   <option value="evenemang">Evenemang</option>
                </Field>
                  </div>

handle submit function

export default withFormik({
  enableReinitialize: true,
  mapPropsToValues({ category }) {
    return {      
      category: category || ''
    }
  },
    handleSubmit(values, { setStatus, setErrors }){
      console.log("data is this: "); 
      console.log(values); //here i am getting all form fields values except select value returning empty value
      console.log("category: "+values.category);// here i always get empty value but not getting selected value

      }

i am getting all text fields values in handle submit function but i am not able to get dropdown selected value....kindly tell me how to get dropdown select value in handle submit function ?

4

2 回答 2

12

我昨天也遇到了这个问题。我的问题是提交包含 ant design 下拉列表的表单。我终于在几个小时后让它工作:

文档说您需要将onChangeonBlur事件分别设置为setFieldValuesetFieldTouched道具,例如第 3 方输入示例:

<MySelect
    value={values.topics}
    onChange={setFieldValue}
    onBlur={setFieldTouched}
    error={errors.topics}
    touched={touched.topics}
/>

所以对于我的问题,我需要改变一下:

<Select
  {...field}
  onChange={(value) => setFieldValue('fruitName', value)}
  onBlur={()=> setFieldTouched('fruitName', true)}
  value={values.fruitName}
  ...
>
...
</Select>

希望这会有所帮助。

这是我的CodeSandbox

于 2018-06-25T17:43:03.657 回答
3

在使用 Formik 时处理选择组件的一种更强大的方法是同时使用 Jed Watsons react-select组件。这两者很好地协同工作,并抽象出许多您通常需要实现的样板,以使组件与 Formik 无缝工作。

我已经从Jared Palmer 的 react-select / Formik 示例中分叉了一个简单的示例,并对其进行了调整以反映您上面的代码。

import "./formik-demo.css";
import React from "react";
import { render } from "react-dom";
import { withFormik } from "formik";
import Select from "react-select";
import "react-select/dist/react-select.css";

const options = [
  { value: "lokaler", label: "Lokaler" },
  { value: "jobb", label: "Jobb" },
  { value: "saker-ting", label: "Saker & ting" },
  { value: "evenemang", label: "Evenemang" }
];

const MyForm = props => {
  const {
    values,
    handleChange,
    handleBlur,
    handleSubmit,
    setFieldValue
  } = props;
  return (
    <form onSubmit={handleSubmit}>
      <label htmlFor="myText" style={{ display: "block" }}>
        My Text Field
      </label>
      <input
        id="myText"
        placeholder="Enter some text"
        value={values.myText}
        onChange={handleChange}
        onBlur={handleBlur}
      />
      <MySelect value={values.option} onChange={setFieldValue} />
      <button type="submit">Submit</button>
    </form>
  );
};

class MySelect extends React.Component {
  handleChange = value => {
    // this is going to call setFieldValue and manually update values.topcis
    this.props.onChange("option", value);
  };
  render() {
    return (
      <div style={{ margin: "1rem 0" }}>
        <label htmlFor="color">Select an Option </label>
        <Select
          id="color"
          options={options}
          onChange={this.handleChange}
          value={this.props.value}
        />
      </div>
    );
  }
}

const MyEnhancedForm = withFormik({
  mapPropsToValues: props => ({
    myText: "",
    option: {}
  }),
  handleSubmit: (values, { setSubmitting }) => {
    console.log(values);
  }
})(MyForm);

const App = () => <MyEnhancedForm />;
render(<App />, document.getElementById("root"));

有一些陷阱,您必须包含 react select css 组件才能正确显示

import "react-select/dist/react-select.css";

函数 mapPropsToValues 选项字段应初始化为对象

 mapPropsToValues: props => ({
    myText: "",
    option: {}

最后这里是codeandbox示例的链接

编辑 31w93yvv45

于 2018-05-18T09:51:39.013 回答