All files / spinbutton spinbutton.ts

88.52% Statements 108/122
95% Branches 19/20
91.66% Functions 11/12
88.52% Lines 108/122

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 1231x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 3x 3x 7x 7x 3x 3x 7x 7x                         7x 7x 6x 6x 6x 6x 6x 6x 1x 1x 6x 1x 1x 6x 1x 1x 6x 1x 1x 6x 1x 1x 6x 1x 1x 6x 6x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 13x 13x 7x 13x 13x 7x 13x 13x 7x 12x 1x 1x 1x     1x 1x 1x 12x 7x 11x 11x 7x 7x 7x 7x 7x 7x 7x 7x 12x 12x 7x 7x 7x 7x 7x 7x 7x 7x 7x  
import { JSX, createSignal, mergeProps } from "solid-js";
import { combineProps } from "@solid-primitives/props";
 
import { IS_DEV } from "../../internals/utils/is-dev";
 
import type { AriaSpinButton, DefaultSpinButtonArguments, SpinButtonArguments } from "./types";
 
const defaultProps = {
	step: 1
} satisfies DefaultSpinButtonArguments;
 
/**
 * Creates a Spinbutton bindings according to the WAI-ARIA specification.
 *
 * @link https://www.w3.org/WAI/ARIA/apg/patterns/spinbutton
 */
export function createSpinButton<T extends string | number>(args: SpinButtonArguments<T>): AriaSpinButton<T> {
	const local = mergeProps(defaultProps, args);
 
	const [selectedIndex, setSelectedIndex] = createSignal(0);
 
	const value = () => args.values[selectedIndex()];
 
	const next = (_: Event | null, step = 1) => {
		setSelectedIndex((selectedIndex() + step) % args.values.length);
	};
 
	const prev = (_: Event | null, step = 1) => {
		setSelectedIndex((selectedIndex() - step + args.values.length) % args.values.length);
	};
 
	const onInput: JSX.EventHandlerUnion<HTMLInputElement, InputEvent> = (event) => {
		let input: string | number = event.currentTarget.value;

		if (typeof args.values[0] === "number") {
			input = Number(input);
		}

		const index = args.values.findIndex((value) => value === input);

		if (index !== -1) {
			setSelectedIndex(index);
		}
	};
 
	const onKeyDown: JSX.EventHandlerUnion<HTMLInputElement, KeyboardEvent> = (event) => {
		if (["ArrowUp", "ArrowDown", "Home", "End", "PageUp", "PageDown"].includes(event.key)) {
			event.preventDefault();
		}
 
		switch (event.key) {
			case "ArrowUp":
				next(null);
				break;
			case "Home":
				setSelectedIndex(0);
				break;
			case "End":
				setSelectedIndex(args.values.length - 1);
				break;
			case "ArrowDown":
				prev(null);
				break;
			case "PageUp":
				next(null, local.step);
				break;
			case "PageDown":
				prev(null, local.step);
				break;
		}
	};
 
	const inputProps: JSX.InputHTMLAttributes<HTMLInputElement> = {
		role: "spinbutton",
		type: "text",
		autocomplete: "off",
		autocorrect: "off",
		spellcheck: false,
		onKeyDown,
		onInput,
		get "aria-valuenow"() {
			return selectedIndex();
		},
		get "aria-valuemin"() {
			return 0;
		},
		get "aria-valuemax"() {
			return args.values.length - 1;
		},
		get "aria-valuetext"() {
			if (!args.mapping) return undefined;
 
			if (IS_DEV) {
				if (args.mapping.length !== args.values.length) {
					console.warn("[solid-apg]: Mapping length should be equal to values length.");
				}
			}
 
			return args.mapping[selectedIndex()];
		},
		get "aria-invalid"() {
			return !args.values.includes(value());
		}
	};
 
	const baseButtonProps: JSX.HTMLAttributes<HTMLButtonElement> = {
		tabIndex: "-1"
	};
 
	const state = combineProps({
		get value() {
			return value();
		}
	});
 
	return {
		inputProps,
		incrementButtonProps: combineProps(baseButtonProps, { onClick: next }),
		decrementButtonProps: combineProps(baseButtonProps, { onClick: prev }),
		state
	} as const;
}