Skip to content

Commit

Permalink
feat: added replacing modelValues in dropdown select instead of toggling
Browse files Browse the repository at this point in the history
  • Loading branch information
mwargan committed Apr 23, 2024
1 parent 1a0d006 commit e3b6a95
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/components/DropdownSelect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,14 @@ const {
updateModelValue,
} = useMultiselect(props, emit);
const setModelValue = (event: Event) => {
const setModelValue = (
event: Event,
existingValue = false as number | false
) => {
const target = event.target as HTMLInputElement;
const value = target.value;
updateModelValue(value, target.checked);
updateModelValue(value, target.checked, existingValue);
};
const filteredOptions = computed(() => {
Expand Down Expand Up @@ -357,7 +360,7 @@ defineExpose({ focus });
</li>
<li
v-for="option in orderedOptions?.slice(0, props.visibleLimit)"
v-for="(option, index) in orderedOptions?.slice(0, props.visibleLimit)"
:key="option[modelKey]"
>
<slot
Expand All @@ -366,6 +369,8 @@ defineExpose({ focus });
:checked="isOptionSelected(option)"
:updateModelValue="setModelValue"
:modelValue="props.modelValue"
:index="index"
:value="option[modelKey]"
>
<label>
<input
Expand Down
46 changes: 46 additions & 0 deletions src/components/__tests__/DropdownSelect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,4 +510,50 @@ describe("Dropdown Select", () => {
["One", "Two", "Three"],
]);
});

it("correctly updates a value in place when setModelValue is passed with an index in the second", async () => {
// We need to use the slots to test this
const wrapper = mount(DropdownSelect, {
props: {
open: true,
options: ["One", "Two", "Three"],
modelValue: ["One", "Two", "Three"],
multiple: true,
"onUpdate:modelValue": (value) => {
wrapper.setProps({ modelValue: value });
},
},
slots: {
optionSlot: `
<template
#optionSlot="{ option, updateModelValue, modelValue, index }"
>
<label>
<input type="text"
:value="option.id"
@input="updateModelValue($event, index)"
/>
{{ option }}
</label>
</template>
`,
},
});

// Click on the summary to open the dropdown
await wrapper.find("details").trigger("click");

// Find the input for the second option - it will have the value of Two
const input = wrapper.findAll("input").at(1);

expect(input?.element.value).toBe("Two");

// Change the value of the input
await input?.setValue("New Value");

// Confirm the value has been updated by checking that the second option is now "New Value"
expect(wrapper.emitted("update:modelValue")?.[0]).toEqual([
["One", "New Value", "Three"],
]);
});
});

0 comments on commit e3b6a95

Please sign in to comment.