fix(core): 🐛 fire input event when search query is cleared

This commit is contained in:
alistair3149 2023-08-28 18:36:51 -07:00
parent bf0ed2bd55
commit 325f22f0dd
No known key found for this signature in database
GPG key ID: 16076C01E5C88864

View file

@ -158,6 +158,8 @@ function renderSearchClearButton( input ) {
clearButton = document.createElement( 'span' ),
clearIcon = document.createElement( 'span' );
let hasClearButton = false;
clearButton.classList.add( 'citizen-search__clear', 'citizen-search__formButton' );
clearIcon.classList.add( 'citizen-ui-icon', 'mw-ui-icon-wikimedia-clear' );
clearButton.append( clearIcon );
@ -166,14 +168,19 @@ function renderSearchClearButton( input ) {
event.preventDefault();
clearButton.remove();
input.value = '';
input.focus();
input.dispatchEvent( new Event( 'input' ) );
setTimeout( () => {
input.focus();
}, 10 );
} );
input.addEventListener( 'input', () => {
if ( input.value === '' ) {
clearButton.remove();
} else {
hasClearButton = false;
} else if ( hasClearButton === false ) {
input.after( clearButton );
hasClearButton = true;
}
} );
}