@xleddyl/443-components-vue 0.16.18 → 0.16.20
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/CATALOG.md +2 -0
- package/dist/components/forms/TreeSelect.vue +515 -0
- package/dist/components/forms/TreeSelect.vue.d.ts +488 -0
- package/dist/components/forms/index.d.ts +1 -0
- package/dist/components/forms/index.mjs +1 -0
- package/dist/components/overlays/ConfirmDialog.vue.d.ts +2 -2
- package/dist/components/overlays/ConfirmHost.vue.d.ts +2 -2
- package/dist/internal/overlays/OverlayView.vue.d.ts +2 -2
- package/dist/locale/de.mjs +7 -0
- package/dist/locale/en.mjs +7 -0
- package/dist/locale/it.mjs +7 -0
- package/dist/locale/types.d.ts +7 -0
- package/dist/styles/forms/combobox.css +1 -1
- package/dist/styles/forms/tree-select.css +261 -0
- package/dist/styles/forms.css +1 -0
- package/package.json +1 -1
package/CATALOG.md
CHANGED
|
@@ -46,6 +46,7 @@ Playground paths in this file are relative to `playground/nuxt/app/pages/compone
|
|
|
46
46
|
| One choice from a short list (5 or fewer) | `RadioGroup` or `SegmentedControl` |
|
|
47
47
|
| One choice from a longer list, no search | `Select` |
|
|
48
48
|
| Searchable select, async search, multi select, create option | `Combobox` |
|
|
49
|
+
| Pick one node of a hierarchy (category, folder) | `TreeSelect` (`leavesOnly` to allow only leaves) |
|
|
49
50
|
| Create new values with a format or rule (tags, codes) | `Combobox` with `creatable`, `createTransform`, `createValidate` |
|
|
50
51
|
| Several choices as checkboxes | `CheckboxGroup` |
|
|
51
52
|
| One yes or no | `Switch` (instant setting) or `Checkbox` (part of a form) |
|
|
@@ -167,6 +168,7 @@ All field components work inside `FormField` and take `invalid`, `disabled`, `re
|
|
|
167
168
|
| `SegmentedControl` | Compact toggle between 2 to 5 options (view mode, period). | Model: value (with `required` it is never null). Props: `options` (`label`, `value`, `icon`, `title`, `muted`), `iconOnly`, `block`, `label`. Slot: `option`. | `forms/segmented-control.vue` |
|
|
168
169
|
| `Select` | Native styled select. Use `Combobox` when search or async options are needed. | Model: `T or null`. Props: `options` (items or groups with `label`, `value`, `disabled`), `placeholder`, `icon`. Option values can be string, number or boolean. | `forms/select.vue` |
|
|
169
170
|
| `Combobox` | Searchable select. Local filter, async `search`, `multiple` with tags, `creatable`. | Models: `v-model`, `v-model:query`. Props: `options` (`label`, `value`, `description`, `icon`, `color` (dot and tag color), `group` (divider with the group name, also the tooltip of a selected tag), `keywords`), `search(query, signal)`, `filter`, `multiple`, `hideTags`, `creatable`, `createTransform`, `createValidate` (a returned string is the inline error), `debounce`, `minQueryLength`, `maxSelected`, `clearable`, `loading`, `placeholder`, `emptyText`. Slots: `option`, `empty`, `header` (top of the panel, for example quick filter pills). Events: `create`, `open`, `close`. | `forms/combobox.vue` |
|
|
171
|
+
| `TreeSelect` | Pick one node of a hierarchy (category, folder) with a drill-down panel, one level at a time. | Model: `TreeNodeId or null`. Props: `nodes` (`TreeNode`: `id`, `label`, `description`, `icon`, `disabled`, `children`), `leavesOnly` (parents only navigate), `separator` (path in the control and in search results, default `' / '`), `clearable`, `placeholder`, `searchPlaceholder`, `emptyText`, `icon`. Search lists every match with its path. Slots: `node` (`node`, `selected`, `path`), `empty`. Events: `open`, `close`. | `forms/tree-select.vue` |
|
|
170
172
|
| `Switch` | On or off setting that applies at once. | Model: boolean. Props: `label`, `description`, `labelPosition`, `checkedIcon`, `uncheckedIcon`, `name`. Slots: default, `description`. | `forms/switch.vue` |
|
|
171
173
|
| `NumberInput` | Locale aware number with stepper, min, max, step. | Model: number or null. Props: `min`, `max`, `step`, `largeStep`, `locale`, `formatOptions` (currency, percent), `stepper`, `wheel`, `align`, `icon`, `placeholder`. | `forms/number-input.vue` |
|
|
172
174
|
| `PasswordInput` | Password with show or hide, optional generator, copy and strength meter. | Model: string. Props: `generate` (true or a function), `copy`, `strength`, `minLength`, `autocomplete`. Event: `generate`. | `forms/password-input.vue` |
|
|
@@ -0,0 +1,515 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<div
|
|
3
|
+
ref="rootRef"
|
|
4
|
+
class="k-tree-select"
|
|
5
|
+
:class="[
|
|
6
|
+
`k-tree-select-${size}`,
|
|
7
|
+
{
|
|
8
|
+
'is-open': layer.isOpen.value,
|
|
9
|
+
'is-empty': !selectedEntry,
|
|
10
|
+
'is-disabled': control.disabled.value,
|
|
11
|
+
'has-lead': Boolean(icon)
|
|
12
|
+
},
|
|
13
|
+
props.class
|
|
14
|
+
]"
|
|
15
|
+
:style="props.style"
|
|
16
|
+
>
|
|
17
|
+
<button
|
|
18
|
+
ref="triggerRef"
|
|
19
|
+
:id="control.id.value"
|
|
20
|
+
:aria-labelledby="
|
|
21
|
+
control.labelledBy.value && !$attrs['aria-label'] ? `${control.labelledBy.value} ${valueId}` : void 0
|
|
22
|
+
"
|
|
23
|
+
v-bind="$attrs"
|
|
24
|
+
:disabled="control.disabled.value || void 0"
|
|
25
|
+
:aria-invalid="control.invalid.value ? 'true' : void 0"
|
|
26
|
+
:aria-describedby="
|
|
27
|
+
joinIds(
|
|
28
|
+
$attrs['aria-describedby'],
|
|
29
|
+
control.describedBy.value,
|
|
30
|
+
readonly ? readonlyId : void 0
|
|
31
|
+
)
|
|
32
|
+
"
|
|
33
|
+
type="button"
|
|
34
|
+
class="k-field k-tree-select-trigger"
|
|
35
|
+
:class="[
|
|
36
|
+
`k-field-${size}`,
|
|
37
|
+
{
|
|
38
|
+
'is-invalid': control.invalid.value,
|
|
39
|
+
'is-disabled': control.disabled.value,
|
|
40
|
+
'is-readonly': readonly
|
|
41
|
+
}
|
|
42
|
+
]"
|
|
43
|
+
aria-haspopup="listbox"
|
|
44
|
+
:aria-expanded="layer.isOpen.value ? 'true' : 'false'"
|
|
45
|
+
:aria-controls="panelId"
|
|
46
|
+
@mousedown="onTriggerMousedown"
|
|
47
|
+
@click="toggleList"
|
|
48
|
+
@keydown="onTriggerKeydown"
|
|
49
|
+
>
|
|
50
|
+
<span v-if="icon" class="k-tree-select-lead" aria-hidden="true"
|
|
51
|
+
><Icon :name="icon"
|
|
52
|
+
/></span>
|
|
53
|
+
<span :id="valueId" class="k-tree-select-value">
|
|
54
|
+
<template v-if="selectedEntry">
|
|
55
|
+
<span v-if="selectedEntry.path.length" class="k-tree-select-value-path">{{
|
|
56
|
+
pathText(selectedEntry.path) + separator
|
|
57
|
+
}}</span>
|
|
58
|
+
<span class="k-tree-select-value-label">{{ selectedEntry.node.label }}</span>
|
|
59
|
+
</template>
|
|
60
|
+
<span v-else class="k-tree-select-placeholder">{{ placeholderText }}</span>
|
|
61
|
+
</span>
|
|
62
|
+
</button>
|
|
63
|
+
<span v-if="readonly" :id="readonlyId" hidden>{{ t("common.readonly") }}</span>
|
|
64
|
+
<span class="k-tree-select-trail">
|
|
65
|
+
<button
|
|
66
|
+
v-if="showClear"
|
|
67
|
+
type="button"
|
|
68
|
+
class="k-tree-select-icon-button"
|
|
69
|
+
:aria-label="clearText"
|
|
70
|
+
:title="clearText"
|
|
71
|
+
tabindex="-1"
|
|
72
|
+
@mousedown.prevent
|
|
73
|
+
@click.stop="clear"
|
|
74
|
+
>
|
|
75
|
+
<Icon name="x-mark" />
|
|
76
|
+
</button>
|
|
77
|
+
<button
|
|
78
|
+
type="button"
|
|
79
|
+
class="k-tree-select-icon-button k-tree-select-chevron"
|
|
80
|
+
:aria-label="toggleText"
|
|
81
|
+
tabindex="-1"
|
|
82
|
+
:disabled="!interactive || void 0"
|
|
83
|
+
@mousedown.prevent
|
|
84
|
+
@click.stop="toggleList"
|
|
85
|
+
>
|
|
86
|
+
<Icon name="chevron-down" />
|
|
87
|
+
</button>
|
|
88
|
+
</span>
|
|
89
|
+
<div
|
|
90
|
+
:id="panelId"
|
|
91
|
+
ref="panelRef"
|
|
92
|
+
popover="manual"
|
|
93
|
+
class="k-floating k-form-popover k-tree-select-panel"
|
|
94
|
+
:data-side="layer.side.value"
|
|
95
|
+
@mousedown.prevent
|
|
96
|
+
>
|
|
97
|
+
<div class="k-tree-select-header">
|
|
98
|
+
<div class="k-tree-select-search">
|
|
99
|
+
<Icon name="magnifying-glass" class="k-tree-select-search-icon" />
|
|
100
|
+
<input
|
|
101
|
+
ref="searchRef"
|
|
102
|
+
class="k-tree-select-search-input"
|
|
103
|
+
type="text"
|
|
104
|
+
role="combobox"
|
|
105
|
+
autocomplete="off"
|
|
106
|
+
autocapitalize="off"
|
|
107
|
+
spellcheck="false"
|
|
108
|
+
aria-autocomplete="list"
|
|
109
|
+
aria-expanded="true"
|
|
110
|
+
:aria-controls="listId"
|
|
111
|
+
:aria-activedescendant="activeId"
|
|
112
|
+
:aria-label="searchText"
|
|
113
|
+
:placeholder="searchText"
|
|
114
|
+
:value="query"
|
|
115
|
+
@input="onInput"
|
|
116
|
+
@keydown="onSearchKeydown"
|
|
117
|
+
@blur="onBlur"
|
|
118
|
+
/>
|
|
119
|
+
</div>
|
|
120
|
+
<button
|
|
121
|
+
v-if="!searching && levelEntry"
|
|
122
|
+
type="button"
|
|
123
|
+
class="k-tree-select-back"
|
|
124
|
+
:aria-label="backText"
|
|
125
|
+
tabindex="-1"
|
|
126
|
+
@click="back"
|
|
127
|
+
>
|
|
128
|
+
<Icon name="chevron-left" class="k-tree-select-back-icon" />
|
|
129
|
+
<span class="k-tree-select-back-text">
|
|
130
|
+
<span v-if="levelEntry.path.length" class="k-tree-select-back-path">{{
|
|
131
|
+
pathText(levelEntry.path) + separator
|
|
132
|
+
}}</span>
|
|
133
|
+
<span class="k-tree-select-back-label">{{ levelEntry.node.label }}</span>
|
|
134
|
+
</span>
|
|
135
|
+
</button>
|
|
136
|
+
</div>
|
|
137
|
+
<ul
|
|
138
|
+
:id="listId"
|
|
139
|
+
role="listbox"
|
|
140
|
+
class="k-tree-select-list"
|
|
141
|
+
:aria-labelledby="listLabelledBy"
|
|
142
|
+
:aria-label="listLabelledBy ? void 0 : listText"
|
|
143
|
+
>
|
|
144
|
+
<li
|
|
145
|
+
v-for="(item, index) in items"
|
|
146
|
+
:id="optionId(index)"
|
|
147
|
+
:key="keyOf(item.node.id)"
|
|
148
|
+
role="option"
|
|
149
|
+
class="k-combobox-option k-tree-select-option"
|
|
150
|
+
:class="{
|
|
151
|
+
'is-active': index === activeIndex,
|
|
152
|
+
'is-selected': item.selected,
|
|
153
|
+
'is-ancestor': item.ancestor,
|
|
154
|
+
'is-parent': item.parent
|
|
155
|
+
}"
|
|
156
|
+
:aria-selected="item.selected ? 'true' : 'false'"
|
|
157
|
+
:aria-disabled="item.node.disabled ? 'true' : void 0"
|
|
158
|
+
@mousemove="activate(index)"
|
|
159
|
+
@click="choose(item)"
|
|
160
|
+
>
|
|
161
|
+
<Icon v-if="item.node.icon" :name="item.node.icon" class="k-combobox-option-icon" />
|
|
162
|
+
<span class="k-combobox-option-text">
|
|
163
|
+
<slot name="node" :node="item.node" :selected="item.selected" :path="item.path">
|
|
164
|
+
<span class="k-combobox-option-label">{{ item.node.label }}</span>
|
|
165
|
+
<span
|
|
166
|
+
v-if="searching ? item.path.length : item.node.description"
|
|
167
|
+
class="k-combobox-option-description"
|
|
168
|
+
>{{ searching ? pathText(item.path) : item.node.description }}</span
|
|
169
|
+
>
|
|
170
|
+
</slot>
|
|
171
|
+
</span>
|
|
172
|
+
<Icon v-if="item.selected" name="check" class="k-combobox-option-selected" />
|
|
173
|
+
<span
|
|
174
|
+
v-if="item.parent && !searching"
|
|
175
|
+
class="k-tree-select-drill"
|
|
176
|
+
:class="{ 'is-button': !leavesOnly }"
|
|
177
|
+
aria-hidden="true"
|
|
178
|
+
@click.stop="drill(item)"
|
|
179
|
+
>
|
|
180
|
+
<Icon name="chevron-right" />
|
|
181
|
+
</span>
|
|
182
|
+
</li>
|
|
183
|
+
</ul>
|
|
184
|
+
<div v-if="!items.length" class="k-combobox-state">
|
|
185
|
+
<slot name="empty" :query="query">{{ emptyLabel }}</slot>
|
|
186
|
+
</div>
|
|
187
|
+
</div>
|
|
188
|
+
<LiveRegion :message="live.message.value" />
|
|
189
|
+
<input
|
|
190
|
+
v-if="formName"
|
|
191
|
+
type="hidden"
|
|
192
|
+
:name="formName"
|
|
193
|
+
:value="selectedEntry ? keyOf(selectedEntry.node.id) : ''"
|
|
194
|
+
/>
|
|
195
|
+
</div>
|
|
196
|
+
</template>
|
|
197
|
+
|
|
198
|
+
<script setup>
|
|
199
|
+
import { computed, nextTick, ref, useAttrs, useId, useTemplateRef, watch } from "vue";
|
|
200
|
+
import { useFieldControl } from "../../composables/useField";
|
|
201
|
+
import { useLocale } from "../../composables/useLocale";
|
|
202
|
+
import { useFloatingLayer } from "../../internal/floating/layer";
|
|
203
|
+
import { nextEnabledIndex } from "../../internal/core/roving";
|
|
204
|
+
import LiveRegion from "../../internal/core/LiveRegion.vue";
|
|
205
|
+
import { useLiveMessage } from "../../internal/core/live";
|
|
206
|
+
import { normalizeSearch } from "../../internal/core/text";
|
|
207
|
+
import { joinIds } from "../../internal/forms/control";
|
|
208
|
+
import Icon from "../core/Icon.vue";
|
|
209
|
+
defineOptions({ inheritAttrs: false });
|
|
210
|
+
const props = defineProps({
|
|
211
|
+
nodes: { type: Array, required: false, default: () => [] },
|
|
212
|
+
name: { type: String, required: false },
|
|
213
|
+
leavesOnly: { type: Boolean, required: false },
|
|
214
|
+
clearable: { type: Boolean, required: false },
|
|
215
|
+
placeholder: { type: String, required: false },
|
|
216
|
+
searchPlaceholder: { type: String, required: false },
|
|
217
|
+
emptyText: { type: String, required: false },
|
|
218
|
+
size: { type: String, required: false, default: "md" },
|
|
219
|
+
icon: { type: null, required: false },
|
|
220
|
+
separator: { type: String, required: false, default: " / " },
|
|
221
|
+
clearLabel: { type: String, required: false },
|
|
222
|
+
toggleLabel: { type: String, required: false },
|
|
223
|
+
backLabel: { type: String, required: false },
|
|
224
|
+
listLabel: { type: String, required: false },
|
|
225
|
+
resultsText: { type: Function, required: false },
|
|
226
|
+
invalid: { type: Boolean, required: false, default: void 0 },
|
|
227
|
+
disabled: { type: Boolean, required: false },
|
|
228
|
+
readonly: { type: Boolean, required: false },
|
|
229
|
+
required: { type: Boolean, required: false },
|
|
230
|
+
class: { type: null, required: false },
|
|
231
|
+
style: { type: null, required: false }
|
|
232
|
+
});
|
|
233
|
+
const model = defineModel({ type: [String, Number, null] });
|
|
234
|
+
const emit = defineEmits(["open", "close"]);
|
|
235
|
+
const { t } = useLocale();
|
|
236
|
+
const placeholderText = computed(() => props.placeholder ?? t("treeSelect.placeholder"));
|
|
237
|
+
const searchText = computed(() => props.searchPlaceholder ?? t("common.search"));
|
|
238
|
+
const emptyLabel = computed(() => props.emptyText ?? t("common.noResults"));
|
|
239
|
+
const clearText = computed(() => props.clearLabel ?? t("treeSelect.clear"));
|
|
240
|
+
const toggleText = computed(() => props.toggleLabel ?? t("treeSelect.toggle"));
|
|
241
|
+
const backText = computed(() => props.backLabel ?? t("common.back"));
|
|
242
|
+
const listText = computed(() => props.listLabel ?? t("treeSelect.list"));
|
|
243
|
+
const attrs = useAttrs();
|
|
244
|
+
const baseId = useId();
|
|
245
|
+
const listId = `${baseId}-listbox`;
|
|
246
|
+
const panelId = `${baseId}-panel`;
|
|
247
|
+
const valueId = `${baseId}-value`;
|
|
248
|
+
const readonlyId = `${baseId}-readonly`;
|
|
249
|
+
const rootRef = useTemplateRef("rootRef");
|
|
250
|
+
const triggerRef = useTemplateRef("triggerRef");
|
|
251
|
+
const panelRef = useTemplateRef("panelRef");
|
|
252
|
+
const searchRef = useTemplateRef("searchRef");
|
|
253
|
+
const query = ref("");
|
|
254
|
+
const level = ref(null);
|
|
255
|
+
const activeIndex = ref(-1);
|
|
256
|
+
const control = useFieldControl({
|
|
257
|
+
invalid: () => props.invalid,
|
|
258
|
+
disabled: () => props.disabled,
|
|
259
|
+
required: () => props.required,
|
|
260
|
+
requiredHint: true
|
|
261
|
+
});
|
|
262
|
+
const layer = useFloatingLayer({
|
|
263
|
+
floating: panelRef,
|
|
264
|
+
anchor: () => triggerRef.value,
|
|
265
|
+
placement: () => "bottom-start",
|
|
266
|
+
offset: () => 6,
|
|
267
|
+
mode: "manual",
|
|
268
|
+
matchWidth: true,
|
|
269
|
+
returnFocus: () => false,
|
|
270
|
+
onOpen: () => emit("open"),
|
|
271
|
+
onClose: () => emit("close")
|
|
272
|
+
});
|
|
273
|
+
const interactive = computed(() => !control.disabled.value && !props.readonly);
|
|
274
|
+
const formName = computed(() => props.name ?? control.field?.name.value);
|
|
275
|
+
const listLabelledBy = computed(
|
|
276
|
+
() => attrs["aria-labelledby"] ?? control.field?.labelId
|
|
277
|
+
);
|
|
278
|
+
const index = computed(() => {
|
|
279
|
+
const map = /* @__PURE__ */ new Map();
|
|
280
|
+
const visit = (nodes, parent, path) => {
|
|
281
|
+
for (const node of nodes) {
|
|
282
|
+
map.set(keyOf(node.id), { node, parent, path });
|
|
283
|
+
if (node.children?.length) visit(node.children, node.id, [...path, node]);
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
visit(props.nodes, null, []);
|
|
287
|
+
return map;
|
|
288
|
+
});
|
|
289
|
+
const selectedEntry = computed(
|
|
290
|
+
() => model.value === null || model.value === void 0 ? void 0 : index.value.get(keyOf(model.value))
|
|
291
|
+
);
|
|
292
|
+
const selectedKey = computed(() => selectedEntry.value ? keyOf(selectedEntry.value.node.id) : "");
|
|
293
|
+
const ancestorKeys = computed(
|
|
294
|
+
() => new Set(selectedEntry.value?.path.map((node) => keyOf(node.id)) ?? [])
|
|
295
|
+
);
|
|
296
|
+
const levelEntry = computed(
|
|
297
|
+
() => level.value === null ? void 0 : index.value.get(keyOf(level.value))
|
|
298
|
+
);
|
|
299
|
+
const showClear = computed(
|
|
300
|
+
() => props.clearable && interactive.value && Boolean(selectedEntry.value)
|
|
301
|
+
);
|
|
302
|
+
const needle = computed(() => normalizeSearch(query.value.trim()));
|
|
303
|
+
const searching = computed(() => Boolean(needle.value));
|
|
304
|
+
function toItem(node, path) {
|
|
305
|
+
const key = keyOf(node.id);
|
|
306
|
+
return {
|
|
307
|
+
node,
|
|
308
|
+
path,
|
|
309
|
+
parent: hasChildren(node),
|
|
310
|
+
selected: key === selectedKey.value,
|
|
311
|
+
ancestor: ancestorKeys.value.has(key)
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
const items = computed(() => {
|
|
315
|
+
if (searching.value) {
|
|
316
|
+
const found = [];
|
|
317
|
+
for (const entry2 of index.value.values()) {
|
|
318
|
+
if (props.leavesOnly && hasChildren(entry2.node)) continue;
|
|
319
|
+
if (normalizeSearch(entry2.node.label).includes(needle.value))
|
|
320
|
+
found.push(toItem(entry2.node, entry2.path));
|
|
321
|
+
}
|
|
322
|
+
return found;
|
|
323
|
+
}
|
|
324
|
+
const entry = levelEntry.value;
|
|
325
|
+
const nodes = entry ? entry.node.children ?? [] : props.nodes;
|
|
326
|
+
const path = entry ? [...entry.path, entry.node] : [];
|
|
327
|
+
return nodes.map((node) => toItem(node, path));
|
|
328
|
+
});
|
|
329
|
+
const activeId = computed(
|
|
330
|
+
() => layer.isOpen.value && activeIndex.value >= 0 && activeIndex.value < items.value.length ? optionId(activeIndex.value) : void 0
|
|
331
|
+
);
|
|
332
|
+
const statusText = computed(() => {
|
|
333
|
+
if (!layer.isOpen.value) return "";
|
|
334
|
+
if (!items.value.length) return emptyLabel.value;
|
|
335
|
+
const count = items.value.length;
|
|
336
|
+
return props.resultsText ? props.resultsText(count) : t("treeSelect.results", { count });
|
|
337
|
+
});
|
|
338
|
+
const live = useLiveMessage();
|
|
339
|
+
watch([statusText, query, level], ([text]) => {
|
|
340
|
+
if (text) live.announce(text);
|
|
341
|
+
else live.clear();
|
|
342
|
+
});
|
|
343
|
+
function keyOf(value) {
|
|
344
|
+
return String(value);
|
|
345
|
+
}
|
|
346
|
+
function hasChildren(node) {
|
|
347
|
+
return Boolean(node.children?.length);
|
|
348
|
+
}
|
|
349
|
+
function pathText(path) {
|
|
350
|
+
return path.map((node) => node.label).join(props.separator);
|
|
351
|
+
}
|
|
352
|
+
function optionId(position) {
|
|
353
|
+
return `${baseId}-option-${position}`;
|
|
354
|
+
}
|
|
355
|
+
function firstEnabled(from = 0, direction = 1) {
|
|
356
|
+
return nextEnabledIndex(
|
|
357
|
+
items.value.length,
|
|
358
|
+
from,
|
|
359
|
+
direction,
|
|
360
|
+
(position) => Boolean(items.value[position]?.node.disabled)
|
|
361
|
+
);
|
|
362
|
+
}
|
|
363
|
+
function activate(position) {
|
|
364
|
+
if (items.value[position]?.node.disabled) return;
|
|
365
|
+
activeIndex.value = position;
|
|
366
|
+
}
|
|
367
|
+
function activateKey(key) {
|
|
368
|
+
const position = items.value.findIndex((item) => keyOf(item.node.id) === key);
|
|
369
|
+
activeIndex.value = position >= 0 && !items.value[position]?.node.disabled ? position : firstEnabled();
|
|
370
|
+
}
|
|
371
|
+
function move(delta) {
|
|
372
|
+
const total = items.value.length;
|
|
373
|
+
if (!total) return;
|
|
374
|
+
const start = activeIndex.value < 0 ? delta > 0 ? 0 : total - 1 : activeIndex.value + delta;
|
|
375
|
+
activeIndex.value = firstEnabled((start + total) % total, delta > 0 ? 1 : -1);
|
|
376
|
+
}
|
|
377
|
+
function openList(text = "") {
|
|
378
|
+
if (!interactive.value || layer.isOpen.value) return;
|
|
379
|
+
query.value = text;
|
|
380
|
+
level.value = selectedEntry.value?.parent ?? null;
|
|
381
|
+
layer.show();
|
|
382
|
+
if (text) activeIndex.value = firstEnabled();
|
|
383
|
+
else activateKey(selectedKey.value);
|
|
384
|
+
nextTick(() => searchRef.value?.focus());
|
|
385
|
+
}
|
|
386
|
+
function closeList(returnFocus = false) {
|
|
387
|
+
if (layer.isOpen.value) layer.hide();
|
|
388
|
+
activeIndex.value = -1;
|
|
389
|
+
query.value = "";
|
|
390
|
+
if (returnFocus) triggerRef.value?.focus();
|
|
391
|
+
}
|
|
392
|
+
function toggleList() {
|
|
393
|
+
if (!interactive.value) return;
|
|
394
|
+
if (layer.isOpen.value) closeList(true);
|
|
395
|
+
else openList();
|
|
396
|
+
}
|
|
397
|
+
function onTriggerMousedown(event) {
|
|
398
|
+
if (interactive.value) event.preventDefault();
|
|
399
|
+
}
|
|
400
|
+
function onTriggerKeydown(event) {
|
|
401
|
+
if (!interactive.value || layer.isOpen.value) return;
|
|
402
|
+
if (["Enter", " ", "ArrowDown", "ArrowUp"].includes(event.key)) {
|
|
403
|
+
event.preventDefault();
|
|
404
|
+
openList();
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
if (event.key.length === 1 && !event.ctrlKey && !event.metaKey && !event.altKey) {
|
|
408
|
+
event.preventDefault();
|
|
409
|
+
openList(event.key);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
function onInput(event) {
|
|
413
|
+
query.value = event.target.value;
|
|
414
|
+
if (searching.value) activeIndex.value = firstEnabled();
|
|
415
|
+
else activateKey(selectedKey.value);
|
|
416
|
+
}
|
|
417
|
+
function onBlur(event) {
|
|
418
|
+
const next = event.relatedTarget;
|
|
419
|
+
if (next && rootRef.value?.contains(next)) return;
|
|
420
|
+
closeList();
|
|
421
|
+
}
|
|
422
|
+
function drill(item) {
|
|
423
|
+
if (item.node.disabled || !item.parent) return;
|
|
424
|
+
query.value = "";
|
|
425
|
+
level.value = item.node.id;
|
|
426
|
+
activeIndex.value = firstEnabled();
|
|
427
|
+
searchRef.value?.focus();
|
|
428
|
+
}
|
|
429
|
+
function back() {
|
|
430
|
+
const entry = levelEntry.value;
|
|
431
|
+
if (!entry) return;
|
|
432
|
+
level.value = entry.parent;
|
|
433
|
+
activateKey(keyOf(entry.node.id));
|
|
434
|
+
searchRef.value?.focus();
|
|
435
|
+
}
|
|
436
|
+
function choose(item) {
|
|
437
|
+
if (item.node.disabled) return;
|
|
438
|
+
if (!searching.value && props.leavesOnly && item.parent) {
|
|
439
|
+
drill(item);
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
model.value = item.node.id;
|
|
443
|
+
closeList(true);
|
|
444
|
+
}
|
|
445
|
+
function clear() {
|
|
446
|
+
model.value = null;
|
|
447
|
+
closeList();
|
|
448
|
+
triggerRef.value?.focus();
|
|
449
|
+
}
|
|
450
|
+
function onSearchKeydown(event) {
|
|
451
|
+
const active = items.value[activeIndex.value];
|
|
452
|
+
switch (event.key) {
|
|
453
|
+
case "ArrowDown":
|
|
454
|
+
event.preventDefault();
|
|
455
|
+
move(1);
|
|
456
|
+
break;
|
|
457
|
+
case "ArrowUp":
|
|
458
|
+
event.preventDefault();
|
|
459
|
+
move(-1);
|
|
460
|
+
break;
|
|
461
|
+
case "Home":
|
|
462
|
+
if (query.value || !items.value.length) return;
|
|
463
|
+
event.preventDefault();
|
|
464
|
+
activeIndex.value = firstEnabled();
|
|
465
|
+
break;
|
|
466
|
+
case "End":
|
|
467
|
+
if (query.value || !items.value.length) return;
|
|
468
|
+
event.preventDefault();
|
|
469
|
+
activeIndex.value = firstEnabled(items.value.length - 1, -1);
|
|
470
|
+
break;
|
|
471
|
+
case "Enter":
|
|
472
|
+
if (!active) return;
|
|
473
|
+
event.preventDefault();
|
|
474
|
+
choose(active);
|
|
475
|
+
break;
|
|
476
|
+
case "ArrowRight":
|
|
477
|
+
if (query.value || !active?.parent) return;
|
|
478
|
+
event.preventDefault();
|
|
479
|
+
drill(active);
|
|
480
|
+
break;
|
|
481
|
+
case "ArrowLeft":
|
|
482
|
+
case "Backspace":
|
|
483
|
+
if (query.value || !levelEntry.value) return;
|
|
484
|
+
event.preventDefault();
|
|
485
|
+
back();
|
|
486
|
+
break;
|
|
487
|
+
case "Escape":
|
|
488
|
+
event.preventDefault();
|
|
489
|
+
event.stopPropagation();
|
|
490
|
+
closeList(true);
|
|
491
|
+
break;
|
|
492
|
+
case "Tab":
|
|
493
|
+
closeList(true);
|
|
494
|
+
break;
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
watch(activeIndex, (position) => {
|
|
498
|
+
if (position < 0) return;
|
|
499
|
+
nextTick(() => {
|
|
500
|
+
panelRef.value?.querySelector(`#${CSS.escape(optionId(position))}`)?.scrollIntoView({ block: "nearest" });
|
|
501
|
+
});
|
|
502
|
+
});
|
|
503
|
+
watch(
|
|
504
|
+
() => items.value.length,
|
|
505
|
+
() => {
|
|
506
|
+
if (layer.isOpen.value) nextTick(layer.reposition);
|
|
507
|
+
}
|
|
508
|
+
);
|
|
509
|
+
defineExpose({
|
|
510
|
+
focus: () => triggerRef.value?.focus(),
|
|
511
|
+
open: () => openList(),
|
|
512
|
+
close: () => closeList(),
|
|
513
|
+
el: triggerRef
|
|
514
|
+
});
|
|
515
|
+
</script>
|