前几天在用antd Select的时候遇到的一个问题,觉得可以记录一下。

既然是问题,那就得解决了,废话不多说,直接上代码
const [state, dispatchState] = useReducer(reducer, initialState);
const handleSearch = useCallback(
debounce(value => searchValue(value), 300),
[],
);
const searchValue = value => {
const allList = userList.filter(item => {
return item.name.toLowerCase().indexOf(value.toLowerCase()) > -1;
});
dispatchState({
type: 'search',
payload: {
scrollPage: 1,
allUserList: allList,
currentUserList: [...allList.slice(0, perPage)],
},
});
};
const handleScroll = e => {
e.persist();
const { target } = e;
// scrollHeight:代表包括当前不可见部分的元素的高度
// scrollTop:代表当有滚动条时滚动条向下滚动的距离,也就是元素顶部被遮住的高度
// clientHeight:包括padding但不包括border、水平滚动条、margin的元素的高度
const rmHeight = target.scrollHeight - target.scrollTop;
const clHeight = target.clientHeight;
// 当下拉框失焦的时候,也就是不下拉的时候
if (rmHeight === 0 && clHeight === 0) {
dispatchState({ type: 'stopScroll' });
} else {
// 滚动到底部
if (rmHeight < clHeight + 5) {
dispatchState({ type: 'scroll' });
}
}
};
const handleBlur = () => {
dispatchState({
type: 'clear',
payload: initialState,
});
};

啊~写的都是啥啊,大概就这个意思,先就这样吧,代码只贴了相关的部分,部分变量定义未贴出来~