Skip to content

Commit a968a71

Browse files
authored
fix(Select): multiple form submission (#1816)
1 parent 8bb7605 commit a968a71

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

.changeset/clean-showers-float.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"bits-ui": patch
3+
---
4+
5+
fix(Select): don't submit empty string when no values are selected in multiple mode

packages/bits-ui/src/lib/bits/select/components/select-hidden-input.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import HiddenInput from "$lib/bits/utilities/hidden-input.svelte";
66
77
let {
8-
value = $bindable(""),
8+
value = $bindable(),
99
autocomplete,
1010
}: { value?: string } & Omit<HTMLInputAttributes, "value"> = $props();
1111

packages/bits-ui/src/lib/bits/select/select.svelte.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1182,7 +1182,7 @@ export class SelectGroupHeadingState {
11821182

11831183
interface SelectHiddenInputStateOpts
11841184
extends ReadableBoxedValues<{
1185-
value: string;
1185+
value: string | undefined;
11861186
}> {}
11871187

11881188
export class SelectHiddenInputState {

tests/src/tests/select/select.browser.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,17 @@ describe("select - multiple", () => {
851851

852852
await expectNotExists(t.getContent());
853853
});
854+
855+
it("should not submit an array with a single empty string when the `name` prop is passed but no items are selected", async () => {
856+
let submittedValues: string[] | undefined;
857+
const t = setupMultiple({
858+
onFormSubmit: (fd: FormData) => {
859+
submittedValues = fd.getAll("themes") as string[];
860+
},
861+
});
862+
await t.submit.click();
863+
expect(submittedValues).toHaveLength(0);
864+
});
854865
});
855866

856867
function getItems(getter: typeof page.getByTestId, items = testItems) {

0 commit comments

Comments
 (0)