-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Expand file tree
/
Copy pathsimple-combobox.ts
More file actions
313 lines (247 loc) · 10.1 KB
/
Copy pathsimple-combobox.ts
File metadata and controls
313 lines (247 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {KeyboardEventManager, ClickEventManager, Modifier} from '../behaviors/event-manager';
import {computed, signal, untracked} from '@angular/core';
import {SignalLike, WritableSignalLike} from '../behaviors/signal-like/signal-like';
import {ExpansionItem} from '../behaviors/expansion/expansion';
/** Represents the required inputs for a simple combobox. */
export interface SimpleComboboxInputs extends ExpansionItem {
/** Whether the combobox should always remain expanded. */
alwaysExpanded: SignalLike<boolean>;
/** The value of the combobox. */
value: WritableSignalLike<string>;
/** The element that the combobox is attached to. */
element: SignalLike<HTMLElement>;
/** The popup associated with the combobox. */
popup: SignalLike<SimpleComboboxPopupPattern | undefined>;
/** An inline suggestion to be displayed in the input. */
inlineSuggestion: SignalLike<string | undefined>;
/** Whether the combobox is disabled. */
disabled: SignalLike<boolean>;
/** Whether the combobox is soft disabled. */
softDisabled?: SignalLike<boolean>;
/** Whether the combobox opens automatically on text input. */
openOnInput: SignalLike<boolean>;
/** Optional trigger element associated with the combobox. */
trigger?: SignalLike<HTMLElement | undefined>;
}
/** Controls the state of a simple combobox. */
export class SimpleComboboxPattern {
/** The expanded state of the combobox. */
readonly isExpanded = computed(() => this.inputs.alwaysExpanded() || this.inputs.expanded());
/** The value of the combobox. */
readonly value: WritableSignalLike<string>;
/** The element that the combobox is attached to. */
readonly element = () => this.inputs.element();
/** Whether the combobox is disabled. */
readonly disabled = () => this.inputs.disabled();
/** Whether the combobox is soft disabled. */
readonly softDisabled = () => this.inputs.softDisabled?.() ?? true;
/** An inline suggestion to be displayed in the input. */
readonly inlineSuggestion = () => this.inputs.inlineSuggestion();
/** The ID of the active descendant in the popup. */
readonly activeDescendant = computed(() => this.inputs.popup()?.activeDescendant());
/** The ID of the popup. */
readonly popupId = computed(() => this.inputs.popup()?.popupId());
/** The type of the popup. */
readonly popupType = computed(() => this.inputs.popup()?.popupType());
/** The autocomplete behavior of the combobox. */
readonly autocomplete = computed<'none' | 'inline' | 'list' | 'both'>(() => {
const popupType = this.popupType();
const hasAutocompletePopup = !!this.inputs.popup() && popupType !== 'dialog';
const hasInlineSuggestion = !!this.inlineSuggestion();
if (hasAutocompletePopup && hasInlineSuggestion) {
return 'both';
}
if (hasAutocompletePopup) {
return 'list';
}
if (hasInlineSuggestion) {
return 'inline';
}
return 'none';
});
/** A relay for keyboard events to the popup. */
readonly keyboardEventRelay = signal<KeyboardEvent | undefined>(undefined);
/** Whether the combobox is focused. */
readonly isFocused = signal(false);
/** Whether the most recent input event was a deletion. */
readonly isDeleting = signal(false);
/** Whether the combobox is editable (i.e., an input or textarea). */
readonly isEditable = computed(
() =>
this.element().tagName.toLowerCase() === 'input' ||
this.element().tagName.toLowerCase() === 'textarea',
);
/** The keydown event manager for the combobox. */
keydown = computed(() => {
const manager = new KeyboardEventManager();
if (!this.isExpanded()) {
manager.on('ArrowDown', () => this.inputs.expanded.set(true));
if (!this.isEditable()) {
manager.on('Enter', () => this.inputs.expanded.set(true));
manager.on(' ', () => this.inputs.expanded.set(true));
}
return manager;
}
manager
.on(
'ArrowLeft',
e => {
this.keyboardEventRelay.set(e);
},
{preventDefault: this.popupType() !== 'listbox', ignoreRepeat: false},
)
.on(
'ArrowRight',
e => {
this.keyboardEventRelay.set(e);
},
{preventDefault: this.popupType() !== 'listbox', ignoreRepeat: false},
)
.on('ArrowUp', e => this.keyboardEventRelay.set(e), {ignoreRepeat: false})
.on('ArrowDown', e => this.keyboardEventRelay.set(e), {ignoreRepeat: false})
.on(Modifier.Shift, 'ArrowUp', e => this.keyboardEventRelay.set(e), {ignoreRepeat: false})
.on(Modifier.Shift, 'ArrowDown', e => this.keyboardEventRelay.set(e), {ignoreRepeat: false})
.on('Home', e => this.keyboardEventRelay.set(e))
.on('End', e => this.keyboardEventRelay.set(e))
.on('Enter', e => this.keyboardEventRelay.set(e))
.on('PageUp', e => this.keyboardEventRelay.set(e))
.on('PageDown', e => this.keyboardEventRelay.set(e))
.on('Escape', () => {
if (!this.inputs.alwaysExpanded()) {
this.inputs.expanded.set(false);
}
});
if (!this.isEditable()) {
manager
.on(' ', e => this.keyboardEventRelay.set(e))
.on([Modifier.Ctrl, Modifier.Meta], 'a', e => this.keyboardEventRelay.set(e))
.on([Modifier.Ctrl, Modifier.Meta], 'A', e => this.keyboardEventRelay.set(e))
.on(Modifier.Shift, 'Home', e => this.keyboardEventRelay.set(e))
.on(Modifier.Shift, 'End', e => this.keyboardEventRelay.set(e))
.on(/^.$/, e => {
this.keyboardEventRelay.set(e);
});
}
return manager;
});
/** The click event manager for the combobox. */
click = computed(() => {
const manager = new ClickEventManager<PointerEvent>();
if (this.isEditable()) return manager;
manager.on(() => this.inputs.expanded.update(v => !v));
return manager;
});
constructor(readonly inputs: SimpleComboboxInputs) {
this.value = inputs.value;
}
/** Handles keydown events for the combobox. */
onKeydown(event: KeyboardEvent) {
if (!this.inputs.disabled()) {
this.keydown().handle(event);
}
}
/** Handles click events for the combobox. */
onClick(event: PointerEvent) {
if (!this.disabled()) {
this.click().handle(event);
}
}
/** Handles focus in events for the combobox. */
onFocusin() {
this.isFocused.set(true);
}
/** Handles focus out events for the combobox. */
onFocusout(event: FocusEvent) {
this.isFocused.set(false);
}
/** Handles input events for the combobox. */
onInput(event: Event) {
if (!(event.target instanceof HTMLInputElement)) return;
if (this.disabled()) return;
if (this.inputs.openOnInput()) {
this.inputs.expanded.set(true);
}
this.value.set(event.target.value);
this.isDeleting.set(event instanceof InputEvent && !!event.inputType.match(/^delete/));
}
/** Highlights the currently selected item in the combobox. */
highlightEffect() {
const value = this.value();
const inlineSuggestion = this.inlineSuggestion();
const isDeleting = untracked(() => this.isDeleting());
const isFocused = untracked(() => this.isFocused());
const isExpanded = this.isExpanded();
if (!inlineSuggestion || !isFocused || !isExpanded || isDeleting) return;
const inputEl = this.element() as HTMLInputElement;
const isHighlightable = inlineSuggestion.toLowerCase().startsWith(value.toLowerCase());
if (isHighlightable) {
inputEl.value = value + inlineSuggestion.slice(value.length);
inputEl.setSelectionRange(value.length, inlineSuggestion.length);
}
}
/** Relays keyboard events to the popup. */
keyboardEventRelayEffect() {
const event = this.keyboardEventRelay();
if (event === undefined) return;
// Reset isDeleting when the user navigates, so that the highlight effect can run again.
this.isDeleting.set(false);
const popup = untracked(() => this.inputs.popup());
const popupExpanded = untracked(() => this.isExpanded());
if (popupExpanded) {
popup?.controlTarget()?.dispatchEvent(event);
}
}
/** Closes the popup when focus leaves the combobox and popup. */
closePopupOnBlurEffect() {
if (!this.isExpanded() || this.inputs.alwaysExpanded()) return;
const comboboxFocused = this.isFocused();
const popupFocused = !!this.inputs.popup()?.isFocused();
const triggerEl = this.inputs.trigger?.();
const triggerFocused = !!triggerEl?.contains(document.activeElement);
if (!comboboxFocused && !popupFocused && !triggerFocused) {
this.inputs.expanded.set(false);
}
}
}
/** Represents the required inputs for a simple combobox popup. */
export interface SimpleComboboxPopupInputs {
/** The type of the popup. */
popupType: SignalLike<'listbox' | 'tree' | 'grid' | 'dialog'>;
/** The element that serves as the control target for the popup. */
controlTarget: SignalLike<HTMLElement | undefined>;
/** The ID of the active descendant in the popup. */
activeDescendant: SignalLike<string | undefined>;
/** The ID of the popup. */
popupId: SignalLike<string | undefined>;
}
/** Controls the state of a simple combobox popup. */
export class SimpleComboboxPopupPattern {
/** The type of the popup. */
readonly popupType = () => this.inputs.popupType();
/** The element that serves as the control target for the popup. */
readonly controlTarget = () => this.inputs.controlTarget();
/** The ID of the active descendant in the popup. */
readonly activeDescendant = () => this.inputs.activeDescendant();
/** The ID of the popup. */
readonly popupId = () => this.inputs.popupId();
/** Whether the popup is focused. */
readonly isFocused = signal(false);
constructor(readonly inputs: SimpleComboboxPopupInputs) {}
/** Handles focus in events for the popup. */
onFocusin() {
this.isFocused.set(true);
}
/** Handles focus out events for the popup. */
onFocusout(event: FocusEvent) {
const focusTarget = event.relatedTarget as Element | null;
if (this.controlTarget()?.contains(focusTarget)) return;
this.isFocused.set(false);
}
}