Skip to content

Commit fdf0c1a

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

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

src/cdk-experimental/combobox/combobox.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ export class CdkCombobox<V> {
5757
/** The values of the current selected items. */
5858
value = model<V | undefined>(undefined);
5959

60+
filter = input<(inputText: string, itemText: string) => boolean>((inputText, itemText) =>
61+
itemText.toLowerCase().includes(inputText.toLowerCase()),
62+
);
63+
6064
/** The combobox ui pattern. */
6165
readonly pattern = new ComboboxPattern<any, V>({
6266
...this,

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ export type ComboboxInputs<T extends ListItem<V>, V> = {
2727

2828
/** The filtering mode for the combobox. */
2929
filterMode: SignalLike<'manual' | 'auto-select' | 'highlight'>;
30+
31+
/** The function used to filter items in the combobox. */
32+
filter: SignalLike<(inputText: string, itemText: string) => boolean>;
3033
};
3134

3235
/** An interface that allows combobox popups to expose the necessary controls for the combobox. */
@@ -189,6 +192,19 @@ export class ComboboxPattern<T extends ListItem<V>, V> {
189192
}
190193
}
191194

195+
setDefaultState() {
196+
if (this.inputs.value() !== undefined) {
197+
this.inputs.popupControls()?.setValue(this.inputs.value());
198+
199+
const inputEl = this.inputs.inputEl();
200+
const searchTerm = this.inputs.popupControls()?.getSelectedItem()?.searchTerm() ?? '';
201+
202+
if (inputEl) {
203+
inputEl.value = searchTerm;
204+
}
205+
}
206+
}
207+
192208
/** Closes the combobox. */
193209
close() {
194210
this.expanded.set(false);
@@ -223,7 +239,7 @@ export class ComboboxPattern<T extends ListItem<V>, V> {
223239
.startsWith(this.searchString().toLowerCase());
224240

225241
if (element && isHighlightable) {
226-
element.value = item.searchTerm();
242+
element.value = this.searchString() + item.searchTerm().slice(this.searchString().length);
227243
element.setSelectionRange(this.searchString().length, item.searchTerm().length);
228244
this.highlightedItem.set(item);
229245
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,10 @@ export class ListboxPattern<V> {
296296
setValue: (value: V | undefined) => this.inputs.value.set(value ? [value] : []),
297297

298298
filter: (text: string) => {
299+
const filterFn = this.inputs.combobox()!.inputs.filter();
300+
299301
this.inputs.items().forEach(i => {
300-
const isMatch = i.searchTerm().toLowerCase().includes(text.toLowerCase());
302+
const isMatch = filterFn(text, i.searchTerm());
301303
i.inert.set(isMatch ? null : true);
302304
});
303305
},

0 commit comments

Comments
 (0)