Skip to content

Commit d62c01e

Browse files
authored
Dropdown bugfixes (#678)
1 parent a641308 commit d62c01e

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

.changeset/big-dryers-bow.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@crowdstrike/glide-core': patch
3+
---
4+
5+
- Single-select Dropdown now clears its input field when its selected option is removed.
6+
- Single-select Dropdown now sets the value of its input field to the label of the selected option on first render.

src/dropdown.test.interactions.filterable.ts

+27
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,33 @@ it('sets the `value` of its `<input>` to the label of the selected option when n
792792
expect(input?.value).to.equal(option?.label);
793793
});
794794

795+
it('clears the `value` of its `<input>` when single-select and its selected option is removed', async () => {
796+
const component = await fixture<GlideCoreDropdown>(
797+
html`<glide-core-dropdown label="Label" filterable>
798+
<glide-core-dropdown-option
799+
label="One"
800+
selected
801+
></glide-core-dropdown-option>
802+
803+
<glide-core-dropdown-option label="Two"></glide-core-dropdown-option>
804+
</glide-core-dropdown>`,
805+
);
806+
807+
component.querySelector('glide-core-dropdown-option')?.remove();
808+
809+
// Wait for `#onDefaultSlotChange()`.
810+
await aTimeout(0);
811+
812+
// Now wait for the forced update in `#onDefaultSlotChange()`.
813+
await component.updateComplete;
814+
815+
const input = component.shadowRoot?.querySelector<HTMLInputElement>(
816+
'[data-test="input"]',
817+
);
818+
819+
expect(input?.value).to.be.empty.string;
820+
});
821+
795822
it('clears the `value` of its `<input>` when multiselect and an option is selected', async () => {
796823
const component = await fixture<GlideCoreDropdown>(
797824
html`<glide-core-dropdown label="Label" placeholder="Placeholder" multiple>

src/dropdown.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1276,14 +1276,16 @@ export default class GlideCoreDropdown
12761276
if (
12771277
!this.multiple &&
12781278
this.#inputElementRef.value &&
1279-
this.lastSelectedOption?.value
1279+
this.lastSelectedOption
12801280
) {
12811281
this.#inputElementRef.value.value = this.lastSelectedOption.label;
12821282
this.inputValue = this.lastSelectedOption.label;
12831283

12841284
this.isInputOverflow =
12851285
this.#inputElementRef.value.scrollWidth >
12861286
this.#inputElementRef.value.clientWidth;
1287+
} else if (!this.multiple && this.#inputElementRef.value) {
1288+
this.#inputElementRef.value.value = '';
12871289
}
12881290
}
12891291

0 commit comments

Comments
 (0)