Niv*_*gam 3 javascript reactjs react-select redux-form react-redux
我正在使用 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>
Run Code Online (Sandbox Code Playgroud)
但这不是可搜索的下拉列表。即使我要使用它,这也只是提供了一个基本的 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>
)
};
Run Code Online (Sandbox Code Playgroud)
这无法正常工作,仅当下拉列表处于活动状态(单击时)时,值才会存储在 redux-store 中,在事件模糊时,这会丢失该值。
我在这里做错了什么?
是否可以在 redux 形式中有可搜索的下拉列表?
回答我自己的问题,最后我想我不能使用带有 redux-form 的 react-select 包。
当该值被选择时,它会被添加到反应存储中,但在事件模糊时会丢失该值。发生这种情况是因为 redux-form onBlur 事件触发了一个动作,该动作在其有效负载中传递了 null。为了避免这种情况,github问题线程中提到了几个解决方案
通过定义一个自定义方法来处理 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"
/>
Run Code Online (Sandbox Code Playgroud)
希望这对其他人有帮助!
| 归档时间: |
|
| 查看次数: |
2549 次 |
| 最近记录: |