覆盖组件样式#antd
因为业务的个性化需求,咱们常常会遇到须要覆盖组件样式的状况,这里举个简单的例子。less
antd Select 在多选状态下,默认会展现全部选中项,这里咱们给它加一个限制高度,超过此高度就出滚动条。code
// TestPage.jsip
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; } } }
方法很简单,有两点须要注意:io
引入的 antd 组件类名没有被 CSS Modules 转化,因此被覆盖的类名 .ant-select-selection 必须放到 :global 中。class
由于上一条的关系,覆盖是全局性的。为了防止对其余 Select 组件形成影响,因此须要包裹额外的 className 限制样式的生效范围。import