有没有办法覆盖 Ant design Switch 组件中的颜色

发布于 2025-01-21 03:48:20 字数 87 浏览 3 评论 0原文

我需要在选中时覆盖 Antd Switch 组件上的默认蓝色(主色)并将其更改为红色。我有办法做到这一点吗?

我尝试过使用样式属性,但它不起作用。

i need to override the default blue (primary color) on Antd Switch Component when checked and change it to red color. is there a way i can do this?

i have tried using style attribute but it didnt work.

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

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

发布评论

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

评论(6

只涨不跌 2025-01-28 03:48:20

您可以使用这样的内联样式更改背景颜色。

<Switch style={{backgroundColor: permission.enabled ? 'green' : 'orange'}}
        checked={permission.enabled}
        checkedChildren={'ENABLED'}
        unCheckedChildren={'DISABLED'}
        onChange={(e) => onPermissionChanged(e, permission)} />

You can change the backgroundColor using inline styles like this.

<Switch style={{backgroundColor: permission.enabled ? 'green' : 'orange'}}
        checked={permission.enabled}
        checkedChildren={'ENABLED'}
        unCheckedChildren={'DISABLED'}
        onChange={(e) => onPermissionChanged(e, permission)} />
毁虫ゝ 2025-01-28 03:48:20

您可以像这样重写 .ant-switch-checked

.ant-switch-checked {
   background-color: red;
}

You can override the .ant-switch-checked class like so

.ant-switch-checked {
   background-color: red;
}
空城之時有危險 2025-01-28 03:48:20

更简洁的方法是使用 Ant Design (antd) 5 版本之后提供的官方主题。下面是一个示例:

<ConfigProvider
      theme={{
        components: { 
          Switch: {
            colorPrimary: "red",
          },
        },
      }}
    >
      <Form.Item label="" name="remember" style={{ color: "white" }}>
         <Switch className="mb-2" />
      </Form.Item>
    </ConfigProvider>

A more concise way is utilizing the Ant Design (antd) official theme available after version 5. Here's an example:

<ConfigProvider
      theme={{
        components: { 
          Switch: {
            colorPrimary: "red",
          },
        },
      }}
    >
      <Form.Item label="" name="remember" style={{ color: "white" }}>
         <Switch className="mb-2" />
      </Form.Item>
    </ConfigProvider>

对不⑦ 2025-01-28 03:48:20

我发现

<Switch style={{backgroundColor: permission.enabled ? 'green' : 'orange'}}
        checked={permission.enabled}
        checkedChildren={'ENABLED'}
        unCheckedChildren={'DISABLED'}
        onChange={(e) => onPermissionChanged(e, permission)} />

当我做出反应表格setfieldvalue发生在此开关的其他地方时,做得并没有工作,所以我有

<Switch
    style={{backgroundColor:`${props.form.getFieldValue("permission")?"green":"orange"}`}}/>

I found that doing

<Switch style={{backgroundColor: permission.enabled ? 'green' : 'orange'}}
        checked={permission.enabled}
        checkedChildren={'ENABLED'}
        unCheckedChildren={'DISABLED'}
        onChange={(e) => onPermissionChanged(e, permission)} />

didnt quite work when i had react Form setFieldValue happening elsewhere to this switch so instead i have

<Switch
    style={{backgroundColor:`${props.form.getFieldValue("permission")?"green":"orange"}`}}/>
眸中客 2025-01-28 03:48:20

您可以通过覆盖开关类的默认类(您将从开发人员工具中获取所有元素类)来更改开关样式,

.ant-switch-checked{
   background:#fdfdfd !important;
}

因此它将全局覆盖颜色,要覆盖特定元素的颜色,只需将元素包装在某个 div 中给类“test”并覆盖CSS

.test .ant-switch-checked{
   background:#fdfdfd !important;
}

,它只会更新特定元素的颜色。

you can change switch styling by overriding default class (you will get all the element classes from developer tool) for switch class

.ant-switch-checked{
   background:#fdfdfd !important;
}

as a result it will override the color globally, to override the color for specific element only just wrap the element in some div by giving class "test" and override the css like

.test .ant-switch-checked{
   background:#fdfdfd !important;
}

and it will update the color for specific elemet only.

完美的未来在梦里 2025-01-28 03:48:20

在您的 main/theme 文件中,您可以像这样覆盖变量:

@switch-color: red; // @primary-color

或者

@switch-bg: red; // @component-background;

您实际上可以覆盖您使用的任何变量:参考

In your main/themefile, you can just override the variable like so:

@switch-color: red; // @primary-color

or

@switch-bg: red; // @component-background;

You can actually override any variables to your use: reference

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