- 使用 onDropdownVisibleChange 监听下拉菜单的打开和关闭状态,在菜单打开时记录搜索值,在菜单关闭时清空搜索框中的值。
- 使用 ref 获取 Select 组件实例,在 onBlur 事件中判断菜单是否打开,如果没有打开则清空搜索框中的值。
class CustomSelect extends React.Component {
constructor(props) {
super(props);
this.selectRef = React.createRef();
this.state = { searchValue: '' };
}
handleBlur = () => {
setTimeout(() => {
if (!this.selectRef.current.rcSelect.state.dropdownRender) {
this.setState({ searchValue: '' });
}
});
};
render() {
const { options } = this.props;
return (
);
}
}