Antd-Select下拉搜索內(nèi)容高亮

一、期望效果:
期望效果圖.png

二、實(shí)現(xiàn)代碼:

// 模擬 options 數(shù)據(jù):
const options = [
        {label: '中國大陸', value: 'zg'},
        {label: '中國香港', value: '2e'},
        {label: '中國澳門', value: 'da'},
        {label: '中國臺灣', value: 'df'},
        {label: '阿富汗', value: '0b'},
        {label: '阿爾巴尼亞', value: 'a2'}
        // ......
];

// 初始化 options 數(shù)據(jù)
const [countryListOptions, setCountryListOptions] = useState();

useEffect(()=>{
    setCountryListOptions(options);
},[]);

const handleSearch = (value) => {
     const options = options.map((val) => {
      const content = val.label.replaceAll(value, `<i class='selectSearchString'>${ value }</i>`)
       return {
         label: <span dangerouslySetInnerHTML={{ __html: content }} />,
         value: val.value,
       };
     });
    setCountryListOptions(options);
};


// Select 組件應(yīng)用
<Select
      allowClear
      // disabled={disabled}
      showSearch
      // value={value}
      // onChange={(value) => {
        // onChange(value);
      // }}
      options={countryListOptions}
      onSearch={handleSearch}
      filterOption={(input, options) => {
        const labelVal =
          options.props.label.props.children ||
          options.props.label.props.dangerouslySetInnerHTML?.__html ||
          options.props.label;
        return labelVal.toLowerCase().indexOf(input.toLowerCase()) >= 0;
      }}
    />


// 樣式
:global(.selectSearchString){
    color: #8969c8;
    font-weight: bold;
    font-style: normal;
}

三、選中后 input 中文字樣式出現(xiàn) bug:

修復(fù)代碼:

/* less 中增加 */
:global(.ant-select-selection-item .selectSearchString){
    color: inherit;
    font-weight: normal;
}

四、再次顯示下拉面板時樣式 bug:

修復(fù)代碼:

// 選中后數(shù)據(jù)還原
onSelect={() => {
       setCountryListOptions(options);
}}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容