-
Notifications
You must be signed in to change notification settings - Fork 4
/
autocomplete-input-text.js
243 lines (218 loc) · 7.02 KB
/
autocomplete-input-text.js
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
import '@brightspace-ui/core/components/inputs/input-text.js';
import '@brightspace-ui/core/components/dropdown/dropdown-content.js';
import '@brightspace-ui/core/components/dropdown/dropdown.js';
import '@brightspace-ui/core/components/colors/colors.js';
import { css, html, LitElement } from 'lit';
import { bodyCompactStyles } from '@brightspace-ui/core/components/typography/styles.js';
import { getUniqueId } from '@brightspace-ui/core/helpers/uniqueId.js';
import { ifDefined } from 'lit/directives/if-defined.js';
const KeyCodes = { UP: 38, DOWN: 40, ENTER: 13, ESCAPE: 27, HOME: 36, END: 35 };
class AutocompleteInputText extends LitElement {
static get properties() {
return {
/**
* These properties are used by d2l-labs-autocomplete
*/
data: { type:Object },
dropdownLabel: { type: String, attribute: 'dropdown-label' },
maxLength: { type: String, attribute: 'max-length' },
minLength: { type: String, attribute: 'min-length' },
remoteSource: { type: Boolean, attribute: 'remote-source' },
showOnFocus: { type: Boolean, attribute: 'show-on-focus' },
_showSuggestions: { state: true },
_minWidth: { state: true },
_filter: { state: true },
_suggestions: { state: true }
};
}
static get styles() {
return [bodyCompactStyles, css`
:host {
display: flex;
}
:host([hidden]) {
display: none;
}
#d2l-labs-autocomplete-list {
margin: 0;
padding: 0;
}
#d2l-labs-autocomplete-dropdown-label {
font-size: 0.7rem;
padding: 0.4rem 0.7rem;
}
.d2l-labs-autocomplete-suggestion {
cursor: pointer;
margin-right: 0.1rem;
outline: none;
overflow-x: hidden;
padding: 0.4rem 0.7rem;
text-align: left;
text-overflow: ellipsis;
white-space: nowrap;
}
.d2l-labs-autocomplete-suggestion-highlighted {
font-weight: bold;
}
.d2l-labs-autocomplete-suggestion:focus,
.d2l-labs-autocomplete-suggestion:hover {
background-color: var(--d2l-color-celestine-plus-2);
}
`];
}
constructor() {
super();
this.data = [];
this.dropdownLabel = null;
this.minLength = 1;
this.remoteSource = false;
this.showOnFocus = false;
this._filter = '';
this._showSuggestions = false;
this._suggestions = [];
this._uniqueId = getUniqueId();
}
render() {
return html`<d2l-dropdown
@d2l-dropdown-close=${this._onDropdownClose}
class="d2l-labs-autocomplete-dropdown-wrapper"
no-auto-open>
<div class="d2l-dropdown-opener">
<d2l-input-text
aria-label="${this.getAttribute('aria-label')}"
@input=${this._onInput}
@keydown=${this._onKeyDown}
@focus=${this._onFocus}
maxlength="${ifDefined(this.maxLength)}"
novalidate
autocomplete="list"
role="combobox">
</d2l-input-text>
</div>
<d2l-dropdown-content
id="d2l-labs-autocomplete-dropdown-content"
no-auto-focus
max-height="${this.maxHeight}"
min-width="${this._minWidth}"
no-padding=""
no-pointer=""
vertical-offset="5"
align="start"
?opened=${this._showSuggestions}
>
<div ?hidden="${!this.dropdownLabel}" id="d2l-labs-autocomplete-dropdown-label">${this.dropdownLabel}</div>
<ul id="d2l-labs-autocomplete-list" role="listbox">
${this._suggestions.map((item, index) => html`
<li
aria-label="${item.value}"
data-index=${index}
class="d2l-labs-autocomplete-suggestion d2l-body-compact"
@click=${this._onSuggestionSelected}
role="option"
tabindex="0"
@keydown=${this._onKeyDown}
>
${this._computeText(item.value)}
</li>
`)}
</ul>
</d2l-dropdown-content>
</d2l-dropdown>`;
}
willUpdate(changedProperties) {
super.willUpdate(changedProperties);
if (changedProperties.has('_filter')) this._filterChanged();
if (changedProperties.has('_filter') || changedProperties.has('minLength') || changedProperties.has('_suggestions'))
this._showSuggestions = this._suggestions.length > 0 && this._filter.length >= this.minLength;
}
setSuggestions(suggestions) {
this._suggestions = suggestions;
}
get _input() {
return this.shadowRoot.querySelector('d2l-input-text');
}
_computeText(text) {
const filter = this._filter;
const indexOfFilter = text.toLowerCase().indexOf(filter.toLowerCase());
if (indexOfFilter === -1) return html`${text}`;
const indexOfFilterEnd = indexOfFilter + filter.length;
const prefix = text.slice(0, indexOfFilter);
const bolded = text.slice(indexOfFilter, indexOfFilterEnd);
const suffix = text.slice(indexOfFilterEnd);
return html`${prefix}<span class="d2l-labs-autocomplete-suggestion-highlighted">${bolded}</span>${suffix}`;
}
_filterChanged() {
const filter = this._filter;
if (this.remoteSource) {
if (filter.length >= this.minLength || filter.length === 0) {
// Fire event for parent component to handle suggestions
this.dispatchEvent(new CustomEvent(
'd2l-labs-autocomplete-filter-change',
{ bubbles: true, composed: true, detail: { value: filter } }
));
}
} else {
this.setSuggestions(
filter.length < this.minLength ? [] : this.data.filter(({ value }) => {
return value.toLowerCase().indexOf(filter.toLowerCase()) === 0;
})
);
}
}
_focusDropdownIndex(index) {
this.shadowRoot.querySelectorAll('.d2l-labs-autocomplete-suggestion')[index]?.focus();
}
_onDropdownClose() {
this._showSuggestions = false;
}
_onFocus() {
this._minWidth = this._input.offsetWidth;
if (this.showOnFocus) this._showSuggestions = this._suggestions.length > 0;
}
_onInput() {
clearTimeout(this._inputDebouncer);
this._inputDebouncer = setTimeout(() => this._filter = this._input.value, 250);
}
_onKeyDown(event) {
const { UP, DOWN, ENTER, ESCAPE, HOME, END } = KeyCodes;
const { keyCode } = event;
const currentIndex = Number(event.currentTarget.dataset.index ?? -1);
if (keyCode === UP) {
currentIndex - 1 >= 0
? this._focusDropdownIndex(currentIndex - 1)
: this._focusDropdownIndex(this._suggestions.length - 1);
} else if (keyCode === DOWN) {
currentIndex + 1 < this._suggestions.length
? this._focusDropdownIndex(currentIndex + 1)
: this._focusDropdownIndex(0);
} else if (keyCode === HOME) {
this._focusDropdownIndex(0);
} else if (keyCode === END) {
this._focusDropdownIndex(this._suggestions.length - 1);
} else if (keyCode === ENTER && currentIndex >= 0) {
this._selectDropdownIndex(currentIndex);
} else if (keyCode === ESCAPE) {
this._input.value = '';
} else {
return;
}
event.preventDefault();
}
_onSuggestionSelected(event) {
this._selectDropdownIndex(Number(event.currentTarget.dataset.index));
}
_prefix(id) {
return `${this._uniqueId}-${id}`;
}
_selectDropdownIndex(index) {
const selection = this._suggestions[index].value;
this._input.value = selection;
this._showSuggestions = false;
this.dispatchEvent(new CustomEvent(
'd2l-labs-autocomplete-suggestion-selected',
{ bubbles: true, composed: true, detail: { value: selection } }
));
this._input.focus();
}
}
customElements.define('d2l-labs-autocomplete-input-text', AutocompleteInputText);