我尝试了不同的方法来实现mutator,以便用户在文本字段中输入文本后立即将文本转换为大写。但还没有成功。这个
example
埃里克提供的似乎是正确的,但也许我遗漏了一些东西。
/** Converts a form value to uppercase **/
const upper = ([name], state, { changeValue }) => {
changeValue(state, name, (value) => value.toUpperCase())
}
<Form
onSubmit={onSubmit}
mutators={{
...arrayMutators,
upper
}}
render={({
handleSubmit,
mutators: { push, pop, upper }, // injected from final-form-arrays above
pristine,
reset,
submitting,
values
}) => {
return (
<form onSubmit={handleSubmit}>
<div>
<label>Company</label>
<Field name="company" component="input" onChange={(e) => upper('company')} />
</div>
</form>
)}
}
/>
toUpperCase
按价值计算
value
更新日期:2018年10月27日
我意识到我们也可以使用解析器来解决这个问题:
upper = value => {
return value ? value.toUpperCase() : ''
}
<Field name="company" component="input" parse={this.upper} />
仍然在寻找mutator是如何工作的,我的代码中有什么错误。
已经尝试过:
Mutate state based on existing fields
How to set/change Field value from external user action 🏁 React Final Form