【发布时间】:2018-01-31 21:39:34
【问题描述】:
我正在处理一个 redux-form 字段数组。我想摆脱任何和字段按钮,并让下一个字段生成 onkeyPress 'enter'。我们将使用扫描仪输入字段,我可以在扫描的返回中编码以呈现下一个字段。 Redux 表单默认输入太'提交'。我已经阅读了几篇关于禁用它的在线文章。我已经关注了他们,但仍然有相同的“输入”键要提交的问题。我对这方面的任何想法持开放态度。
class AssignDeviceForm extends Component {
constructor(props) {
super(props);
FieldArraysForm = props => {
const { handleSubmit, pristine, reset, submitting } = this.props;
return (
<div>
<Well>
<form onSubmit={handleSubmit(() => {})}>
<FieldArray name="IMEIs" component={this.renderIMEI}/>
<div>
<button type="submit" disabled={submitting}>Submit</button>
<button type="button" disabled={pristine || submitting} onClick={reset}>
Clear Values
</button>
</div>
</form>
</Well>
</div>
);
};
renderIMEI = ({ fields, meta: { touched, error, submitFailed } }) => {
if (fields.length == 0) {
fields.unshift({});
}
return(
<ul>
<h3>Scan the devices you want to assign</h3>
{fields.map((IMEI, index) => (
<li key={index}>
<button
type="button"
title="Remove Device"
onClick={() => fields.remove(index)}
/>
<h4>Device #{index + 1}</h4>
<Field
name={`${IMEI}.txtIMEI`}
type="text"
component={this.renderField}
label="IMEI"
onKeyPress={e => {
if (e.key === 'Enter')
e.preventDefault()
console.log('WAHOO!!!')
{this.renderIMEI}
}}
/>
</li>
))}
</ul>
)};
renderField = ({ input, label, type, meta: { touched, error, active } }) => (
<div>
<label>{label}</label>
<div>
<input {...input} type={type} placeholder={label} />
{active && touched && error && <span>{error}</span>}
</div>
</div>
);
render(){
return(
<div>
{this.FieldArraysForm()}
</div>
)
}
}
【问题讨论】:
-
我找到了如何在不添加 type="button" stackoverflow.com/a/51982595/1648498 的情况下实现
标签: reactjs redux redux-form