@dile/ui 3.20.1 → 3.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/components/chip/src/DileChip.js +48 -7
- package/components/input-suggestions/index.js +3 -0
- package/components/input-suggestions/input-suggestions.js +5 -0
- package/components/input-suggestions/src/DileInputSuggestions.js +46 -0
- package/components/input-suggestions/src/DileInputSuggestionsBase.js +516 -0
- package/components/input-suggestions/src/normalize.js +6 -0
- package/components/input-tags/index.js +2 -0
- package/components/input-tags/input-tags.js +5 -0
- package/components/input-tags/src/DileInputTags.js +11 -0
- package/components/input-tags/src/DileInputTagsMixin.js +288 -0
- package/components/pages/src/DilePages.js +76 -0
- package/components/select/color-select.js +5 -0
- package/components/select/index.js +1 -0
- package/components/select/src/DileColorSelect.js +104 -0
- package/components/select/src/DileSelectOverlay.js +28 -4
- package/components/sortable/index.js +1 -0
- package/components/sortable/sortable.js +5 -0
- package/components/sortable/src/DileSortable.js +290 -0
- package/components/tabs/src/DileTab.js +21 -0
- package/package.json +2 -2
|
@@ -19,22 +19,41 @@ export class DileChip extends LitElement {
|
|
|
19
19
|
padding-bottom: var(--dile-chip-padding-y, 0.3rem);
|
|
20
20
|
border-radius: var(--dile-chip-border-radius, 0);
|
|
21
21
|
}
|
|
22
|
-
|
|
22
|
+
.text {
|
|
23
23
|
font-size: var(--dile-chip-font-size, 1rem);
|
|
24
24
|
font-weight: var(--dile-chip-font-weight, bold);
|
|
25
25
|
color: var(--dile-chip-text-color, #303030);
|
|
26
26
|
display: inline-block;
|
|
27
27
|
margin-right: 20px;
|
|
28
|
-
transition: color 0.3s ease;
|
|
29
28
|
}
|
|
30
|
-
|
|
29
|
+
button {
|
|
30
|
+
all: unset;
|
|
31
|
+
box-sizing: border-box;
|
|
32
|
+
display: inline-flex;
|
|
33
|
+
align-items: center;
|
|
34
|
+
justify-content: center;
|
|
35
|
+
/* Enlarge the hit area (WCAG 2.5.8) without changing the visual size */
|
|
36
|
+
padding: 4px;
|
|
37
|
+
margin: -4px;
|
|
38
|
+
min-width: 24px;
|
|
39
|
+
min-height: 24px;
|
|
40
|
+
border-radius: 4px;
|
|
41
|
+
cursor: pointer;
|
|
31
42
|
--dile-icon-color: var(--dile-chip-icon-color, #303030);
|
|
32
43
|
--dile-icon-size: var(--dile-chip-icon-size, 16px);
|
|
33
|
-
|
|
44
|
+
-webkit-tap-highlight-color: transparent;
|
|
34
45
|
}
|
|
35
|
-
|
|
46
|
+
button:hover,
|
|
47
|
+
button:focus-visible {
|
|
36
48
|
--dile-icon-color: var(--dile-chip-icon-hover-color, #d06060);
|
|
37
49
|
}
|
|
50
|
+
button:focus-visible {
|
|
51
|
+
outline: 2px solid var(--dile-chip-focus-outline-color, var(--dile-primary-color, #1a73e8));
|
|
52
|
+
outline-offset: 0;
|
|
53
|
+
}
|
|
54
|
+
.action-label {
|
|
55
|
+
display: none;
|
|
56
|
+
}
|
|
38
57
|
`;
|
|
39
58
|
}
|
|
40
59
|
|
|
@@ -42,23 +61,45 @@ export class DileChip extends LitElement {
|
|
|
42
61
|
return {
|
|
43
62
|
name: { type: String },
|
|
44
63
|
icon: { type: Object },
|
|
64
|
+
iconLabel: { type: String, attribute: 'icon-label' },
|
|
45
65
|
};
|
|
46
66
|
}
|
|
47
67
|
|
|
48
68
|
constructor() {
|
|
49
69
|
super();
|
|
50
70
|
this.icon = clearIcon;
|
|
71
|
+
this.iconLabel = 'Remove';
|
|
51
72
|
}
|
|
52
73
|
|
|
53
74
|
render() {
|
|
54
75
|
return html`
|
|
55
76
|
<div>
|
|
56
|
-
<span><slot></slot></span>
|
|
57
|
-
<
|
|
77
|
+
<span class="text" id="text"><slot></slot></span>
|
|
78
|
+
<span class="action-label" id="action">${this.iconLabel}</span>
|
|
79
|
+
<button
|
|
80
|
+
type="button"
|
|
81
|
+
part="button"
|
|
82
|
+
aria-labelledby="action text"
|
|
83
|
+
@click="${this.doClick}"
|
|
84
|
+
@keydown="${this.onKeyDown}"
|
|
85
|
+
>
|
|
86
|
+
<dile-icon .icon="${this.icon}" aria-hidden="true"></dile-icon>
|
|
87
|
+
</button>
|
|
58
88
|
</div>
|
|
59
89
|
`;
|
|
60
90
|
}
|
|
61
91
|
|
|
92
|
+
onKeyDown(e) {
|
|
93
|
+
if (e.key === 'Delete' || e.key === 'Backspace') {
|
|
94
|
+
e.preventDefault();
|
|
95
|
+
this.doClick();
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
focus(options) {
|
|
100
|
+
this.shadowRoot?.querySelector('button')?.focus(options);
|
|
101
|
+
}
|
|
102
|
+
|
|
62
103
|
doClick() {
|
|
63
104
|
this.dispatchEvent(new CustomEvent('dile-chip-icon-click', {
|
|
64
105
|
bubbles: true,
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { DileInputSuggestionsBase } from './DileInputSuggestionsBase.js';
|
|
2
|
+
import { normalize } from './normalize.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* # dile-input-suggestions
|
|
6
|
+
*
|
|
7
|
+
* Free text field with suggestions taken from the `suggestions` property, which can be:
|
|
8
|
+
* - an array of objects: filtered by `displayProperty`, ignoring case and accents;
|
|
9
|
+
* - a function `(keyword) => array | Promise<array>` that decides what to return.
|
|
10
|
+
*
|
|
11
|
+
* See DileInputSuggestionsBase for the rest of the API. The Ajax variant,
|
|
12
|
+
* `dile-ajax-input-suggestions`, lives in `@dile/crud`.
|
|
13
|
+
*/
|
|
14
|
+
export class DileInputSuggestions extends DileInputSuggestionsBase {
|
|
15
|
+
static get properties() {
|
|
16
|
+
return {
|
|
17
|
+
suggestions: { type: Object },
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
constructor() {
|
|
22
|
+
super();
|
|
23
|
+
this.suggestions = [];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async loadData() {
|
|
27
|
+
const requestId = this._requestId;
|
|
28
|
+
const keyword = this.keyword;
|
|
29
|
+
try {
|
|
30
|
+
const result =
|
|
31
|
+
typeof this.suggestions === 'function'
|
|
32
|
+
? await this.suggestions(keyword)
|
|
33
|
+
: this.filterSuggestions(this.suggestions, keyword);
|
|
34
|
+
this.setResults(result, requestId);
|
|
35
|
+
} catch {
|
|
36
|
+
this.setResults([], requestId);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
filterSuggestions(list, keyword) {
|
|
41
|
+
const needle = normalize(keyword);
|
|
42
|
+
return (Array.isArray(list) ? list : []).filter((item) =>
|
|
43
|
+
normalize(item?.[this.displayProperty]).includes(needle),
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,516 @@
|
|
|
1
|
+
import { LitElement, html, css, nothing } from 'lit';
|
|
2
|
+
import { ifDefined } from 'lit/directives/if-defined.js';
|
|
3
|
+
import { DileEmmitChange } from '../../../mixins/form/index.js';
|
|
4
|
+
import { labelStyles, messageStyles } from '../../input/index.js';
|
|
5
|
+
import '../../select/select-overlay.js';
|
|
6
|
+
import '../../spinner/spinner-icon.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Keys forwarded to the nested dile-select-overlay. Unlike DileSelectSearchableBase's
|
|
10
|
+
* `NAVIGATION_KEYS`, Enter and Escape are handled here: in a free text field the list must
|
|
11
|
+
* not open, nor anything be selected, when no suggestions are visible. Space is never
|
|
12
|
+
* forwarded, so it can be typed.
|
|
13
|
+
*/
|
|
14
|
+
const FORWARDED_KEYS = ['ArrowDown', 'ArrowUp', 'Home', 'End', 'Tab'];
|
|
15
|
+
|
|
16
|
+
/** Option parts of the nested dile-select-overlay re-exported by the host. */
|
|
17
|
+
const OPTION_PARTS = ['option', 'option-highlighted', 'option-selected', 'option-content'];
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* # DileInputSuggestionsBase
|
|
21
|
+
*
|
|
22
|
+
* Free text field with suggestions (editable combobox, WAI-ARIA APG "list autocomplete"
|
|
23
|
+
* pattern). The field value is always the typed text. Choosing a suggestion copies its
|
|
24
|
+
* `displayProperty` into the field and emits `dile-input-suggestions-selected` with the whole
|
|
25
|
+
* object, so the consumer decides what to do with it.
|
|
26
|
+
*
|
|
27
|
+
* Same composition as DileSelectSearchableBase: its own input plus a nested
|
|
28
|
+
* dile-select-overlay with `hideTrigger`, anchored to the input with `setTriggerElement()`
|
|
29
|
+
* and driven with `handleExternalKeydown()`.
|
|
30
|
+
*
|
|
31
|
+
* Subclasses implement `loadData()`, which gets the suggestions for `keyword` and calls
|
|
32
|
+
* `setResults(data)`.
|
|
33
|
+
*
|
|
34
|
+
* ## Extension points for variants (e.g. dile-input-tags)
|
|
35
|
+
* - `renderBeforeField()`: content between the label and the field.
|
|
36
|
+
* - `renderFieldEnd()`: content inside the field border, after the `suffix` slot.
|
|
37
|
+
* - `filterResults(data)`: filters the received suggestions before showing them.
|
|
38
|
+
* - `onEnterWithoutSuggestion(e)`: Enter with no suggestion highlighted with the keyboard.
|
|
39
|
+
* By default, if the list is open, it closes it.
|
|
40
|
+
* - `selectItem(item)`: what to do when a suggestion is chosen.
|
|
41
|
+
*
|
|
42
|
+
* ## Templated options
|
|
43
|
+
* - `optionRenderer(item, index)` renders each suggestion with a custom template. The text
|
|
44
|
+
* from `getOptionText` / `displayProperty` is still the accessible name and the announced text.
|
|
45
|
+
* - The `option`, `option-highlighted`, `option-selected` and `option-content` parts, plus the
|
|
46
|
+
* ones in `optionParts`, can be styled from outside with `::part()`.
|
|
47
|
+
*
|
|
48
|
+
* ## Slots
|
|
49
|
+
* - `prefix` / `suffix`: content inside the field border, before or after the text
|
|
50
|
+
* (e.g. an icon button).
|
|
51
|
+
*
|
|
52
|
+
* ## CSS custom properties
|
|
53
|
+
* - `--dile-input-suggestions-min-width` (16rem): minimum width of the suggestions list when
|
|
54
|
+
* the field is narrower. Never wider than the viewport.
|
|
55
|
+
* - `--dile-input-suggestions-margin-bottom` (10px).
|
|
56
|
+
* - Plus the usual `--dile-input-*` of the field and `--dile-select-overlay-*` of the list.
|
|
57
|
+
*
|
|
58
|
+
* ## Events
|
|
59
|
+
* - `element-changed`: when the text changes (`detail: { name, value }`).
|
|
60
|
+
* - `dile-input-suggestions-selected`: when a suggestion is chosen (`detail: { name, item }`).
|
|
61
|
+
*/
|
|
62
|
+
export class DileInputSuggestionsBase extends DileEmmitChange(LitElement) {
|
|
63
|
+
static get styles() {
|
|
64
|
+
return [
|
|
65
|
+
labelStyles,
|
|
66
|
+
messageStyles,
|
|
67
|
+
css`
|
|
68
|
+
* {
|
|
69
|
+
box-sizing: border-box;
|
|
70
|
+
}
|
|
71
|
+
:host {
|
|
72
|
+
display: block;
|
|
73
|
+
position: relative;
|
|
74
|
+
margin-bottom: var(--dile-input-suggestions-margin-bottom, 10px);
|
|
75
|
+
}
|
|
76
|
+
.wrapper {
|
|
77
|
+
display: flex;
|
|
78
|
+
align-items: center;
|
|
79
|
+
gap: 4px;
|
|
80
|
+
width: var(--dile-input-width, 100%);
|
|
81
|
+
padding: var(--dile-input-padding, 5px);
|
|
82
|
+
border-radius: var(--dile-input-border-radius, 5px);
|
|
83
|
+
border: var(--dile-input-border-width, 1px) solid var(--dile-input-border-color, #888);
|
|
84
|
+
background-color: var(--dile-input-background-color, #fff);
|
|
85
|
+
}
|
|
86
|
+
.wrapper:focus-within {
|
|
87
|
+
border-color: var(--dile-input-focus-border-color, var(--dile-link-color, #6af));
|
|
88
|
+
}
|
|
89
|
+
.wrapper.errored {
|
|
90
|
+
border-color: var(--dile-input-error-border-color, #c00);
|
|
91
|
+
}
|
|
92
|
+
.wrapper.disabled {
|
|
93
|
+
opacity: 0.5;
|
|
94
|
+
}
|
|
95
|
+
input {
|
|
96
|
+
flex-grow: 1;
|
|
97
|
+
min-width: 0;
|
|
98
|
+
border: none;
|
|
99
|
+
outline: none;
|
|
100
|
+
padding: 0;
|
|
101
|
+
color: var(--dile-input-color, #303030);
|
|
102
|
+
font-size: var(--dile-input-font-size, 1em);
|
|
103
|
+
line-height: var(--dile-input-line-height, 1.5em);
|
|
104
|
+
background-color: transparent;
|
|
105
|
+
font-family: inherit;
|
|
106
|
+
}
|
|
107
|
+
input::placeholder {
|
|
108
|
+
color: var(--dile-input-placeholder-color, #ccc);
|
|
109
|
+
}
|
|
110
|
+
dile-spinner-icon {
|
|
111
|
+
--dile-icon-size: 16px;
|
|
112
|
+
display: flex;
|
|
113
|
+
}
|
|
114
|
+
::slotted(*) {
|
|
115
|
+
flex-shrink: 0;
|
|
116
|
+
}
|
|
117
|
+
dile-select-overlay {
|
|
118
|
+
margin-bottom: 0;
|
|
119
|
+
/* The list copies the field width, with a minimum so the suggestions stay readable
|
|
120
|
+
in narrow fields. dile-select-overlay caps it to the viewport width and moves it
|
|
121
|
+
back if it overflows on the right. */
|
|
122
|
+
--dile-select-overlay-min-width: var(--dile-input-suggestions-min-width, 16rem);
|
|
123
|
+
}
|
|
124
|
+
.visually-hidden {
|
|
125
|
+
position: absolute;
|
|
126
|
+
width: 1px;
|
|
127
|
+
height: 1px;
|
|
128
|
+
padding: 0;
|
|
129
|
+
margin: -1px;
|
|
130
|
+
overflow: hidden;
|
|
131
|
+
clip: rect(0, 0, 0, 0);
|
|
132
|
+
white-space: nowrap;
|
|
133
|
+
border: 0;
|
|
134
|
+
}
|
|
135
|
+
`,
|
|
136
|
+
];
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
static get formAssociated() {
|
|
140
|
+
return true;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
static get properties() {
|
|
144
|
+
return {
|
|
145
|
+
value: { type: String },
|
|
146
|
+
name: { type: String, reflect: true },
|
|
147
|
+
label: { type: String },
|
|
148
|
+
/** Accessible name when there is no visible `label`. */
|
|
149
|
+
accessibleLabel: { type: String },
|
|
150
|
+
placeholder: { type: String },
|
|
151
|
+
disabled: { type: Boolean },
|
|
152
|
+
errored: { type: Boolean },
|
|
153
|
+
message: { type: String },
|
|
154
|
+
hideErrorOnInput: { type: Boolean },
|
|
155
|
+
/** Minimum number of characters to request suggestions. */
|
|
156
|
+
minLength: { type: Number },
|
|
157
|
+
/** Milliseconds to wait after the last keystroke before requesting suggestions. */
|
|
158
|
+
delay: { type: Number },
|
|
159
|
+
/** Property of the suggested object copied into the field when it is chosen. */
|
|
160
|
+
displayProperty: { type: String },
|
|
161
|
+
/**
|
|
162
|
+
* Optional `(item) => string` function with the text of each suggestion. It is also the
|
|
163
|
+
* accessible name and the announced text, even with `optionRenderer`.
|
|
164
|
+
*/
|
|
165
|
+
getOptionText: { type: Object },
|
|
166
|
+
/**
|
|
167
|
+
* Optional renderer for the content of each suggestion: `(item, index) => TemplateResult | string`,
|
|
168
|
+
* with the original data object. Rendered inside the nested dile-select-overlay.
|
|
169
|
+
*/
|
|
170
|
+
optionRenderer: { attribute: false },
|
|
171
|
+
/**
|
|
172
|
+
* Extra part names, separated by spaces or commas, used inside `optionRenderer`.
|
|
173
|
+
* They are re-exported so they can be styled from outside with ::part().
|
|
174
|
+
*/
|
|
175
|
+
optionParts: { type: String },
|
|
176
|
+
/** Text announced when there are suggestions. Receives the number of results. */
|
|
177
|
+
resultsAnnouncement: { type: Object },
|
|
178
|
+
data: { type: Array },
|
|
179
|
+
keyword: { type: String, state: true },
|
|
180
|
+
loading: { type: Boolean, state: true },
|
|
181
|
+
_liveAnnouncement: { state: true },
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
constructor() {
|
|
186
|
+
super();
|
|
187
|
+
this.value = '';
|
|
188
|
+
this.placeholder = '';
|
|
189
|
+
this.disabled = false;
|
|
190
|
+
this.errored = false;
|
|
191
|
+
this.message = '';
|
|
192
|
+
this.minLength = 2;
|
|
193
|
+
this.delay = 300;
|
|
194
|
+
this.displayProperty = 'name';
|
|
195
|
+
this.data = [];
|
|
196
|
+
this.keyword = '';
|
|
197
|
+
this.loading = false;
|
|
198
|
+
this.resultsAnnouncement = (count) =>
|
|
199
|
+
count === 1 ? '1 suggestion available' : `${count} suggestions available`;
|
|
200
|
+
this._liveAnnouncement = '';
|
|
201
|
+
this._debounceTimeout = null;
|
|
202
|
+
this._requestId = 0;
|
|
203
|
+
this._keyboardHighlight = false;
|
|
204
|
+
this.internals = this.attachInternals();
|
|
205
|
+
this.inputId = `${this.localName}-${Math.random().toString(36).slice(2, 11)}`;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** Template method: gets the suggestions for `keyword` and calls `setResults(data)`. */
|
|
209
|
+
loadData() {}
|
|
210
|
+
|
|
211
|
+
/** Extension point: content between the label and the field. */
|
|
212
|
+
renderBeforeField() {
|
|
213
|
+
return nothing;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** Extension point: content inside the field border, at the end. */
|
|
217
|
+
renderFieldEnd() {
|
|
218
|
+
return nothing;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** Extension point: filters the received suggestions before showing them. */
|
|
222
|
+
filterResults(data) {
|
|
223
|
+
return data;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
get input() {
|
|
227
|
+
return this.shadowRoot.getElementById('input');
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
get select() {
|
|
231
|
+
return this.shadowRoot.getElementById('elselect');
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
get opened() {
|
|
235
|
+
return this.select ? this.select.opened : false;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
firstUpdated() {
|
|
239
|
+
this.select.updateComplete.then(() => {
|
|
240
|
+
this.select.setTriggerElement(this.shadowRoot.querySelector('.wrapper'));
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
updated(changedProperties) {
|
|
245
|
+
if (changedProperties.has('value')) {
|
|
246
|
+
this.emmitChange();
|
|
247
|
+
this.internals.setFormValue(this.value ?? '');
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
disconnectedCallback() {
|
|
252
|
+
super.disconnectedCallback();
|
|
253
|
+
clearTimeout(this._debounceTimeout);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
render() {
|
|
257
|
+
const messageId = this.message ? `${this.inputId}-message` : undefined;
|
|
258
|
+
return html`
|
|
259
|
+
<div>
|
|
260
|
+
${this.label ? html`<label for="input">${this.label}</label>` : nothing} ${this.renderBeforeField()}
|
|
261
|
+
<div
|
|
262
|
+
class="wrapper ${this.errored ? 'errored' : ''} ${this.disabled ? 'disabled' : ''}"
|
|
263
|
+
@click=${(e) => e.stopPropagation()}
|
|
264
|
+
>
|
|
265
|
+
<slot name="prefix"></slot>
|
|
266
|
+
<input
|
|
267
|
+
type="text"
|
|
268
|
+
id="input"
|
|
269
|
+
role="combobox"
|
|
270
|
+
aria-autocomplete="list"
|
|
271
|
+
aria-haspopup="listbox"
|
|
272
|
+
aria-expanded="${this.opened}"
|
|
273
|
+
aria-label="${ifDefined(this.label ? undefined : this.accessibleLabel || this.placeholder || undefined)}"
|
|
274
|
+
aria-invalid="${this.errored}"
|
|
275
|
+
aria-describedby="${ifDefined(messageId)}"
|
|
276
|
+
placeholder="${this.placeholder}"
|
|
277
|
+
.value="${this.value ?? ''}"
|
|
278
|
+
?disabled=${this.disabled}
|
|
279
|
+
autocomplete="off"
|
|
280
|
+
@input=${this.onInput}
|
|
281
|
+
@keydown=${this.onKeydown}
|
|
282
|
+
@focus=${this.onFocus}
|
|
283
|
+
@focusout=${this.onFocusOut}
|
|
284
|
+
/>
|
|
285
|
+
${this.loading ? html`<dile-spinner-icon active aria-hidden="true"></dile-spinner-icon>` : nothing}
|
|
286
|
+
<slot name="suffix"></slot>
|
|
287
|
+
${this.renderFieldEnd()}
|
|
288
|
+
</div>
|
|
289
|
+
<div class="visually-hidden" role="status" aria-live="polite">${this._liveAnnouncement}</div>
|
|
290
|
+
<div @mousedown=${this.keepFocus} @pointerdown=${this.keepFocus}>
|
|
291
|
+
<dile-select-overlay
|
|
292
|
+
id="elselect"
|
|
293
|
+
hideTrigger
|
|
294
|
+
name="generated-select-field"
|
|
295
|
+
accessibleLabel="${ifDefined(this.label || this.accessibleLabel || this.placeholder || undefined)}"
|
|
296
|
+
exportparts="${this.exportedOptionParts}"
|
|
297
|
+
.optionRenderer=${this.nestedOptionRenderer}
|
|
298
|
+
?disabled=${this.disabled}
|
|
299
|
+
@element-changed=${this.onOptionSelected}
|
|
300
|
+
>
|
|
301
|
+
<select slot="select">
|
|
302
|
+
${this.data.map(
|
|
303
|
+
(item, index) => html`<option value="${index}">${this.optionText(item)}</option>`,
|
|
304
|
+
)}
|
|
305
|
+
</select>
|
|
306
|
+
</dile-select-overlay>
|
|
307
|
+
</div>
|
|
308
|
+
</div>
|
|
309
|
+
${
|
|
310
|
+
this.message
|
|
311
|
+
? html`<div class="message ${this.errored ? 'errored-msg' : ''}" id="${messageId}">
|
|
312
|
+
<span>${this.message}</span>
|
|
313
|
+
</div>`
|
|
314
|
+
: nothing
|
|
315
|
+
}
|
|
316
|
+
`;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/** exportparts value of the nested overlay: the option parts plus `optionParts`. */
|
|
320
|
+
get exportedOptionParts() {
|
|
321
|
+
const extra = (this.optionParts || '').split(/[\s,]+/).filter(Boolean);
|
|
322
|
+
return [...new Set([...OPTION_PARTS, ...extra])].join(', ');
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Adapts `optionRenderer`, which receives data objects, to the overlay's `(option, index)`
|
|
327
|
+
* signature. Options are generated from `data` in order and their value is the index. Cached
|
|
328
|
+
* per function so the overlay doesn't receive a new function on every render.
|
|
329
|
+
*/
|
|
330
|
+
get nestedOptionRenderer() {
|
|
331
|
+
if (typeof this.optionRenderer !== 'function') {
|
|
332
|
+
return undefined;
|
|
333
|
+
}
|
|
334
|
+
if (this._nestedRendererFor !== this.optionRenderer) {
|
|
335
|
+
const renderer = this.optionRenderer;
|
|
336
|
+
this._nestedRendererFor = renderer;
|
|
337
|
+
this._nestedRenderer = (option, index) => {
|
|
338
|
+
const item = this.data[Number(option.value)];
|
|
339
|
+
return item === undefined ? option.text : renderer(item, index);
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
return this._nestedRenderer;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
optionText(item) {
|
|
346
|
+
if (typeof this.getOptionText === 'function') {
|
|
347
|
+
return this.getOptionText(item);
|
|
348
|
+
}
|
|
349
|
+
return item?.[this.displayProperty] ?? '';
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/** Prevents pressing a suggestion from blurring the input and closing the list before the click. */
|
|
353
|
+
keepFocus(e) {
|
|
354
|
+
e.preventDefault();
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
onInput(e) {
|
|
358
|
+
this.value = e.target.value;
|
|
359
|
+
this.keyword = this.value.trim();
|
|
360
|
+
if (this.hideErrorOnInput && this.errored) {
|
|
361
|
+
this.clearError();
|
|
362
|
+
}
|
|
363
|
+
clearTimeout(this._debounceTimeout);
|
|
364
|
+
if (this.keyword.length < this.minLength) {
|
|
365
|
+
this._requestId++;
|
|
366
|
+
this.loading = false;
|
|
367
|
+
this.setResults([]);
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
this._debounceTimeout = setTimeout(() => this.requestData(), this.delay);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
requestData() {
|
|
374
|
+
this._requestId++;
|
|
375
|
+
this.loading = true;
|
|
376
|
+
this.loadData();
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* Subclasses can pass the request id, taken from `this._requestId` when the request
|
|
381
|
+
* started, so late responses are discarded.
|
|
382
|
+
*/
|
|
383
|
+
setResults(data, requestId = this._requestId) {
|
|
384
|
+
if (requestId !== this._requestId) return;
|
|
385
|
+
this.data = this.filterResults(Array.isArray(data) ? data : []);
|
|
386
|
+
this.loading = false;
|
|
387
|
+
this._keyboardHighlight = false;
|
|
388
|
+
this.updateComplete.then(() => {
|
|
389
|
+
// Closing before opening resets the highlighted option, which could otherwise keep
|
|
390
|
+
// pointing to a position of the previous list.
|
|
391
|
+
this.select?.close();
|
|
392
|
+
if (this.data.length > 0 && this.hasFocus) {
|
|
393
|
+
this.select?.open();
|
|
394
|
+
this._liveAnnouncement = this.resultsAnnouncement(this.data.length);
|
|
395
|
+
} else {
|
|
396
|
+
this._liveAnnouncement = '';
|
|
397
|
+
}
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
get hasFocus() {
|
|
402
|
+
return this.shadowRoot.activeElement === this.input;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
onKeydown(e) {
|
|
406
|
+
if (!this.select) return;
|
|
407
|
+
if (e.key === 'Escape') {
|
|
408
|
+
this._keyboardHighlight = false;
|
|
409
|
+
if (this.opened) {
|
|
410
|
+
e.preventDefault();
|
|
411
|
+
e.stopPropagation();
|
|
412
|
+
this.select.close();
|
|
413
|
+
}
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
if (e.key === 'Enter') {
|
|
417
|
+
// With a suggestion highlighted with the keyboard, the overlay selects it. Otherwise
|
|
418
|
+
// onEnterWithoutSuggestion decides. With the list closed it isn't forwarded: it must not open it.
|
|
419
|
+
if (this.opened && this._keyboardHighlight) {
|
|
420
|
+
this.select.handleExternalKeydown(e);
|
|
421
|
+
} else {
|
|
422
|
+
this.onEnterWithoutSuggestion(e);
|
|
423
|
+
}
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
if (!FORWARDED_KEYS.includes(e.key)) return;
|
|
427
|
+
if (e.key !== 'Tab' && this.data.length === 0) return;
|
|
428
|
+
this.select.handleExternalKeydown(e);
|
|
429
|
+
// Only keyboard highlights count for Enter: the overlay also highlights options on mouse
|
|
430
|
+
// hover, and Enter must not pick an option the user only pointed at.
|
|
431
|
+
this._keyboardHighlight = this.opened && this.select.highlightedText !== '';
|
|
432
|
+
this._liveAnnouncement = this.select.highlightedText;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* Enter with no suggestion highlighted with the keyboard. By default, with the list open,
|
|
437
|
+
* it is forwarded to the overlay, which closes it (or picks the option under the mouse).
|
|
438
|
+
*/
|
|
439
|
+
onEnterWithoutSuggestion(e) {
|
|
440
|
+
if (this.opened) {
|
|
441
|
+
this.select.handleExternalKeydown(e);
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
onFocus() {
|
|
446
|
+
if (this.data.length > 0 && this.keyword.length >= this.minLength) {
|
|
447
|
+
this.select?.open();
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
onFocusOut(e) {
|
|
452
|
+
if (!this.shadowRoot.contains(e.relatedTarget)) {
|
|
453
|
+
this._keyboardHighlight = false;
|
|
454
|
+
this.select?.close();
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
onOptionSelected(e) {
|
|
459
|
+
e.stopPropagation();
|
|
460
|
+
if (e.detail.name !== 'generated-select-field' || e.detail.value === '' || e.detail.value == null) {
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
const item = this.data[Number(e.detail.value)];
|
|
464
|
+
// Reset the nested select so the same suggestion can be chosen again later.
|
|
465
|
+
this.select.quietChange('');
|
|
466
|
+
if (!item) return;
|
|
467
|
+
this.selectItem(item);
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/** Applies a suggestion: copies its text into the field and emits the event with the object. */
|
|
471
|
+
selectItem(item) {
|
|
472
|
+
clearTimeout(this._debounceTimeout);
|
|
473
|
+
this._requestId++;
|
|
474
|
+
this.value = item[this.displayProperty] ?? '';
|
|
475
|
+
this.keyword = '';
|
|
476
|
+
this.data = [];
|
|
477
|
+
this.loading = false;
|
|
478
|
+
this._keyboardHighlight = false;
|
|
479
|
+
this.select?.close();
|
|
480
|
+
this._liveAnnouncement = '';
|
|
481
|
+
if (this.hideErrorOnInput && this.errored) {
|
|
482
|
+
this.clearError();
|
|
483
|
+
}
|
|
484
|
+
// Emitted after the element-changed of the new text, so a consumer listening to both can
|
|
485
|
+
// give priority to the chosen suggestion without the text change arriving later and
|
|
486
|
+
// overriding it.
|
|
487
|
+
this.updateComplete.then(() => {
|
|
488
|
+
this.dispatchEvent(
|
|
489
|
+
new CustomEvent('dile-input-suggestions-selected', {
|
|
490
|
+
bubbles: true,
|
|
491
|
+
composed: true,
|
|
492
|
+
detail: { name: this.name, item },
|
|
493
|
+
}),
|
|
494
|
+
);
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
focus(options) {
|
|
499
|
+
this.input?.focus(options);
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
clearError() {
|
|
503
|
+
this.errored = false;
|
|
504
|
+
this.message = '';
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
clear() {
|
|
508
|
+
this.value = '';
|
|
509
|
+
this.keyword = '';
|
|
510
|
+
this.data = [];
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
set(value) {
|
|
514
|
+
this.value = value ?? '';
|
|
515
|
+
}
|
|
516
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { DileInputSuggestions } from '../../input-suggestions/src/DileInputSuggestions.js';
|
|
2
|
+
import { DileInputTagsMixin } from './DileInputTagsMixin.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* # dile-input-tags
|
|
6
|
+
*
|
|
7
|
+
* Tags editor with local suggestions (the `suggestions` property, like
|
|
8
|
+
* dile-input-suggestions). See DileInputTagsMixin. The Ajax variant,
|
|
9
|
+
* `dile-ajax-input-tags`, lives in `@dile/crud`.
|
|
10
|
+
*/
|
|
11
|
+
export class DileInputTags extends DileInputTagsMixin(DileInputSuggestions) {}
|