React Antd Select組件輸入搜索時(shí)調(diào)用接口方式
前言
由于接口返回較多,需要在Select組件中輸入時(shí)進(jìn)行調(diào)用接口并搜索,而不是初始化時(shí)把全部寫入Option。
問(wèn)題
當(dāng)輸入中文時(shí),每打一個(gè)字母都會(huì)調(diào)用一次接口,想在中文輸入完成,即點(diǎn)擊空格后再進(jìn)行接口調(diào)用
<Select
prefix={
<SearchOutlined
style={{
color: SearchOutlinedColor?.backgroundColor,
}}
/>
}
showSearch
value={orgValue}
placeholder={t('Input Search Text')}
style={{ width: 250 }}
defaultActiveFirstOption={false}
suffixIcon={null}
filterOption={false}
notFoundContent={null}
onSearch={handleOrgSearch}
onChange={handleOrgChange}
options={orgOptions}
onCompositionStart={handleCompositionStart} // 對(duì)中文的處理
onCompositionEnd={handleCompositionEnd}
allowClear
onClear={() => {
setOrgValue('');
onOrgSearch('');
setOrgOptions([]);
}}
size="small"
variant="filled"
/>
引入lodash.debounce
const [orgValue, setOrgValue] = useState();
const [orgOptions, setOrgOptions] = useState([]);
const [isComposing, setIsComposing] = useState(false);
const searchRef = useRef(null);
// 注冊(cè)debouncedRef,輸入結(jié)束后再進(jìn)行接口調(diào)用
const debouncedFetchOrgData = useRef(
debounce((searchValue) => {
fetchOrgList(searchValue, setOrgOptions);
}, 500)
).current;
// 調(diào)用接口,獲取列表,放入option中
const fetchOrgList = (value, callback) => {
selectCenterInfo({ name: value })
.then(result => {
if (result.length) {
const data = result.map((item) => ({
value: item.value,
label: `${item.label}(${item.value})`,
}));
callback(data);
} else {
callback([]);
}
})
.catch(err => {
console.log(err);
callback([]);
});
};
const handleOrgSearch = (newValue) => {
searchRef.current = newValue;
if (!isComposing) {
debouncedFetchOrgData(newValue);
};
};
const handleOrgChange = (newValue) => {
setOrgValue(newValue);
onOrgSearch(newValue); // 這是傳入組件的props, 用來(lái)把選中的值傳回父組件
};
const handleCompositionStart = () => {
setIsComposing(true);
};
const handleCompositionEnd = (e) => {
setIsComposing(false);
searchRef.current = e.target.value;
if (searchRef.current) {
fetchOrgList(searchRef.current, setOrgOptions);
}
};
useEffect(() => {
return () => {
debouncedFetchOrgData.cancel();
};
}, [debouncedFetchOrgData]);
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
React?hook實(shí)現(xiàn)簡(jiǎn)單的websocket封裝方式
這篇文章主要介紹了React?hook實(shí)現(xiàn)簡(jiǎn)單的websocket封裝方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
React中使用collections時(shí)key的重要性詳解
這篇文章主要給大家介紹了關(guān)于在React.js中使用collections時(shí)key的重要性,注意:一定不能不能忘了key,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面跟著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08
antd?table動(dòng)態(tài)修改表格高度的實(shí)現(xiàn)
本文主要介紹了antd?table動(dòng)態(tài)修改表格高度的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
使用React實(shí)現(xiàn)輪播效果組件示例代碼
React剛出來(lái)不久,組件還比較少,不像jquery那樣已經(jīng)有很多現(xiàn)成的插件了,于是自己寫了一個(gè)基于React的輪播效果組件,現(xiàn)在分享給大家,有需要的可以參考借鑒。2016-09-09
React Native基礎(chǔ)入門之初步使用Flexbox布局
React中引入了flexbox概念,flexbox是屬于web前端領(lǐng)域CSS的一種布局方案,下面這篇文章主要給大家介紹了關(guān)于React Native基礎(chǔ)入門之初步使用Flexbox布局的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-07-07
React中的Hooks路由跳轉(zhuǎn)問(wèn)題
這篇文章主要介紹了React中的Hooks路由跳轉(zhuǎn)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
react數(shù)據(jù)管理機(jī)制React.Context源碼解析
這篇文章主要為大家介紹了react數(shù)據(jù)管理機(jī)制React.Context源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11

