@zauru-sdk/components 6.20.7 → 6.20.8
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/CHANGELOG.md +11 -0
- package/dist/Form/SelectField/index.d.ts +2 -4
- package/dist/Form/SelectField/selectFieldSelection.d.ts +15 -0
- package/dist/esm/Form/SelectField/index.js +18 -10
- package/dist/esm/Form/SelectField/selectFieldSelection.js +45 -0
- package/package.json +4 -3
- package/src/Form/SelectField/index.tsx +38 -22
- package/src/Form/SelectField/selectFieldSelection.test.ts +173 -0
- package/src/Form/SelectField/selectFieldSelection.ts +80 -0
- package/tsconfig.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,17 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [6.20.8](https://github.com/intuitiva/zauru-typescript-sdk/compare/@zauru-sdk/components@6.20.7...@zauru-sdk/components@6.20.8) (2026-11-04)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Bug Fixes
|
|
10
|
+
|
|
11
|
+
* no tronar SelectField si el índice resaltado queda viejo ([ac80a44](https://github.com/intuitiva/zauru-typescript-sdk/commit/ac80a44e8721a303a79601ca529a2f52f0dc02c4))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
6
17
|
## [6.20.7](https://github.com/intuitiva/zauru-typescript-sdk/compare/@zauru-sdk/components@6.20.6...@zauru-sdk/components@6.20.7) (2026-11-04)
|
|
7
18
|
|
|
8
19
|
**Note:** Version bump only for package @zauru-sdk/components
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { SelectFieldOption } from "@zauru-sdk/types";
|
|
2
|
+
import { type SelectFieldChoice } from "./selectFieldSelection.js";
|
|
2
3
|
type Props = {
|
|
3
4
|
id?: string;
|
|
4
5
|
name?: string;
|
|
@@ -6,10 +7,7 @@ type Props = {
|
|
|
6
7
|
defaultValue?: SelectFieldOption;
|
|
7
8
|
defaultValueMulti?: SelectFieldOption[];
|
|
8
9
|
helpText?: string;
|
|
9
|
-
options: Array<
|
|
10
|
-
disabled?: boolean;
|
|
11
|
-
disabledLabel?: string;
|
|
12
|
-
}>;
|
|
10
|
+
options: Array<SelectFieldChoice>;
|
|
13
11
|
onChange?: (value: SelectFieldOption | null) => void;
|
|
14
12
|
onChangeMulti?: (value: SelectFieldOption[]) => void;
|
|
15
13
|
onInputChange?: (newValue: string) => void;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { SelectFieldOption } from "@zauru-sdk/types";
|
|
2
|
+
export type SelectFieldChoice = SelectFieldOption & {
|
|
3
|
+
disabled?: boolean;
|
|
4
|
+
disabledLabel?: string;
|
|
5
|
+
};
|
|
6
|
+
export declare function isSelectableChoice(option: SelectFieldChoice | null | undefined): option is SelectFieldChoice;
|
|
7
|
+
export declare function choiceAtIndex(options: ReadonlyArray<SelectFieldChoice | null | undefined>, index: number): SelectFieldChoice | undefined;
|
|
8
|
+
export declare function highlightedIndexAfterOptionsChange(highlightedIndex: number, nextLength: number): number;
|
|
9
|
+
export declare function nextHighlightedIndex(current: number, length: number, direction: "down" | "up"): number;
|
|
10
|
+
export declare function resolveEnterSelection(options: ReadonlyArray<SelectFieldChoice | null | undefined>, highlightedIndex: number): SelectFieldChoice | undefined;
|
|
11
|
+
export declare function resolveTabBlurSelection({ options, highlightedIndex, isSearching, }: {
|
|
12
|
+
options: ReadonlyArray<SelectFieldChoice | null | undefined>;
|
|
13
|
+
highlightedIndex: number;
|
|
14
|
+
isSearching: boolean;
|
|
15
|
+
}): SelectFieldChoice | undefined;
|
|
@@ -3,6 +3,7 @@ import { IdeaIconSVG } from "@zauru-sdk/icons";
|
|
|
3
3
|
import { useEffect, useState, useRef, useId, useCallback, } from "react";
|
|
4
4
|
import { LoadingInputSkeleton } from "../../Skeletons/index.js";
|
|
5
5
|
import { useFormContext } from "react-hook-form";
|
|
6
|
+
import { highlightedIndexAfterOptionsChange, isSelectableChoice, nextHighlightedIndex, resolveEnterSelection, resolveTabBlurSelection, } from "./selectFieldSelection.js";
|
|
6
7
|
// Simple comparison helper to check if two SelectFieldOption arrays contain the
|
|
7
8
|
// same values (order-insensitive). We only compare the `value` of each option
|
|
8
9
|
// because it uniquely identifies a SelectFieldOption.
|
|
@@ -105,6 +106,7 @@ export const SelectField = (props) => {
|
|
|
105
106
|
}, [defaultValueMultiKey, isMulti, name, setFormValue]);
|
|
106
107
|
useEffect(() => {
|
|
107
108
|
setFilteredOptions(options);
|
|
109
|
+
setHighlightedIndex((prev) => highlightedIndexAfterOptionsChange(prev, options.length));
|
|
108
110
|
}, [options]);
|
|
109
111
|
useEffect(() => {
|
|
110
112
|
const handleClickOutside = (event) => {
|
|
@@ -175,10 +177,12 @@ export const SelectField = (props) => {
|
|
|
175
177
|
setInputValue(newValue);
|
|
176
178
|
onInputChangeRef.current && onInputChangeRef.current(newValue);
|
|
177
179
|
setIsSearching(true);
|
|
178
|
-
|
|
180
|
+
const nextOptions = options.filter((option) => option.label.toLowerCase().includes(newValue.toLowerCase()));
|
|
181
|
+
setFilteredOptions(nextOptions);
|
|
182
|
+
setHighlightedIndex((prev) => highlightedIndexAfterOptionsChange(prev, nextOptions.length));
|
|
179
183
|
}, [register, options]);
|
|
180
184
|
const handleOptionClick = useCallback((option) => {
|
|
181
|
-
if (option
|
|
185
|
+
if (!isSelectableChoice(option)) {
|
|
182
186
|
return;
|
|
183
187
|
}
|
|
184
188
|
if (isMulti) {
|
|
@@ -202,11 +206,12 @@ export const SelectField = (props) => {
|
|
|
202
206
|
}, [isMulti, handleAddMultiValue, handleSetSingleValue]);
|
|
203
207
|
const handleBlur = useCallback(() => {
|
|
204
208
|
setTimeout(() => {
|
|
205
|
-
if (isTabPressed
|
|
206
|
-
handleOptionClick(
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
209
|
+
if (isTabPressed) {
|
|
210
|
+
handleOptionClick(resolveTabBlurSelection({
|
|
211
|
+
options: filteredOptions,
|
|
212
|
+
highlightedIndex,
|
|
213
|
+
isSearching,
|
|
214
|
+
}));
|
|
210
215
|
}
|
|
211
216
|
setHighlightedIndex(-1);
|
|
212
217
|
setIsTabPressed(false);
|
|
@@ -236,17 +241,17 @@ export const SelectField = (props) => {
|
|
|
236
241
|
}
|
|
237
242
|
else if (e.key === "ArrowDown") {
|
|
238
243
|
e.preventDefault();
|
|
239
|
-
setHighlightedIndex((prevIndex) => prevIndex
|
|
244
|
+
setHighlightedIndex((prevIndex) => nextHighlightedIndex(prevIndex, filteredOptions.length, "down"));
|
|
240
245
|
scrollToHighlightedOption();
|
|
241
246
|
}
|
|
242
247
|
else if (e.key === "ArrowUp") {
|
|
243
248
|
e.preventDefault();
|
|
244
|
-
setHighlightedIndex((prevIndex) => prevIndex
|
|
249
|
+
setHighlightedIndex((prevIndex) => nextHighlightedIndex(prevIndex, filteredOptions.length, "up"));
|
|
245
250
|
scrollToHighlightedOption();
|
|
246
251
|
}
|
|
247
252
|
else if (e.key === "Enter" && highlightedIndex !== -1) {
|
|
248
253
|
e.preventDefault();
|
|
249
|
-
handleOptionClick(filteredOptions
|
|
254
|
+
handleOptionClick(resolveEnterSelection(filteredOptions, highlightedIndex));
|
|
250
255
|
}
|
|
251
256
|
else if (e.key === "Backspace" &&
|
|
252
257
|
inputValue === "" &&
|
|
@@ -288,6 +293,9 @@ export const SelectField = (props) => {
|
|
|
288
293
|
: "Limpiar la opción seleccionada", className: "absolute inset-y-0 right-0 flex cursor-pointer items-center pr-3 text-gray-600 hover:text-gray-900 focus:outline-none focus-visible:ring-2 focus-visible:ring-indigo-500 focus-visible:ring-offset-2", children: _jsx("span", { "aria-hidden": "true", children: "\u00D7" }) })), helpText && (_jsx("div", { className: "flex items-center relative ml-3", children: _jsxs("div", { className: "relative cursor-pointer", onMouseEnter: () => setShowTooltip(true), onMouseLeave: () => setShowTooltip(false), children: [_jsx(IdeaIconSVG, {}), showTooltip && (_jsx("div", { className: "absolute -left-48 top-0 mt-8 p-2 bg-white border rounded shadow text-black z-50", children: helpText }))] }) }))] }), _jsx("input", { type: "hidden", ...(register ?? {}), name: name, value: isMulti
|
|
289
294
|
? valueMulti.map((v) => v.value).join(",")
|
|
290
295
|
: value?.value || "" }), isOpen && !isReadOnly && (_jsx("ul", { ref: optionsRef, className: "absolute z-10 mt-1 w-full bg-white shadow-lg max-h-60 rounded-md py-1 text-base ring-1 ring-black ring-opacity-5 overflow-auto focus:outline-none sm:text-sm", children: filteredOptions.map((option, index) => {
|
|
296
|
+
if (!option) {
|
|
297
|
+
return null;
|
|
298
|
+
}
|
|
291
299
|
const isDisabled = Boolean(option.disabled);
|
|
292
300
|
const disabledLabel = option.disabledLabel;
|
|
293
301
|
const isSelected = isMulti
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export function isSelectableChoice(option) {
|
|
2
|
+
return option != null && option.disabled !== true;
|
|
3
|
+
}
|
|
4
|
+
export function choiceAtIndex(options, index) {
|
|
5
|
+
if (!Number.isInteger(index) || index < 0 || index >= options.length) {
|
|
6
|
+
return undefined;
|
|
7
|
+
}
|
|
8
|
+
return options[index] ?? undefined;
|
|
9
|
+
}
|
|
10
|
+
export function highlightedIndexAfterOptionsChange(highlightedIndex, nextLength) {
|
|
11
|
+
if (highlightedIndex < 0 || nextLength <= 0) {
|
|
12
|
+
return -1;
|
|
13
|
+
}
|
|
14
|
+
if (highlightedIndex >= nextLength) {
|
|
15
|
+
return -1;
|
|
16
|
+
}
|
|
17
|
+
return highlightedIndex;
|
|
18
|
+
}
|
|
19
|
+
export function nextHighlightedIndex(current, length, direction) {
|
|
20
|
+
if (length <= 0) {
|
|
21
|
+
return -1;
|
|
22
|
+
}
|
|
23
|
+
if (direction === "down") {
|
|
24
|
+
return current < length - 1 ? current + 1 : 0;
|
|
25
|
+
}
|
|
26
|
+
return current > 0 ? current - 1 : length - 1;
|
|
27
|
+
}
|
|
28
|
+
export function resolveEnterSelection(options, highlightedIndex) {
|
|
29
|
+
if (highlightedIndex === -1) {
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
const option = choiceAtIndex(options, highlightedIndex);
|
|
33
|
+
return isSelectableChoice(option) ? option : undefined;
|
|
34
|
+
}
|
|
35
|
+
export function resolveTabBlurSelection({ options, highlightedIndex, isSearching, }) {
|
|
36
|
+
if (highlightedIndex >= 0) {
|
|
37
|
+
const highlighted = choiceAtIndex(options, highlightedIndex);
|
|
38
|
+
return isSelectableChoice(highlighted) ? highlighted : undefined;
|
|
39
|
+
}
|
|
40
|
+
if (!isSearching) {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
const first = choiceAtIndex(options, 0);
|
|
44
|
+
return isSelectableChoice(first) ? first : undefined;
|
|
45
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zauru-sdk/components",
|
|
3
|
-
"version": "6.20.
|
|
3
|
+
"version": "6.20.8",
|
|
4
4
|
"description": "Componentes reutilizables en las WebApps de Zauru.",
|
|
5
5
|
"main": "./dist/esm/index.js",
|
|
6
6
|
"module": "./dist/esm/index.js",
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"build:esm": "tsc -p tsconfig.esm.json",
|
|
16
16
|
"play": "ladle serve --port 61000",
|
|
17
17
|
"play:build": "ladle build",
|
|
18
|
-
"test": "
|
|
18
|
+
"test": "tsx --test src/Form/SelectField/selectFieldSelection.test.ts"
|
|
19
19
|
},
|
|
20
20
|
"author": {
|
|
21
21
|
"name": "Oscar Cuellar",
|
|
@@ -35,6 +35,7 @@
|
|
|
35
35
|
"react-redux": "^9.1.2",
|
|
36
36
|
"react-router-dom": "6.26.2",
|
|
37
37
|
"tailwindcss": "^3.4.1",
|
|
38
|
+
"tsx": "^4.23.15",
|
|
38
39
|
"typescript": "^5.1.6",
|
|
39
40
|
"vite": "^8.3.0"
|
|
40
41
|
},
|
|
@@ -62,5 +63,5 @@
|
|
|
62
63
|
"styled-components": "^5.3.5",
|
|
63
64
|
"zod": "^3.23.8"
|
|
64
65
|
},
|
|
65
|
-
"gitHead": "
|
|
66
|
+
"gitHead": "de38b697c4e97276fb750b782ae33a7eb5151015"
|
|
66
67
|
}
|
|
@@ -10,6 +10,14 @@ import React, {
|
|
|
10
10
|
} from "react";
|
|
11
11
|
import { LoadingInputSkeleton } from "../../Skeletons/index.js";
|
|
12
12
|
import { useFormContext } from "react-hook-form";
|
|
13
|
+
import {
|
|
14
|
+
highlightedIndexAfterOptionsChange,
|
|
15
|
+
isSelectableChoice,
|
|
16
|
+
nextHighlightedIndex,
|
|
17
|
+
resolveEnterSelection,
|
|
18
|
+
resolveTabBlurSelection,
|
|
19
|
+
type SelectFieldChoice,
|
|
20
|
+
} from "./selectFieldSelection.js";
|
|
13
21
|
|
|
14
22
|
type Props = {
|
|
15
23
|
id?: string;
|
|
@@ -18,9 +26,7 @@ type Props = {
|
|
|
18
26
|
defaultValue?: SelectFieldOption;
|
|
19
27
|
defaultValueMulti?: SelectFieldOption[];
|
|
20
28
|
helpText?: string;
|
|
21
|
-
options: Array<
|
|
22
|
-
SelectFieldOption & { disabled?: boolean; disabledLabel?: string }
|
|
23
|
-
>;
|
|
29
|
+
options: Array<SelectFieldChoice>;
|
|
24
30
|
onChange?: (value: SelectFieldOption | null) => void;
|
|
25
31
|
onChangeMulti?: (value: SelectFieldOption[]) => void;
|
|
26
32
|
onInputChange?: (newValue: string) => void;
|
|
@@ -194,6 +200,9 @@ export const SelectField = (props: Props) => {
|
|
|
194
200
|
|
|
195
201
|
useEffect(() => {
|
|
196
202
|
setFilteredOptions(options);
|
|
203
|
+
setHighlightedIndex((prev) =>
|
|
204
|
+
highlightedIndexAfterOptionsChange(prev, options.length),
|
|
205
|
+
);
|
|
197
206
|
}, [options]);
|
|
198
207
|
|
|
199
208
|
useEffect(() => {
|
|
@@ -288,18 +297,20 @@ export const SelectField = (props: Props) => {
|
|
|
288
297
|
setInputValue(newValue);
|
|
289
298
|
onInputChangeRef.current && onInputChangeRef.current(newValue);
|
|
290
299
|
setIsSearching(true);
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
300
|
+
const nextOptions = options.filter((option) =>
|
|
301
|
+
option.label.toLowerCase().includes(newValue.toLowerCase()),
|
|
302
|
+
);
|
|
303
|
+
setFilteredOptions(nextOptions);
|
|
304
|
+
setHighlightedIndex((prev) =>
|
|
305
|
+
highlightedIndexAfterOptionsChange(prev, nextOptions.length),
|
|
295
306
|
);
|
|
296
307
|
},
|
|
297
308
|
[register, options]
|
|
298
309
|
);
|
|
299
310
|
|
|
300
311
|
const handleOptionClick = useCallback(
|
|
301
|
-
(option
|
|
302
|
-
if (option
|
|
312
|
+
(option?: SelectFieldChoice | null) => {
|
|
313
|
+
if (!isSelectableChoice(option)) {
|
|
303
314
|
return;
|
|
304
315
|
}
|
|
305
316
|
if (isMulti) {
|
|
@@ -325,10 +336,14 @@ export const SelectField = (props: Props) => {
|
|
|
325
336
|
|
|
326
337
|
const handleBlur = useCallback(() => {
|
|
327
338
|
setTimeout(() => {
|
|
328
|
-
if (isTabPressed
|
|
329
|
-
handleOptionClick(
|
|
330
|
-
|
|
331
|
-
|
|
339
|
+
if (isTabPressed) {
|
|
340
|
+
handleOptionClick(
|
|
341
|
+
resolveTabBlurSelection({
|
|
342
|
+
options: filteredOptions,
|
|
343
|
+
highlightedIndex,
|
|
344
|
+
isSearching,
|
|
345
|
+
}),
|
|
346
|
+
);
|
|
332
347
|
}
|
|
333
348
|
|
|
334
349
|
setHighlightedIndex(-1);
|
|
@@ -364,18 +379,20 @@ export const SelectField = (props: Props) => {
|
|
|
364
379
|
} else if (e.key === "ArrowDown") {
|
|
365
380
|
e.preventDefault();
|
|
366
381
|
setHighlightedIndex((prevIndex) =>
|
|
367
|
-
prevIndex
|
|
382
|
+
nextHighlightedIndex(prevIndex, filteredOptions.length, "down"),
|
|
368
383
|
);
|
|
369
384
|
scrollToHighlightedOption();
|
|
370
385
|
} else if (e.key === "ArrowUp") {
|
|
371
386
|
e.preventDefault();
|
|
372
387
|
setHighlightedIndex((prevIndex) =>
|
|
373
|
-
prevIndex
|
|
388
|
+
nextHighlightedIndex(prevIndex, filteredOptions.length, "up"),
|
|
374
389
|
);
|
|
375
390
|
scrollToHighlightedOption();
|
|
376
391
|
} else if (e.key === "Enter" && highlightedIndex !== -1) {
|
|
377
392
|
e.preventDefault();
|
|
378
|
-
handleOptionClick(
|
|
393
|
+
handleOptionClick(
|
|
394
|
+
resolveEnterSelection(filteredOptions, highlightedIndex),
|
|
395
|
+
);
|
|
379
396
|
} else if (
|
|
380
397
|
e.key === "Backspace" &&
|
|
381
398
|
inputValue === "" &&
|
|
@@ -513,12 +530,11 @@ export const SelectField = (props: Props) => {
|
|
|
513
530
|
className="absolute z-10 mt-1 w-full bg-white shadow-lg max-h-60 rounded-md py-1 text-base ring-1 ring-black ring-opacity-5 overflow-auto focus:outline-none sm:text-sm"
|
|
514
531
|
>
|
|
515
532
|
{filteredOptions.map((option, index) => {
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
const
|
|
520
|
-
|
|
521
|
-
).disabledLabel;
|
|
533
|
+
if (!option) {
|
|
534
|
+
return null;
|
|
535
|
+
}
|
|
536
|
+
const isDisabled = Boolean(option.disabled);
|
|
537
|
+
const disabledLabel = option.disabledLabel;
|
|
522
538
|
const isSelected = isMulti
|
|
523
539
|
? valueMulti.some((v) => v.value === option.value)
|
|
524
540
|
: value?.value === option.value;
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { describe, it } from "node:test";
|
|
3
|
+
import {
|
|
4
|
+
choiceAtIndex,
|
|
5
|
+
highlightedIndexAfterOptionsChange,
|
|
6
|
+
isSelectableChoice,
|
|
7
|
+
nextHighlightedIndex,
|
|
8
|
+
resolveEnterSelection,
|
|
9
|
+
resolveTabBlurSelection,
|
|
10
|
+
type SelectFieldChoice,
|
|
11
|
+
} from "./selectFieldSelection.js";
|
|
12
|
+
|
|
13
|
+
const option = (
|
|
14
|
+
value: string,
|
|
15
|
+
extra: Partial<SelectFieldChoice> = {},
|
|
16
|
+
): SelectFieldChoice => ({
|
|
17
|
+
label: extra.label ?? value,
|
|
18
|
+
value,
|
|
19
|
+
...extra,
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
const catalog: SelectFieldChoice[] = [
|
|
23
|
+
option("central"),
|
|
24
|
+
option("norte", { disabled: true, disabledLabel: "cerrada" }),
|
|
25
|
+
option("sur"),
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
describe("isSelectableChoice", () => {
|
|
29
|
+
it("When the option is missing, it should not be selectable", () => {
|
|
30
|
+
assert.equal(isSelectableChoice(undefined), false);
|
|
31
|
+
assert.equal(isSelectableChoice(null), false);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("When the option is disabled, it should not be selectable", () => {
|
|
35
|
+
assert.equal(isSelectableChoice(catalog[1]), false);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("When the option is enabled, it should be selectable", () => {
|
|
39
|
+
assert.equal(isSelectableChoice(catalog[0]), true);
|
|
40
|
+
assert.equal(isSelectableChoice(catalog[2]), true);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
describe("choiceAtIndex", () => {
|
|
45
|
+
it("When the index is in range, it should return that option", () => {
|
|
46
|
+
assert.equal(choiceAtIndex(catalog, 2)?.value, "sur");
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("When the index is stale after options shrink, it should return undefined", () => {
|
|
50
|
+
assert.equal(choiceAtIndex(catalog.slice(0, 1), 2), undefined);
|
|
51
|
+
assert.equal(choiceAtIndex(catalog, -1), undefined);
|
|
52
|
+
assert.equal(choiceAtIndex([], 0), undefined);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("When the slot is empty, it should return undefined", () => {
|
|
56
|
+
assert.equal(choiceAtIndex([catalog[0], undefined, catalog[2]], 1), undefined);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
describe("highlightedIndexAfterOptionsChange", () => {
|
|
61
|
+
it("When the highlighted option still exists, it should keep the index", () => {
|
|
62
|
+
assert.equal(highlightedIndexAfterOptionsChange(1, 3), 1);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("When options shrink past the highlight, it should clear it", () => {
|
|
66
|
+
assert.equal(highlightedIndexAfterOptionsChange(8, 3), -1);
|
|
67
|
+
assert.equal(highlightedIndexAfterOptionsChange(0, 0), -1);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("When nothing is highlighted, it should stay cleared", () => {
|
|
71
|
+
assert.equal(highlightedIndexAfterOptionsChange(-1, 10), -1);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
describe("nextHighlightedIndex", () => {
|
|
76
|
+
it("When moving down, it should wrap to the first option", () => {
|
|
77
|
+
assert.equal(nextHighlightedIndex(2, 3, "down"), 0);
|
|
78
|
+
assert.equal(nextHighlightedIndex(-1, 3, "down"), 0);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("When moving up, it should wrap to the last option", () => {
|
|
82
|
+
assert.equal(nextHighlightedIndex(0, 3, "up"), 2);
|
|
83
|
+
assert.equal(nextHighlightedIndex(-1, 3, "up"), 2);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("When there are no options, it should not highlight an empty slot", () => {
|
|
87
|
+
assert.equal(nextHighlightedIndex(0, 0, "down"), -1);
|
|
88
|
+
assert.equal(nextHighlightedIndex(4, 0, "up"), -1);
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
describe("resolveEnterSelection", () => {
|
|
93
|
+
it("When Enter is pressed on an enabled option, it should select it", () => {
|
|
94
|
+
assert.equal(resolveEnterSelection(catalog, 2)?.value, "sur");
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("When Enter is pressed on a disabled option, it should select nothing", () => {
|
|
98
|
+
assert.equal(resolveEnterSelection(catalog, 1), undefined);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("When Enter is pressed after options shrink, it should not throw and select nothing", () => {
|
|
102
|
+
assert.equal(resolveEnterSelection(catalog.slice(0, 1), 8), undefined);
|
|
103
|
+
assert.equal(resolveEnterSelection(catalog, -1), undefined);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
describe("resolveTabBlurSelection", () => {
|
|
108
|
+
it("When Tab leaves a highlighted enabled option, it should select that option", () => {
|
|
109
|
+
assert.equal(
|
|
110
|
+
resolveTabBlurSelection({
|
|
111
|
+
options: catalog,
|
|
112
|
+
highlightedIndex: 2,
|
|
113
|
+
isSearching: false,
|
|
114
|
+
})?.value,
|
|
115
|
+
"sur",
|
|
116
|
+
);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("When Tab leaves a stale highlight, it should not fall back to the first option", () => {
|
|
120
|
+
assert.equal(
|
|
121
|
+
resolveTabBlurSelection({
|
|
122
|
+
options: catalog.slice(0, 1),
|
|
123
|
+
highlightedIndex: 8,
|
|
124
|
+
isSearching: true,
|
|
125
|
+
}),
|
|
126
|
+
undefined,
|
|
127
|
+
);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("When Tab leaves a disabled highlight, it should select nothing", () => {
|
|
131
|
+
assert.equal(
|
|
132
|
+
resolveTabBlurSelection({
|
|
133
|
+
options: catalog,
|
|
134
|
+
highlightedIndex: 1,
|
|
135
|
+
isSearching: true,
|
|
136
|
+
}),
|
|
137
|
+
undefined,
|
|
138
|
+
);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("When Tab leaves a search with no highlight, it should select the first enabled match", () => {
|
|
142
|
+
assert.equal(
|
|
143
|
+
resolveTabBlurSelection({
|
|
144
|
+
options: catalog,
|
|
145
|
+
highlightedIndex: -1,
|
|
146
|
+
isSearching: true,
|
|
147
|
+
})?.value,
|
|
148
|
+
"central",
|
|
149
|
+
);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("When Tab leaves without searching or highlighting, it should select nothing", () => {
|
|
153
|
+
assert.equal(
|
|
154
|
+
resolveTabBlurSelection({
|
|
155
|
+
options: catalog,
|
|
156
|
+
highlightedIndex: -1,
|
|
157
|
+
isSearching: false,
|
|
158
|
+
}),
|
|
159
|
+
undefined,
|
|
160
|
+
);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it("When the first search match is disabled, it should not select it", () => {
|
|
164
|
+
assert.equal(
|
|
165
|
+
resolveTabBlurSelection({
|
|
166
|
+
options: [catalog[1], catalog[2]],
|
|
167
|
+
highlightedIndex: -1,
|
|
168
|
+
isSearching: true,
|
|
169
|
+
}),
|
|
170
|
+
undefined,
|
|
171
|
+
);
|
|
172
|
+
});
|
|
173
|
+
});
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { SelectFieldOption } from "@zauru-sdk/types";
|
|
2
|
+
|
|
3
|
+
export type SelectFieldChoice = SelectFieldOption & {
|
|
4
|
+
disabled?: boolean;
|
|
5
|
+
disabledLabel?: string;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export function isSelectableChoice(
|
|
9
|
+
option: SelectFieldChoice | null | undefined,
|
|
10
|
+
): option is SelectFieldChoice {
|
|
11
|
+
return option != null && option.disabled !== true;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function choiceAtIndex(
|
|
15
|
+
options: ReadonlyArray<SelectFieldChoice | null | undefined>,
|
|
16
|
+
index: number,
|
|
17
|
+
): SelectFieldChoice | undefined {
|
|
18
|
+
if (!Number.isInteger(index) || index < 0 || index >= options.length) {
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
return options[index] ?? undefined;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function highlightedIndexAfterOptionsChange(
|
|
25
|
+
highlightedIndex: number,
|
|
26
|
+
nextLength: number,
|
|
27
|
+
): number {
|
|
28
|
+
if (highlightedIndex < 0 || nextLength <= 0) {
|
|
29
|
+
return -1;
|
|
30
|
+
}
|
|
31
|
+
if (highlightedIndex >= nextLength) {
|
|
32
|
+
return -1;
|
|
33
|
+
}
|
|
34
|
+
return highlightedIndex;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function nextHighlightedIndex(
|
|
38
|
+
current: number,
|
|
39
|
+
length: number,
|
|
40
|
+
direction: "down" | "up",
|
|
41
|
+
): number {
|
|
42
|
+
if (length <= 0) {
|
|
43
|
+
return -1;
|
|
44
|
+
}
|
|
45
|
+
if (direction === "down") {
|
|
46
|
+
return current < length - 1 ? current + 1 : 0;
|
|
47
|
+
}
|
|
48
|
+
return current > 0 ? current - 1 : length - 1;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function resolveEnterSelection(
|
|
52
|
+
options: ReadonlyArray<SelectFieldChoice | null | undefined>,
|
|
53
|
+
highlightedIndex: number,
|
|
54
|
+
): SelectFieldChoice | undefined {
|
|
55
|
+
if (highlightedIndex === -1) {
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
const option = choiceAtIndex(options, highlightedIndex);
|
|
59
|
+
return isSelectableChoice(option) ? option : undefined;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function resolveTabBlurSelection({
|
|
63
|
+
options,
|
|
64
|
+
highlightedIndex,
|
|
65
|
+
isSearching,
|
|
66
|
+
}: {
|
|
67
|
+
options: ReadonlyArray<SelectFieldChoice | null | undefined>;
|
|
68
|
+
highlightedIndex: number;
|
|
69
|
+
isSearching: boolean;
|
|
70
|
+
}): SelectFieldChoice | undefined {
|
|
71
|
+
if (highlightedIndex >= 0) {
|
|
72
|
+
const highlighted = choiceAtIndex(options, highlightedIndex);
|
|
73
|
+
return isSelectableChoice(highlighted) ? highlighted : undefined;
|
|
74
|
+
}
|
|
75
|
+
if (!isSearching) {
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
const first = choiceAtIndex(options, 0);
|
|
79
|
+
return isSelectableChoice(first) ? first : undefined;
|
|
80
|
+
}
|
package/tsconfig.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"include": ["**/*.ts", "**/*.tsx", "**/*.css"],
|
|
3
|
-
"exclude": ["stories", "dist", "node_modules", ".ladle"],
|
|
3
|
+
"exclude": ["src/**/*.test.ts", "src/**/*.test.tsx", "stories", "dist", "node_modules", ".ladle"],
|
|
4
4
|
"compilerOptions": {
|
|
5
5
|
"target": "ES2022",
|
|
6
6
|
"module": "NodeNext",
|