Skip to content

Commit

Permalink
Call onFilterValueChange on blur if no value is selected
Browse files Browse the repository at this point in the history
  • Loading branch information
zaguiini committed Sep 12, 2024
1 parent 0423a7e commit 4455012
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
5 changes: 5 additions & 0 deletions packages/components/src/combobox-control/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ function ComboboxControl( props: ComboboxControlProps ) {

const onBlur = () => {
setInputHasFocus( false );

if ( ! currentOption ) {
onFilterValueChange( '' );
setInputValue( '' );
}
};

const onFocus = () => {
Expand Down
41 changes: 40 additions & 1 deletion packages/components/src/combobox-control/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ describe.each( [
expect( input ).toHaveValue( targetOption.label );
} );

it( 'calls onFilterValueChange whenever the textbox changes', async () => {
it( 'does not call onFilterValueChange on focus', async () => {
const user = userEvent.setup();
const onChangeSpy = jest.fn();
render(
Expand All @@ -202,12 +202,51 @@ describe.each( [

await user.click( input );
expect( onChangeSpy ).not.toHaveBeenCalled();
} );

it( 'calls onFilterValueChange whenever the textbox changes', async () => {
const user = userEvent.setup();
const onChangeSpy = jest.fn();
render(
<Component
options={ timezones }
label={ defaultLabelText }
onFilterValueChange={ onChangeSpy }
/>
);

const input = getInput( defaultLabelText );

await user.type( input, 'a' );
expect( onChangeSpy ).toHaveBeenCalledTimes( 1 );
expect( onChangeSpy ).toHaveBeenCalledWith( 'a' );
} );

it( 'clears the textbox value if there is no selected value on blur', async () => {
const user = userEvent.setup();
const onChangeSpy = jest.fn();
render(
<Component
options={ timezones }
label={ defaultLabelText }
onFilterValueChange={ onChangeSpy }
/>
);
const input = getInput( defaultLabelText );

await user.type( input, 'a' );
expect( input ).toHaveValue( 'a' );

onChangeSpy.mockReset();

// Clicking document.body to trigger a blur event on the input.
await user.click( document.body );

expect( input ).toHaveValue( '' );
expect( onChangeSpy ).toHaveBeenCalledTimes( 1 );
expect( onChangeSpy ).toHaveBeenCalledWith( '' );
} );

it( 'should select the correct option from a search', async () => {
const user = await userEvent.setup();
const targetOption = timezones[ 13 ];
Expand Down

0 comments on commit 4455012

Please sign in to comment.