我使用的是redux-form 7.0.4,它的工作效率高于以下问题.
新用户可以输入详细信息,现有用户可以在同一表单上编辑详细信息。当现有用户编辑详细信息并从表单字段中删除数据时,我有问题。我想添加check onBlur。如果用户空表单字段,并且有模糊事件,我需要填充内部的初始数据。
如果字段为空,则在blur上还原初始/服务器值。
表单字段:
<Field
component={InputField}
type='text'
name='registeredName'
className='form-control'
required
placeholder='Registered Business Name*'
/>InputField组件:
const InputField = ({
input,
type,
placeholder,
meta: { touched, error, initial }
}) => {
const onChange = (e) => {
const val = e.target.value;
if (type === 'phoneNumber') {
if (!/^\d+$/.test(val) || String(val).length > 10) {
return;
}
}
input.onChange(e);
};
return (
<div className='form-group'>
<input
{...input}
onChange={onChange}
type={type}
className='form-control'
placeholder={placeholder}
value={input.value }
onBlur={e => {input.value = (!e.target.value) ? initial : e.target.value;}}
/>
{touched &&
((error &&
<div className='error text-danger'>
{error}
</div>
))}
</div>
);
};
/* eslint-enable */
export default InputField;发布于 2022-10-24 08:40:38
您需要在字段组件中设置onChange和onBlur,如下所示:
<Field
component={InputField}
type='text'
name='registeredName'
className='form-control'
required
placeholder='Registered Business Name*'
onChange={(event) => {
...your onChange function here...
}}
onBlur={(event) => {
...Your onBlur function here...
}}
/>我建议您很好地阅读医生们中的<Field />组件。
希望这会有帮助
https://stackoverflow.com/questions/47412235
复制相似问题