@v1nt1248/3nclient-lib 0.3.48 → 0.3.49

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/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@v1nt1248/3nclient-lib",
3
3
  "license": "AGPL-3.0-or-later",
4
4
  "author": "v1nt1248",
5
- "version": "0.3.48",
5
+ "version": "0.3.49",
6
6
  "description": "Library for 3NWeb clients",
7
7
  "type": "module",
8
8
  "files": [
@@ -48,6 +48,18 @@ export interface Ui3nAutocompleteProps<T extends Ui3nAutocompleteOptionBase> {
48
48
  * @default false
49
49
  */
50
50
  disabled?: boolean;
51
+ /**
52
+ * Whether an individual item cannot be picked.
53
+ *
54
+ * A disabled item is still listed, greyed out and inert to both the pointer
55
+ * and the keyboard. Use it when leaving the item out would be worse than
56
+ * showing it: an absence explains nothing, while a disabled row - with a
57
+ * reason rendered through the `item` slot - does.
58
+ *
59
+ * A predicate rather than a flag on the item itself, so that a caller can
60
+ * answer from state of its own without copying or mutating its data.
61
+ */
62
+ itemDisabled?: (item: T) => boolean;
51
63
  /**
52
64
  * Filter keys
53
65
  */
@@ -81,6 +81,33 @@
81
81
  });
82
82
  });
83
83
 
84
+ /**
85
+ * Whether this item is listed but cannot be picked.
86
+ *
87
+ * Asked in one place, because the pointer and the keyboard have to agree: an
88
+ * item that cannot be clicked must not be reachable with arrows and Enter
89
+ * either.
90
+ */
91
+ function isItemDisabled(item: T | undefined): boolean {
92
+ return !!item && !!props.itemDisabled?.(item);
93
+ }
94
+
95
+ /**
96
+ * Index of the nearest item that can be picked, looking from `from` in the
97
+ * direction of `step`.
98
+ *
99
+ * @returns null when there is none that way, so that the caller can leave the
100
+ * highlight where it is rather than moving it onto a dead row.
101
+ */
102
+ function nearestEnabledIndex(from: number, step: 1 | -1): number | null {
103
+ for (let i = from; (i >= 0) && (i < size(filteredItems.value)); i += step) {
104
+ if (!isItemDisabled(filteredItems.value[i])) {
105
+ return i;
106
+ }
107
+ }
108
+ return null;
109
+ }
110
+
84
111
  function onInput() {
85
112
  isLastChipHighlighted.value = false;
86
113
  emits('update:search', query.value);
@@ -146,7 +173,7 @@
146
173
  }
147
174
 
148
175
  function onItemClick(item: T) {
149
- if (props.disabled) return;
176
+ if (props.disabled || isItemDisabled(item)) return;
150
177
 
151
178
  if (!props.multiple) {
152
179
  const newValue = props.returnObject ? [item] : [item[props.itemValue]];
@@ -183,25 +210,33 @@
183
210
 
184
211
  function handlePressingDownKey() {
185
212
  if (activeItemsIndex.value === null) {
186
- activeItemsIndex.value = 0;
213
+ activeItemsIndex.value = nearestEnabledIndex(0, 1);
187
214
  menuBodyEl.value && menuBodyEl.value.focus({ preventScroll: true });
188
215
  return;
189
216
  }
190
217
 
191
218
  if (activeItemsIndex.value >= 0 && activeItemsIndex.value < size(filteredItems.value) - 1) {
192
- activeItemsIndex.value += 1;
219
+ // Past the disabled ones rather than onto them: the highlight stays put
220
+ // when everything below cannot be picked.
221
+ const next = nearestEnabledIndex(activeItemsIndex.value + 1, 1);
222
+ if (next !== null) {
223
+ activeItemsIndex.value = next;
224
+ }
193
225
  }
194
226
  }
195
227
 
196
228
  function handlePressingUpKey() {
197
229
  if (activeItemsIndex.value === null) {
198
- activeItemsIndex.value = size(filteredItems.value) - 1;
230
+ activeItemsIndex.value = nearestEnabledIndex(size(filteredItems.value) - 1, -1);
199
231
  menuBodyEl.value && menuBodyEl.value.focus({ preventScroll: true });
200
232
  return;
201
233
  }
202
234
 
203
235
  if (activeItemsIndex.value > 0 && activeItemsIndex.value < size(filteredItems.value)) {
204
- activeItemsIndex.value -= 1;
236
+ const previous = nearestEnabledIndex(activeItemsIndex.value - 1, -1);
237
+ if (previous !== null) {
238
+ activeItemsIndex.value = previous;
239
+ }
205
240
  }
206
241
  }
207
242
 
@@ -228,6 +263,12 @@
228
263
  function handlePressingEnterKey() {
229
264
  if (activeItemsIndex.value !== null) {
230
265
  const item = filteredItems.value[activeItemsIndex.value!];
266
+ // Nothing happens on a disabled item, and nothing is cleared either: the
267
+ // query and the highlight are the user's place in the list, and taking
268
+ // them away would read as though the item had been taken.
269
+ if (isItemDisabled(item)) {
270
+ return;
271
+ }
231
272
  onItemClick(item);
232
273
  activeItemsIndex.value = null;
233
274
  props.clearOnSelect && (query.value = '');
@@ -241,7 +282,7 @@
241
282
 
242
283
  if (!isMenuOpen.value) {
243
284
  isMenuOpen.value = true;
244
- activeItemsIndex.value = 0;
285
+ activeItemsIndex.value = nearestEnabledIndex(0, 1);
245
286
  menuBodyEl.value && menuBodyEl.value.focus({ preventScroll: true });
246
287
  }
247
288
  }
@@ -467,8 +508,9 @@
467
508
  :class="[
468
509
  $style.item,
469
510
  activeItemsIndex === index && $style.itemSelected,
470
- disabled && $style.itemDisabled,
511
+ (disabled || isItemDisabled(item)) && $style.itemDisabled,
471
512
  ]"
513
+ :aria-disabled="disabled || isItemDisabled(item)"
472
514
  @mousedown="onItemMouseDown"
473
515
  @click.stop.prevent="onItemClick(item)"
474
516
  >