react formik如何从复选框组件传递值和ID

发布于 2025-01-22 04:31:36 字数 2419 浏览 0 评论 0原文

这是我的复选框组件:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

有深☉意 2025-01-29 04:31:36

是的,您可以尝试以下内容:

更改&lt; input&gt;的“值”属性;标记为:
value = {{“ {

”您可以执行以下操作的值:

let result = JSON.parse(value);

返回的对象将具有以下形状:

result = {
  id: option.key,
  name: option.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:

let result = JSON.parse(value);

The returned object will be of the following shape:

result = {
  id: option.key,
  name: option.value,
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文