Skip to content

Commit

Permalink
fix(ui): svelte select clear event (#1792)
Browse files Browse the repository at this point in the history
Signed-off-by: axel7083 <[email protected]>
  • Loading branch information
axel7083 authored Sep 26, 2024
1 parent 7576181 commit 01a82be
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
35 changes: 35 additions & 0 deletions packages/frontend/src/lib/select/Select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,38 @@ test('selecting value should call onchange callback', async () => {
});
});
});

test('clearing value should call onchange callback with undefined', async () => {
const onChangeMock = vi.fn();
const { container } = render(Select, {
label: 'Select Item',
items: [
{
label: 'Dummy Item 1',
value: 'item-1',
},
{
label: 'Dummy Item 2',
value: 'item-2',
},
],
value: {
label: 'Dummy Item 2',
value: 'item-2',
},
onchange: onChangeMock,
});

// get clear HTMLElement
const clear = container.querySelector('button[class~="clear-select"]');
// ensure we have two options
expect(clear).not.toBeNull();
if (!clear) throw new Error('clear is null');

await fireEvent.click(clear);

await vi.waitFor(() => {
expect(onChangeMock).toHaveBeenCalledWith(undefined);
expect(onChangeMock).toHaveBeenCalledOnce();
});
});
6 changes: 6 additions & 0 deletions packages/frontend/src/lib/select/Select.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@ function handleOnChange(e: CustomEvent<T | undefined>): void {
value = e.detail;
onchange?.(value);
}
function handleOnClear(): void {
value = undefined;
onchange?.(value);
}
</script>

<Select
inputAttributes={{ 'aria-label': label }}
name={name}
disabled={disabled}
value={value}
on:clear={handleOnClear}
on:change={handleOnChange}
--item-color={'var(--pd-dropdown-item-text)'}
--item-is-active-color={'var(--pd-dropdown-item-text)'}
Expand Down

0 comments on commit 01a82be

Please sign in to comment.