react formik如何从复选框组件传递值和ID
这是我的复选框组件:
function CheckBoxesControl(props) {
const { label, name, options, ...rest } = props;
return (
<div className='checkbox-control'>
<span className='checkbox-title'>{label}</span>
<div className='check-elements'>
<Field name={name}>
{({ field, form }) => {
return options.map((option) => {
return (
<React.Fragment key={option.key}>
<input
type='checkbox'
id={option.key}
{...field}
{...rest}
value={option.value}
checked={field.value.includes(option.value)}
/>
<label htmlFor={option.value}>{option.value}</label>
</React.Fragment>
);
});
}}
</Field>
</div>
<ErrorMessage component={TextError} name={name} />
</div>
);
}
export default CheckBoxesControl;
formikcontrol:
import React from 'react';
import CheckBoxesControl from './checkboxescontrol';
import RadioButtons from './radiobuttons';
function FormikControl(props) {
const { control, ...rest } = props;
switch (control) {
case 'radio':
return <RadioButtons {...rest} />;
case 'checkbox':
return <CheckBoxesControl {...rest} />;
default:
return null;
}
}
export default FormikControl;
parent consement:
<Formik
initialValues={formValues || initialValues}
validationSchema={validationSchema}
validateOnMount
onSubmit={onSubmit}
enableReinitialize
>
{(formikProps) => {
return (
<Form>
<FormikControl
control='checkbox'
label='Some label'
name='somearray'
options={somearray}
/>
</Form>
</Formik>
somearray-这是我从firestore获得的数组,看起来像这样: [{key:'',value:''}]
我是一个初学者,所以请忍受我:
目前,我所能获得的是option.value或option.key
我想要的是从Checked复选框发送到Formik的值 [id:option.id,name:option.value]
可能吗?
Here is my checkbox component:
function CheckBoxesControl(props) {
const { label, name, options, ...rest } = props;
return (
<div className='checkbox-control'>
<span className='checkbox-title'>{label}</span>
<div className='check-elements'>
<Field name={name}>
{({ field, form }) => {
return options.map((option) => {
return (
<React.Fragment key={option.key}>
<input
type='checkbox'
id={option.key}
{...field}
{...rest}
value={option.value}
checked={field.value.includes(option.value)}
/>
<label htmlFor={option.value}>{option.value}</label>
</React.Fragment>
);
});
}}
</Field>
</div>
<ErrorMessage component={TextError} name={name} />
</div>
);
}
export default CheckBoxesControl;
Code for FormikControl:
import React from 'react';
import CheckBoxesControl from './checkboxescontrol';
import RadioButtons from './radiobuttons';
function FormikControl(props) {
const { control, ...rest } = props;
switch (control) {
case 'radio':
return <RadioButtons {...rest} />;
case 'checkbox':
return <CheckBoxesControl {...rest} />;
default:
return null;
}
}
export default FormikControl;
And parent component:
<Formik
initialValues={formValues || initialValues}
validationSchema={validationSchema}
validateOnMount
onSubmit={onSubmit}
enableReinitialize
>
{(formikProps) => {
return (
<Form>
<FormikControl
control='checkbox'
label='Some label'
name='somearray'
options={somearray}
/>
</Form>
</Formik>
somearray- it is an array that I get from firestore that looks like this:
[{key:'', value:''}]
I am a beginner, so please bear with me:
Currently all I can get is either option.value or option.key
What I want is the value from checked checkbox send to formik to look like this:
[id: option.id, name: option.value]
Is it possible ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您可以尝试以下内容:
更改&lt; input&gt;的“值”属性;标记为:
value = {{“ {
”您可以执行以下操作的值:
返回的对象将具有以下形状:
Yes, you can try the following:
Change the "value" attribute of the <input> tag to:
value={`{"id": "${option.key}", "name": "${option.value}"}`}
What you have above is a JSON encoded string so in order to get back the individual values you can do the following:
The returned object will be of the following shape: