Reactjs+蚂蚁设计:连接两个ANTD组件

发布于 2025-01-18 01:14:49 字数 1302 浏览 2 评论 0 原文

我是一个新手,我需要一个问题,我需要从Ant Design中的Ant Design中连接两个组件(滑块和无线电按钮),并将它们合作在一起。因此,如果我选择“单选”按钮的选项之一,我希望滑块可以调整选择并相反。例如,如果我在第2022号广播按钮上选择,我希望滑块向右移动,例如,如果我在滑块上选择2022,我想检查单个单选按钮值2022,依此类推...试图将它们置于某些条件下,但真的不知道该怎么做。并具有与滑块相同宽度的无线电按钮。

import React from "react";
import { Radio, Slider } from "antd";

const App = () => {
  const [currentValueRadio, setcurrentValueRadio] = React.useState(1);
  const [currentValue, setCurrentValue] = React.useState();

  return (
    <>
      <Radio.Group
        onChange={(e) => {
          console.log("radio checked", e.target.value);
          setcurrentValueRadio(e.target.value);
        }}
        value={currentValueRadio}
      >
        <Radio value={1}>2021</Radio>
        <Radio value={2}>2022</Radio>
      </Radio.Group>
      <Slider
        defaultValue={2021}
        min={2021}
        max={2022}
        style={{ width: 240 }}
        onChange={(value) => {
          console.log(currentValue);
          setCurrentValue(value);
        }}
      />
    </>
  );
};

I am a newbie and I have an issue that I need to connect two components(slider and radio buttons) from Ant design in React and make them collaborate together. So if I choose one of the options of the radio button, I want the slider to adjust the choice and conversely. For example, if I choose on the radio button year 2022 I want the to slider move to the right, and for example, if I choose 2022 on the slider I want to have checked radio button value 2022, and so on... I tried to put them into some conditions, but really don't know what to do with them. And also to have radio buttons in same width as slider.

import React from "react";
import { Radio, Slider } from "antd";

const App = () => {
  const [currentValueRadio, setcurrentValueRadio] = React.useState(1);
  const [currentValue, setCurrentValue] = React.useState();

  return (
    <>
      <Radio.Group
        onChange={(e) => {
          console.log("radio checked", e.target.value);
          setcurrentValueRadio(e.target.value);
        }}
        value={currentValueRadio}
      >
        <Radio value={1}>2021</Radio>
        <Radio value={2}>2022</Radio>
      </Radio.Group>
      <Slider
        defaultValue={2021}
        min={2021}
        max={2022}
        style={{ width: 240 }}
        onChange={(value) => {
          console.log(currentValue);
          setCurrentValue(value);
        }}
      />
    </>
  );
};

https://codesandbox.io/embed/radio-group-antd-4-19-4-forked-134cps?fontsize=14&hidenavigation=1&theme=dark

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

内心激荡 2025-01-25 01:14:49

您需要将被控制为“值”支柱的状态添加到两个组件中,以使它们合并。

<Slider
  value={currentValueRadio} // <- add a value prop
  defaultValue={2021}
  ...

在您的示例中添加值支柱将使两个组件都对相同的状态值做出反应。
但是,由于无线电组件将值设置为1或2,并且滑块上的范围是2021年至2022年,因此您可能看不到所需的结果。也许将无线电值分别更改为2021和2022。

You need to add the state being controlled as a 'value' prop to both components, to have them syncronised.

<Slider
  value={currentValueRadio} // <- add a value prop
  defaultValue={2021}
  ...

Adding the value prop in your example will have both components react to the same state value.
However, since the Radio components set the value to 1 or 2, and the range on the Slider is from 2021 to 2022, you may not see the result you're looking for. Perhaps change the Radio values to 2021 and 2022 respectively.

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