为什么这个Redux表单初始化不能与react-select Select一起使用?

时间:2019-05-09 09:42:07

标签: reactjs redux multi-select react-select

我正在处理redux /不变形式。该表单包含一个多选字段。对于选择字段,我使用了react-select并将选择字段作为组件传递给redux / immutable Field。选择其工作精细并在redux存储中传递所选的多个值时。但是在初始化表单时,除该多选择字段外,所有其他字段均有效。我已经为初始化表单设置了一些演示值。选择字段仅显示空白叉号(x),而不显示标签。

初始值从此处传递

const initialValues = {
  name: 'saha',
  targets: [{ label: 'saha', value: 8 }],
};

class Groups extends Component {
  render() {
    return (
          <NewGroup initialValues={initialValues} />
    );
  }
}

表单(NewGroup.js)的代码

class NewGroup extends Component {
render() {
    return (

          <form onSubmit={handleSubmit(onFormSubmit)}>
            <FormInput name="name" label="Name" />
            <FormSelect name="targets" label="Target" options={targets}/>
          </form>

    );
  }
}

export default compose(
  reduxForm({
    form: 'groupForm',
    enableReinitialize: true,
  })
)(NewGroup);

“选择”字段(FormSelect.js)的代码

  renderField = field => {
    const { input, meta, options } = field;
    const reactSelectOptions = options.map(option => ({
      value: option.get('id'),
      label: option.get('name'),
    }));

    return (
       <Select
            {...input}
            classNamePrefix="multivalue"
            closeMenuOnSelect={false}
            components={makeAnimated()}
            onChange={value => input.onChange(value)}
            onBlur={() => input.onBlur(input.value)}
            options={reactSelectOptions}
            styles={customStyle}
            isMulti
          />
    );
  };

render() {
    const { name, label, require, options } = this.props;
    return (

              <Field
                name={name}
                component={this.renderField}
                label={label}
                options={options}
                validate={require ? [required] : []}
              />
    );
  }

预期:Expected

结果:Outcome

0 个答案:

没有答案