From 1e2054a72d483721d4897b4c39752ecc96757825 Mon Sep 17 00:00:00 2001 From: "Anna (Anya) Parker" <50943381+anna-parker@users.noreply.github.com> Date: Wed, 15 Jan 2025 13:50:35 +0100 Subject: [PATCH] fix(components): ensure undefined is thrown if value is empty --- .../{TextChangedEvent.ts => TextInputChangedEvent.ts} | 2 +- components/src/preact/textInput/text-input.stories.tsx | 2 +- components/src/preact/textInput/text-input.tsx | 9 ++++----- .../src/web-components/input/gs-text-input.stories.ts | 2 +- 4 files changed, 7 insertions(+), 8 deletions(-) rename components/src/preact/textInput/{TextChangedEvent.ts => TextInputChangedEvent.ts} (76%) diff --git a/components/src/preact/textInput/TextChangedEvent.ts b/components/src/preact/textInput/TextInputChangedEvent.ts similarity index 76% rename from components/src/preact/textInput/TextChangedEvent.ts rename to components/src/preact/textInput/TextInputChangedEvent.ts index cc0b5a5b..70992124 100644 --- a/components/src/preact/textInput/TextChangedEvent.ts +++ b/components/src/preact/textInput/TextInputChangedEvent.ts @@ -1,6 +1,6 @@ type LapisTextFilter = Record; -export class TextChangedEvent extends CustomEvent { +export class TextInputChangedEvent extends CustomEvent { constructor(detail: LapisTextFilter) { super('gs-text-input-changed', { detail, diff --git a/components/src/preact/textInput/text-input.stories.tsx b/components/src/preact/textInput/text-input.stories.tsx index 1e5b48e4..5fb229d4 100644 --- a/components/src/preact/textInput/text-input.stories.tsx +++ b/components/src/preact/textInput/text-input.stories.tsx @@ -104,7 +104,7 @@ export const WithInitialValue: StoryObj = { await expect(changedListenerMock).toHaveBeenCalledWith( expect.objectContaining({ detail: { - host: null, + host: undefined, }, }), ); diff --git a/components/src/preact/textInput/text-input.tsx b/components/src/preact/textInput/text-input.tsx index 5acb44cf..adde6c49 100644 --- a/components/src/preact/textInput/text-input.tsx +++ b/components/src/preact/textInput/text-input.tsx @@ -5,7 +5,7 @@ import z from 'zod'; import { fetchAutocompleteList } from './fetchAutocompleteList'; import { LapisUrlContext } from '../LapisUrlContext'; -import { TextChangedEvent } from './TextChangedEvent'; +import { TextInputChangedEvent } from './TextInputChangedEvent'; import { ErrorBoundary } from '../components/error-boundary'; import { LoadingDisplay } from '../components/loading-display'; import { NoDataDisplay } from '../components/no-data-display'; @@ -94,7 +94,6 @@ const TextSelector = ({ return true; } return ( - item?.toLowerCase().includes(inputValue?.toLowerCase() || '') || item?.toLowerCase().includes(inputValue?.toLowerCase() || '') ); } @@ -117,7 +116,7 @@ const TextSelector = ({ }, onSelectedItemChange({ selectedItem }) { if (selectedItem !== null) { - divRef.current?.dispatchEvent(new TextChangedEvent({ [lapisField]: selectedItem })); + divRef.current?.dispatchEvent(new TextInputChangedEvent({ [lapisField]: selectedItem })); } }, items, @@ -130,7 +129,7 @@ const TextSelector = ({ const onInputBlur = () => { if (inputValue === '') { - divRef.current?.dispatchEvent(new TextChangedEvent({ [lapisField]: null })); + divRef.current?.dispatchEvent(new TextInputChangedEvent({ [lapisField]: undefined })); selectItem(null); } else if (inputValue !== selectedItem) { setInputValue(selectedItem || ''); @@ -138,7 +137,7 @@ const TextSelector = ({ }; const clearInput = () => { - divRef.current?.dispatchEvent(new TextChangedEvent({ [lapisField]: null })); + divRef.current?.dispatchEvent(new TextInputChangedEvent({ [lapisField]: undefined })); selectItem(null); }; diff --git a/components/src/web-components/input/gs-text-input.stories.ts b/components/src/web-components/input/gs-text-input.stories.ts index 2f957b92..121c1381 100644 --- a/components/src/web-components/input/gs-text-input.stories.ts +++ b/components/src/web-components/input/gs-text-input.stories.ts @@ -123,7 +123,7 @@ export const FiresEventNoValidInput: StoryObj> = { await userEvent.type(inputField(), '{backspace>9/}'); await inputField().blur(); await expect(listenerMock.mock.calls.at(-1)![0].detail).toStrictEqual({ - host: null, + host: undefined, }); }); },