【问题标题】:How to have searchable dropdown in Redux-form如何在 Redux-form 中有可搜索的下拉菜单
【发布时间】:2018-01-24 10:19:15
【问题描述】:

我正在使用来自 redux-form 的字段数组,我需要在我的表单中提供一个可搜索的下拉菜单。我知道正常的下拉菜单可以这样完成,

<Field name={`${object}.method`} component="select" validate={required} id={`${object}.key`}>
       <option value="id">id</option>
       <option value="css">css</option>
       <option value="xpath">xpath</option>
       <option value="binding">binding</option>
       <option value="model">model</option>
</Field>

但这不是可搜索的下拉列表。即使我要使用它,这也只是提供一个基本的 html 选择,我如何能够将 css 样式应用于它以使其与其他引导元素匹配?

为了有一个可搜索的下拉列表,我使用react-select 包。我试图做的是;

<Field
    name={`${object}.method`}
    type='text'
    component={this.renderDropdown}
    label="Method"
    validate={required}
    id={`${object}.key`}
    valueField='value'
/>

renderDropdown = ({ input, id, valueField, meta: { touched, error }, ...props }) => {
    return (
        <React.Fragment>
            <div className='align-inline object-field-length'>
                <Select {...input} id={id} value={input.value.value} onChange={input.onChange}
                    options={[
                        { value: 'id', label: 'id' },
                        { value: 'css', label: 'css' },
                        { value: 'xpath', label: 'xpath' },
                        { value: 'binding', label: 'binding' },
                        { value: 'model', label: 'model' },
                    ]}
                />
            </div>
        </React.Fragment>
    )
};

这不能正常工作,只有当下拉菜单处于活动状态时,值才会存储在 redux-store 中(当它被点击时),在事件模糊时会丢失该值。 p>

我在这里做错了什么?

是否可以有 redux 形式的可搜索下拉列表?

【问题讨论】:

标签: javascript reactjs react-redux redux-form react-select


【解决方案1】:

回答我自己的问题,最后我发现我不能将 react-select 包与 redux-form 一起使用。

When the value is being selected it gets added to the react-store but loses the value on event blur.发生这种情况是因为 redux-form onBlur 事件触发了一个动作,该动作在其有效负载中传递了 null。为避免这种情况,github issue thread

中提到了几个解决方案

通过定义一个自定义方法来处理 onBlur 为我做到了;

renderDropdown = ({ input, onBlur, id, meta: { touched, error }, ...props }) => {

 const handleBlur = e => e.preventDefault();

    return (
        <React.Fragment>
            <Select {...input}
                id="object__dropdown--width"
                options={allRepos}
                onChange={input.onChange}
                onBlur={handleBlur} 
            />
        </React.Fragment>
    )
}



  <Field name="listAllRepo" 
      value="this.state.accountno" 
      component={this.renderDropdown} 
      placeholder="Select" 
  />

希望这对其他人有帮助!

【讨论】:

  • 要是我能在 4 天前找到这个问题就好了。谢谢iit女孩。
【解决方案2】:

有 react-select 包提供可搜索的功能

<Select
        name="form-field-name"
        value={value}
        onChange={this.handleChange}
        options={[
          { value: 'one', label: 'One' },
          { value: 'two', label: 'Two' },
        ]}
        searchable={true}
      />

关注github

【讨论】:

  • 感谢您的回答,但我已经在使用 react-select 包。我的问题是如何在 redux-form 中使用 react-select。
  • 在 redux 表单中可以调度诸如更改、提交等操作。在 onChange 中,您必须对相关数据的更改触发操作。
  • 是的,我知道这一点。但我不确定如何使用 fieldarrays 以 redux 形式更新状态。由于我没有直接与状态交互,所以到目前为止 fieldarray 正在弹出并向状态添加项目。至少到目前为止我的理解是这样的
猜你喜欢
  • 2019-04-04
  • 1970-01-01
  • 2017-01-24
  • 1970-01-01
  • 2020-06-23
  • 1970-01-01
  • 1970-01-01
  • 2020-10-28
  • 1970-01-01
相关资源
最近更新 更多