Skip to content

Commit 8fd2276

Browse files
committed
fixup! feat(cdk-experimental/ui-patterns): create combobox ui pattern
1 parent d9ade63 commit 8fd2276

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

src/cdk-experimental/ui-patterns/combobox/combobox.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ export type ComboboxPopupControls<T extends ListItem<V>, V> = {
4949
/** Selects the current item in the popup. */
5050
select: (item?: T) => void;
5151

52+
/** Clears the selection state of the popup. */
53+
clearSelection: () => void;
54+
5255
/** Filters the items in the popup. */
5356
filter: (text: string) => void;
5457

@@ -95,7 +98,20 @@ export class ComboboxPattern<T extends ListItem<V>, V> {
9598
.on('ArrowUp', () => this.prev())
9699
.on('Home', () => this.first())
97100
.on('End', () => this.last())
98-
.on('Escape', () => this.close())
101+
.on('Escape', () => {
102+
if (this.inputs.filterMode() === 'highlight' && this.inputs.popupControls()?.activeId()) {
103+
this.inputs.popupControls()?.unfocus();
104+
this.inputs.popupControls()?.clearSelection();
105+
106+
const inputEl = this.inputs.inputEl();
107+
108+
if (inputEl) {
109+
inputEl.value = this.searchString();
110+
}
111+
} else {
112+
this.close();
113+
}
114+
}) // TODO: When filter mode is 'highlight', escape should revert to the last committed value.
99115
.on('Enter', () => this.select({commit: true, close: true}));
100116
});
101117

@@ -141,7 +157,11 @@ export class ComboboxPattern<T extends ListItem<V>, V> {
141157
this.open();
142158
this.inputs.popupControls()?.first();
143159

144-
if (event instanceof InputEvent && event.inputType === 'deleteContentBackward') {
160+
if (
161+
event instanceof InputEvent &&
162+
this.inputs.filterMode() !== 'manual' &&
163+
event.inputType.match(/delete.*/)
164+
) {
145165
this.inputs.popupControls()?.select();
146166
return;
147167
}
@@ -161,18 +181,19 @@ export class ComboboxPattern<T extends ListItem<V>, V> {
161181
!(event.relatedTarget instanceof HTMLElement) ||
162182
!this.inputs.containerEl()?.contains(event.relatedTarget)
163183
) {
164-
this.close();
165-
166184
if (this.inputs.filterMode() !== 'manual') {
167185
this.commit();
168186
}
187+
188+
this.close();
169189
}
170190
}
171191

172192
/** Closes the combobox. */
173193
close() {
174194
this.expanded.set(false);
175195
this.inputs.popupControls()?.unfocus();
196+
this.inputs.popupControls()?.clearSelection();
176197
}
177198

178199
/** Opens the combobox. */
@@ -242,6 +263,10 @@ export class ComboboxPattern<T extends ListItem<V>, V> {
242263
if (opts.highlight) {
243264
this.highlight();
244265
}
266+
if (this.inputs.filterMode() === 'manual') {
267+
this.inputs.popupControls()?.clearSelection();
268+
this.inputs.value.set(undefined);
269+
}
245270
}
246271

247272
/** Updates the value of the input based on the currently selected item. */

src/cdk-experimental/ui-patterns/listbox/listbox.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ export class ListboxPattern<V> {
288288
first: () => this.listBehavior.first(),
289289
unfocus: () => this.listBehavior.unfocus(),
290290
select: item => this.listBehavior.select(item),
291+
clearSelection: () => this.listBehavior.deselectAll(),
291292

292293
getItem: e => this._getItem(e),
293294
getSelectedItem: () => this.inputs.items().find(i => i.selected()),

0 commit comments

Comments
 (0)