/
combobox.tsx
243 lines (204 loc) · 6.77 KB
/
combobox.tsx
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT license. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Component, Event, EventEmitter, Prop, State, Watch } from '@stencil/core';
import { SelectOption } from '../../shared/interfaces';
import { getActionFromKey, getUpdatedIndex, isScrollable, maintainScrollVisibility, MenuActions, uniqueId, filterOptions } from '../../shared/utils';
@Component({
tag: 'sui-combobox',
styleUrl: './combobox.css',
shadow: false
})
export class SuiCombobox {
/**
* Whether the combobox should filter based on user input. Defaults to false.
*/
@Prop() filter = false;
/**
* String label
*/
@Prop() label: string;
/**
* Array of name/value options
*/
@Prop() options: SelectOption[];
/**
* Emit a custom select event on value change
*/
@Event({
eventName: 'select'
}) selectEvent: EventEmitter;
// Active option index
@State() activeIndex = 0;
// Filtered options
@State() filteredOptions: SelectOption[];
// Menu state
@State() open = false;
// input value
@State() value: string;
// save reference to active option
private activeOptionRef: HTMLElement;
// Unique ID that should really use a UUID library instead
private htmlId = uniqueId();
// Prevent menu closing before click completed
private ignoreBlur = false;
// save reference to input element
private inputRef: HTMLInputElement;
// save reference to listbox
private listboxRef: HTMLElement;
// save the last selected value
private selectedValue = '';
@Watch('options')
watchOptions(newValue: SelectOption[]) {
if (this.filter) {
this.filteredOptions = filterOptions(newValue, this.value);
}
}
componentDidLoad() {
const {
filter = false,
options = [],
value = ''
} = this;
this.filteredOptions = filter ? filterOptions(options, value) : options;
this.value = typeof this.value === 'string' ? value : this.filteredOptions.length > 0 ? this.filteredOptions[0].name : '';
}
componentDidUpdate() {
if (this.open && isScrollable(this.listboxRef)) {
maintainScrollVisibility(this.activeOptionRef, this.listboxRef);
}
}
render() {
const {
activeIndex,
htmlId,
filter = false,
label = '',
open = false,
filteredOptions = [],
value
} = this;
const activeId = open ? `${htmlId}-${activeIndex}` : '';
return ([
<label id={htmlId} class="combo-label">{label}</label>,
<div class={{ combo: true, open }}>
<input
aria-activedescendant={activeId}
aria-autocomplete={filter ? "list" : "none"}
aria-controls={`${htmlId}-listbox`}
aria-expanded={`${open}`}
aria-haspopup="listbox"
aria-labelledby={htmlId}
class="combo-input"
ref={(el) => this.inputRef = el}
role="combobox"
type="text"
value={value}
onBlur={this.onInputBlur.bind(this)}
onClick={() => this.updateMenuState(true)}
onInput={this.onInput.bind(this)}
onKeyDown={this.onInputKeyDown.bind(this)}
/>
<div class="combo-menu" role="listbox" ref={(el) => this.listboxRef = el} id={`${htmlId}-listbox`}>
{filteredOptions.map((option, i) => {
return (
<div
class={{ 'option-current': this.activeIndex === i, 'combo-option': true }}
id={`${this.htmlId}-${i}`}
aria-selected={this.activeIndex === i ? 'true' : false}
ref={(el) => {if (this.activeIndex === i) this.activeOptionRef = el; }}
role="option"
onClick={() => { this.onOptionClick(i); }}
onMouseDown={this.onOptionMouseDown.bind(this)}
>{option.name}</div>
);
})}
</div>
</div>
]);
}
private onInput() {
const curValue = this.inputRef.value;
const matches = filterOptions(this.options, curValue);
// if we're filtering options, just need to set activeIndex to 0
if (this.filter) {
this.filteredOptions = [...matches];
this.activeIndex = 0;
}
// if not filtering options, set activeIndex to first matching option
// (or leave it alone, if the active option is already in the matching set)
else {
const filterCurrentOption = matches.filter((option) => option.name === this.options[this.activeIndex].name);
if (matches.length > 0 && !filterCurrentOption.length) {
this.activeIndex = this.options.indexOf(matches[0]);
}
}
if (this.value !== curValue) {
this.value = curValue;
}
const menuState = this.filteredOptions.length > 0;
if (this.open !== menuState) {
this.updateMenuState(menuState, false);
}
}
private onInputKeyDown(event: KeyboardEvent) {
const max = this.filteredOptions.length - 1;
const action = getActionFromKey(event, this.open);
switch(action) {
case MenuActions.Next:
case MenuActions.Last:
case MenuActions.First:
case MenuActions.Previous:
event.preventDefault();
return this.onOptionChange(getUpdatedIndex(this.activeIndex, max, action));
case MenuActions.CloseSelect:
event.preventDefault();
this.selectOption(this.activeIndex);
return this.updateMenuState(false);
case MenuActions.Close:
event.preventDefault();
this.activeIndex = 0;
this.value = this.selectedValue;
this.filteredOptions = this.options;
return this.updateMenuState(false);
case MenuActions.Open:
return this.updateMenuState(true);
}
}
private onInputBlur() {
if (this.ignoreBlur) {
this.ignoreBlur = false;
return;
}
if (this.open) {
this.selectOption(this.activeIndex);
this.updateMenuState(false, false);
}
}
private onOptionChange(index: number) {
this.activeIndex = index;
}
private onOptionClick(index: number) {
this.onOptionChange(index);
this.selectOption(index);
this.updateMenuState(false);
}
private onOptionMouseDown() {
this.ignoreBlur = true;
}
private selectOption(index: number) {
const selected = this.filteredOptions[index];
this.value = selected.name;
this.selectedValue = selected.name;
this.activeIndex = 0;
if (this.filter) {
this.filteredOptions = filterOptions(this.options, this.value);
}
this.selectEvent.emit(selected);
}
private updateMenuState(open: boolean, callFocus = true) {
this.open = open;
callFocus && this.inputRef.focus();
}
}