简体   繁体   English

如何在蚂蚁设计上切换单选按钮颜色 - 反应?

[英]How can i switch Radio Button color on ant design - react?

I am using a library called Ant Design that uses a color theme throughout the interface but the radio button component does not have the property to change it easily, my goal is to switch the color of the radio button when pressing the trash button.我正在使用一个名为 Ant Design 的库,它在整个界面中使用颜色主题,但单选按钮组件没有轻松更改它的属性,我的目标是在按下垃圾箱按钮时切换单选按钮的颜色。

from this -> trash button not selected to this trash button selected从这个 ->垃圾按钮没有被选中到这个垃圾按钮被选中

the method I use here is to change the component's css with !important as follows:我在这里使用的方法是使用 !important 更改组件的 css,如下所示:

.ant-radio-button-checked,
.ant-radio-button-inner,
.ant-radio-button-inner,
.ant-radio-button-wrapper-checked,
.ant-radio-button-input:focus,
.ant-radio-button-inner {
  border-color: #ff4d4f !important;
  color: #ff4d4f !important;
}

.ant-radio:hover,
.ant-radio-button-wrapper:hover {
  color: #ff4d4f;
}

but I can't go back this action但我不能回去这个动作

my react component is as follows:我的反应组件如下:

import { React, useState } from 'react';
import { Radio, Button } from 'antd';
import ShopSketch from './ShopSketch';


const Example = () => {
  const [selectedShop, setSelectedShop] = useState(false);
  const [deleteShop, setDeleteShop] = useState(false);
  const [shopNames, setShopNames] = useState(['shop1', 'shop2', 'shop3']);

  return (
    <div>
      <Button danger onClick={() => setDeleteShop(!deleteShop)}>
        <ion-icon
          style={{ color: 'red', fontSize: '18px' }}
          name={deleteShop ? 'close-outline' : 'trash-outline'}
        ></ion-icon>
      </Button>
      <Radio.Group
        className="shops"
        onChange={(e) => {
          const selected = e.target.value;
          setSelectedShop(selected);
        }}
      >
        {shopNames.map((name, index) => (
          <ShopSketch name={name} key={index} deleteShop={deleteShop} />
        ))}
      </Radio.Group>
    </div>
  );
};
export default Example;

and the shopSketch Component is: shopSketch 组件是:

import { Radio } from 'antd';
import { React } from 'react';
const ShopSketch = ({ name, deleteShop }) => {
  return (
    <Radio.Button
      value={name}
      style={{ height: '200px', borderWidth: '2.5px' }}
    >
      <p>{name}</p>
      <ion-icon name="wallet-outline" size="big"></ion-icon>
    </Radio.Button>
  );
};

export default ShopSketch;

I tried to change the style directly in the radio button but it affects all the elements, conditionally import the css but I can't revert the action I ran out of ideas, I know I'm a bit new but I would really appreciate if you could give me any kind of help Thanks in advance.我试图直接在单选按钮中更改样式,但它会影响所有元素,有条件地导入 css,但我无法恢复操作我没有想法,我知道我有点新,但我真的很感激你可以给我任何形式的帮助 提前致谢。

finally, I was able to come up with a small solution最后,我想出了一个小解决方案

On example add SelectedShop as argument:例如添加 SelectedShop 作为参数:

<ShopSketch
  name={name}
  key={index}
  deleteShop={deleteShop}
  selectedShop={selectedShop}
/>

On ShopSketch add selectedShop and the style to radio.button:在 ShopSketch 上将 selectedShop 和样式添加到 radio.button:

const ShopSketch = ({ name, selectedShop, deleteShop }) =>

<Radio.Button
      value={name}
      className={`shop ${deleteShop ? 'shop-delete' : ''}`}
      style={
        selectedShop === name && deleteShop
          ? {
              border: '2.5px solid #ff4d4f',
              color: '#ff4d4f',
            }
          : { borderWidth: '2.5px' }
      }
    >

On css:在 CSS 上:

.shop-delete:hover {
  color: #ff4d4f;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM