如何監聽Ant Design of React 的Search組件設置allowClear後的點擊事件?

image.png

因爲官方沒有給出這個x的點擊事件的監聽hook,因此只能本身曲線救過。spa

根據文檔說明:code

onSearch

The callback function triggered when you click on the search-icon, the clear-icon or press the Enter key對象

能夠了解到onSearch的觸發條件。blog

而後經過event對象進行觸發類型的判斷。事件

onSearch = (value, event) => {
    if (event.nativeEvent.type === 'click' && value === '') {
        // listen click
        setTimeout(() => {
            // TODO
        }, 300);
    }

    
    if (event.nativeEvent.type === 'enter' && value === '') {
        // Enter
        setTimeout(() => {
            // TODO
        }, 300);
    }


    if (value === '') {
        return;
    }

    // search
};
相關文章
相關標籤/搜索