reactjs - 如何使用带有反应最终形式的自定义 radio 组件?

我正在尝试使用带有 React-final-form 的自定义 Radio 组件,但它不是用作单选按钮而是用作复选框,即所有按钮都打开以供选择。

第 3 方单选按钮具有以下架构:

checked boolean     
Whether or not radio is checked

onChange    () => void      
Called when the user attempts to change the checked state

name    string      
The input name, used to reference the element in JavaScript

我为使用 Radio 组件创建了一个自定义组件:

const CustomRadio = (props: any) => (
    <Radio
        {...props.input}
        {...props.rest}
        name={props.name}
        onChange={() => props.input.onChange()}
    />
)

我按如下方式使用它:

<Field name="food"
component={CustomRadio}
value="1"
/>
<Field name="food"
component={CustomRadio}
value="2"
/>

作为 RFF 的新手和 React 的新手,我可能会做一些非常错误的事情,因此我们将不胜感激。

基本上,我想将 RFF 与我的第 3 方组件一起使用。尽管我已经成功地将我的输入组件与 RFF 一起使用,正如预期的那样,单选按钮是造成问题的原因。

最佳答案

这是使用 react-final-form 的 Radio 的正确实现:-

https://codesandbox.io/s/vibrant-easley-5n1ek?file=/index.js

/* eslint-disable jsx-a11y/accessible-emoji */
import React from "react";
import { render } from "react-dom";
import Styles from "./Styles";
import { Form, Field } from "react-final-form";
import RadioGroup from "@material-ui/core/RadioGroup";
import FormControlLabel from "@material-ui/core/FormControlLabel";
import FormControl from "@material-ui/core/FormControl";
import Radio from "@material-ui/core/Radio";
import FormLabel from "@material-ui/core/FormLabel";


const RadioWrapper = (props) => {
  const {
    input: { checked, value, name, onChange, ...restInput },
  } = props;

  return (
    <Radio
      name={name}
      inputProps={restInput}
      onChange={onChange}
      checked={checked}
      value={value}
    />
  );
};

const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

const onSubmit = async (values) => {
  await sleep(300);
  window.alert(JSON.stringify(values, 0, 2));
};

const App = () => {
  return (
    <Styles>
      <h1>React Final Form - Simple Example</h1>
      <a
        href="https://final-form.org/react"
        target="_blank"
        rel="noopener noreferrer"
      >
        Read Docs
      </a>
      <Form
        onSubmit={onSubmit}
        initialValues={{
          employed: false,
          all_sub_tenants: "true"
        }}
        render={({ handleSubmit, form, submitting, pristine, values }) => (
          <form onSubmit={handleSubmit}>
            <FormControl component="fieldset">
              <FormLabel component="legend">
                Select Tenants
              </FormLabel>
              <RadioGroup aria-label="allSubTenants" name="allSubTenants">
                <FormControlLabel
                  value="true"
                  control={
                    <Field
                      name="all_sub_tenants"
                      component={RadioWrapper}
                      type="radio"
                      value={"true"}
                    />
                  }
                  label="All Sub-Tenants"
                />
                <FormControlLabel
                  value="false"
                  control={
                    <Field
                      name="all_sub_tenants"
                      component={RadioWrapper}
                      type="radio"
                      value={"false"}
                    />
                  }
                  label="Select Sub-Tenants"
                />
              </RadioGroup>
            </FormControl>
            
            <div>
              <label>Notes</label>
              <Field name="notes" component="textarea" placeholder="Notes" />
            </div>
            <div className="buttons">
              <button type="submit" disabled={submitting || pristine}>
                Submit
              </button>
              <button
                type="button"
                onClick={form.reset}
                disabled={submitting || pristine}
              >
                Reset
              </button>
            </div>
            <pre>{JSON.stringify(values, 0, 2)}</pre>
          </form>
        )}
      />
    </Styles>
  );
};

render(<App />, document.getElementById("root"));

https://stackoverflow.com/questions/58099124/

相关文章:

visual-studio-code - 如何使用 Visual Studio Code + VSC

python - Airflow 外部任务传感器卡​​住

azure - 作为 Azure DevOps 发布管道的一部分从应​​用服务中删除文件

javascript - 从 JSON 响应中删除 '@' 作为 String() 后,在 WSO2

ruby-on-rails - 仅导入主文件时,SASS 变量不起作用

react-native - 在 native react 中使用相机捕获方框内的区域

jwt - 将 on_premise_sam_account 属性添加到 Azure 广告的 JWT

java - 尝试通过运行 `mvn test` 来运行测试时 JVM 崩溃

php - 如何在 Wordpress REST API 中创建新的 rest url 前缀?

php - 未定义的方法 Laravel\Lumen\Application::booted()