網(wǎng)站首頁 編程語言 正文
這是之前做個小需求遇到的小問題,要做的樣式類似下圖,當時看了ant-design的文檔,并沒有直接的例子可以用,但是發(fā)現(xiàn)ant-design提供了labelInValue屬性,也就可以自定義顯示的內(nèi)容了。
實現(xiàn)的代碼如下
import React, { Component } from 'react';
import { Select } from 'antd';
import './index.less';
import 'antd/dist/antd.css';
export default class Demo extends Component {
state = {
user: { id: 1, name: '管理員', color: '#126EE3' },
optionsList: [
{ id: 1, name: '管理員', color: '#126EE3' },
{ id: 2, name: '普通用戶', color: '#52C41A' },
{ id: 3, name: '超級管理員', color: '#E8D12C' }
]
};
getLbael = item => {
return {
value: item?.name,
label: (
<div>
<span style={{ display: 'inline-block', width: 10, height: 10, background: item?.color }}></span>
<span style={{ marginLeft: 8 }}>{item?.name}</span>
</div>
)
};
};
selectChange = e => {
const { optionsList } = this.state;
const user = optionsList.filter(u => u.name === e.key)?.[0];
this.setState({
user
});
};
render() {
const { user, optionsList } = this.state;
return (
<div style={{ margin: 100 }}>
<Select
labelInValue
placeholder="請選擇用戶類型"
value={this.getLbael(user)}
onChange={e => this.selectChange(e)}
style={{ width: 270 }}
// getPopupContainer={e => e.parentElement}
>
{optionsList.map(item => (
<Select.Option key={item?.id} value={item?.name}>
<div>
<span style={{ display: 'inline-block', width: 10, height: 10, background: item?.color }}></span>
<span style={{ marginLeft: 8 }}>{item?.name}</span>
</div>
</Select.Option>
))}
</Select>
</div>
);
}
}
原文鏈接:https://blog.csdn.net/study_way/article/details/127323750
- 上一篇:沒有了
- 下一篇:沒有了
相關(guān)推薦
- 2022-05-31 Python學習之yaml文件的讀取詳解_python
- 2021-12-07 bitbucket搭建詳細過程記錄_其它綜合
- 2022-07-23 深度解析C語言中數(shù)據(jù)的存儲_C 語言
- 2022-11-03 tomcat?集群監(jiān)控與彈性伸縮詳解_Tomcat
- 2022-06-14 jquery實現(xiàn)點擊按鈕顯示與隱藏效果_jquery
- 2022-10-18 AJAX跨域問題解決方案詳解_AJAX相關(guān)
- 2022-12-15 Python?OpenCV中cv2.minAreaRect實例解析_python
- 2022-05-22 關(guān)于VS2022不能使用<bits/stdc++.h>的解決方案(萬能頭文件)_C 語言
- 欄目分類
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支