简体   繁体   English

如何改变 Ant-Design 'Select' 组件的样式?

[英]How to change the style of a Ant-Design 'Select' component?

Suppose I want to change the standard white background color of the Select component to green.假设我想将 Select 组件的标准白色背景颜色更改为绿色。

My try...我的尝试...

<Select
 style={{ backgroundColor: 'green' }}>
   // Options...
</Select>

...didn't do it. ……没做。

Can someone point me in the right direction?有人能指出我正确的方向吗?

[EDIT] [编辑]

I ended up using the suggested approach from Jesper We .我最终使用了Jesper We推荐的方法。

Overwriting the color for all selections...覆盖所有选择的颜色...

.ant-select-selection {
  background-color: transparent;
}

...then I could style the Select components individually. ...然后我可以单独设置 Select 组件的样式。

<Select> renders a whole set of <div> s, you need to take a look at the resulting HTML element tree to understand what you are doing. <Select>呈现一整套<div> ,您需要查看生成的 HTML 元素树以了解您在做什么。 You can't do it through the style attribute, you need to do it in CSS.你不能通过 style 属性来做,你需要在 CSS 中做。

The proper place to attach a background color is附加背景颜色的正确位置是

.ant-select-selection {
  background-color: green;
}

This will make all your selects green.这将使您的所有选择变为绿色。 Give them individual className s if you want different colors for different selects.如果你想要不同的选择不同的颜色,给他们单独的className s。

For my form with Select element a have some code in render :对于我的带有 Select 元素的表单,在render有一些代码:

const stateTasksOptions =
    this.tasksStore.filters.init.state.map(item =>
        <Select.Option key={item.id} value={item.id} title={<span className={`${item.id}Label`}>{item.title}</span>}>
            <span className={`${item.id}Label`}>{item.title}</span> - <span class="normal-text">{item.help}</span>
        </Select.Option>
    )

return (
    ....
    <Select
        mode="multiple"
        value={this.tasksStore.filters.selected.state.map(d => d)}
        onChange={this.handleTasksStatus}
        optionLabelProp="title"
    >
        {stateTasksOptions}
    </Select>
    ....
)

And some css for colorizing.还有一些用于着色的css。

Result :结果 在此处输入图片说明

Try dropdownStyle instead of style.尝试 dropdownStyle 而不是 style。

<Select
 dropdownStyle={{ backgroundColor: 'green' }}>
   // Options...
</Select>

dropdownStyle is one of select props. dropdownStyle 是选择道具之一。

reference: antd select参考: antd选择

with all the above answers you cant change the styles of tags conditionally but with below approach you can.使用上述所有答案,您无法有条件地更改标签样式,但使用以下方法可以。

You can do a hack and change the styles as you like of tags of select dropdown.您可以根据自己的喜好更改选择下拉列表的标签的样式。 You can use dropdownRender of select which takes 2 arguments您可以使用带有 2 个参数的 select 的 dropdownRender

  • menuNode菜单节点
  • props道具

use props children property to reach to each tag and change the styles and you can conditionally change the styles as you like.使用 props children 属性到达每个标签并更改样式,您可以根据需要有条件地更改样式。

for reference below is the example link for code sandbox以下是代码沙箱的示例链接,供参考

Select Tags Styles Sanbox选择标签样式沙盒

May not be an efficient way to do it but you can use this for now to meet your business requirement.可能不是一种有效的方法,但您现在可以使用它来满足您的业务需求。

Thanks谢谢

They implemented this feature with v4 of ant design:他们用 ant design 的 v4 实现了这个功能:
https://github.com/ant-design/ant-design/pull/21064 https://github.com/ant-design/ant-design/pull/21064

带有彩色标签的选择框

But beware before blindly upgrading from v3 -> v4 - a lot has changed:但是在从 v3 -> v4 盲目升级之前要小心 - 发生了很多变化:
https://github.com/ant-design/ant-design/issues/20661 https://github.com/ant-design/ant-design/issues/20661

Somebody stated the selector to be有人说选择器是

.ant-select-selection {... .ant-select-selection {...

However it should be selector as follows:但是它应该是如下选择器

.ant-select-selector {
  background-color: green;
}

From their official docs https://pro.ant.design/docs/style从他们的官方文档https://pro.ant.design/docs/style

Override the component style Because of the special needs of the project, we often meet the need to cover the component style, here is a simple example.覆盖组件样式因为项目的特殊需要,我们经常会遇到覆盖组件样式的需求,这里举一个简单的例子。

Antd Select In multi-select state, the default will show all the select items, here we add a limit height for display scroll bar when the content beyond this height. Antd Select 在多选状态下,默认会显示所有的选择项,这里我们添加了一个限制高度,当内容超过这个高度时显示滚动条。

// TestPage.ts
import { Select } from 'antd';
import styles from './TestPage.less';
const Option = Select.Option;

const children = [];
for (let i = 10; i < 36; i++) {
  children.push(<Option key={i.toString(36) + i}>{i.toString(36) + i}</Option>);
}

ReactDOM.render(
  <Select
    mode="multiple"
    style={{ width: 300 }}
    placeholder="Please select"
    className={styles.customSelect}
  >
        {children}
      
  </Select>,
  mountNode,
);
/* TestPage.less */
.customSelect {
  :global {
    .ant-select-selection {
      max-height: 51px;
      overflow: auto;
    }
  }
}

Two points need to be noted:需要注意两点:

The imported antd component class name is not translated by CSS Modules, so the overridden class name .ant-select-selection must be put in :global.导入的 antd 组件类名不会被 CSS Modules 翻译,所以覆盖的类名 .a​​nt-select-selection 必须放在 :global 中。 Because of the previous note, the override is global.由于前面的说明,覆盖是全局的。 To avoid affecting other Select components, the setting needs to be wrapped by an extra classname to add range restriction为避免影响其他 Select 组件,设置需要用额外的类名包裹以添加范围限制

In angular, you can override the style with ng-deep在 angular 中,您可以使用 ng-deep 覆盖样式

::ng-deep .ant-select-selector {
  background-color: red;
}

menuItemSelectedIcon={(props) => {
                return (mode == "multiple" ?
                  <Tooltip title="Check to confirm the apps alongwith the vendor">
                    <input type="checkbox" checked={props.isSelected}
                      style={{
                        margin: 5
                      }}
                    />
                  </Tooltip>
                  : null)
              }}

lastly i was working on ant dropdown and it did not get style as i wanted and i did not find a good solution for that;最后,我正在研究 ant 下拉菜单,但它没有达到我想要的风格,我也没有找到一个好的解决方案; then i decide to share my css solution for those who are in my situation:然后我决定为那些处于我情况的人分享我的CSS解决方案:

 .license-plate-letters { overflow-y: hidden;important: min-width; 240px.important: ;rc-virtual-list-holder>div { height. auto:important; }:rc-virtual-list-holder-inner { display, grid;important: grid-template-columns; repeat(5: 1fr);important. flex-direction: row.important; flex-wrap: wrap:important; :ant-select-item-option { padding; 0.5rem 12px !important; &:hover { background-color: #452380d2 !important; color: white !important; } } } }
 <Select virtual={false} popupClassName="license-plate-letters"> <Select.Option key={sth} Title="title">title</Select.Option> </Select>

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

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