在 React Final Form 中,我们可以使用 format
或 parse
Prop text
键入输入以存储与指定值不同的值,但这似乎与复选框的工作方式不同。
例子:
<FieldArray name='test' initialValue={[true, false]}>
<Field
component='input'
type='checkbox'
format={value => value ? 'foo' : null}
parse={value => value ? 'foo' : null}/>
</Field>
</FieldArray>
在这个例子中,要存储的值仍然是
true
或 false
,不管使用format
和 parse
.是否可以格式化来自 [true, false]
的值?至 ["foo"]
?在此先感谢您的帮助。
最佳答案
您的 format
函数需要将其转换回 bool 值。
<Field
name="employed"
component="input"
type="checkbox"
format={v => v === "foo"}
parse={v => (v ? "foo" : null)}
/>
但听起来您想要使用复选框来管理数组中的成员资格的内置功能。为此,您只需指定
value
支持 <Field/>
.请参阅“酱汁”示例:☝️ 还包括
format
/parse
例子。
关于reactjs - 如何使用 React Final Form Field Array 格式化复选框字段的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59832895/