Skip to content

Commit

Permalink
fix(components): ensure undefined is thrown if value is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
anna-parker committed Jan 15, 2025
1 parent 2538257 commit 1e2054a
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
type LapisTextFilter = Record<string, string | null | undefined>;

export class TextChangedEvent extends CustomEvent<LapisTextFilter> {
export class TextInputChangedEvent extends CustomEvent<LapisTextFilter> {
constructor(detail: LapisTextFilter) {
super('gs-text-input-changed', {
detail,
Expand Down
2 changes: 1 addition & 1 deletion components/src/preact/textInput/text-input.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export const WithInitialValue: StoryObj<TextInputProps> = {
await expect(changedListenerMock).toHaveBeenCalledWith(
expect.objectContaining({
detail: {
host: null,
host: undefined,
},
}),
);
Expand Down
9 changes: 4 additions & 5 deletions components/src/preact/textInput/text-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -94,7 +94,6 @@ const TextSelector = ({
return true;
}
return (
item?.toLowerCase().includes(inputValue?.toLowerCase() || '') ||
item?.toLowerCase().includes(inputValue?.toLowerCase() || '')
);
}
Expand All @@ -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,
Expand All @@ -130,15 +129,15 @@ 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 || '');
}
};

const clearInput = () => {
divRef.current?.dispatchEvent(new TextChangedEvent({ [lapisField]: null }));
divRef.current?.dispatchEvent(new TextInputChangedEvent({ [lapisField]: undefined }));
selectItem(null);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export const FiresEventNoValidInput: StoryObj<Required<TextInputProps>> = {
await userEvent.type(inputField(), '{backspace>9/}');
await inputField().blur();
await expect(listenerMock.mock.calls.at(-1)![0].detail).toStrictEqual({
host: null,
host: undefined,
});
});
},
Expand Down

0 comments on commit 1e2054a

Please sign in to comment.