-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Expand file tree
/
Copy pathspinbutton.ts
More file actions
187 lines (153 loc) · 5.57 KB
/
spinbutton.ts
File metadata and controls
187 lines (153 loc) · 5.57 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {KeyboardEventManager} from '../behaviors/event-manager';
import {SignalLike, WritableSignalLike, computed} from '../behaviors/signal-like/signal-like';
/** Represents the required inputs for a spinbutton. */
export interface SpinButtonInputs {
/** A unique identifier for the spinbutton input element. */
id: SignalLike<string>;
/** The current numeric value of the spinbutton. */
value: WritableSignalLike<number>;
/** The minimum allowed value. */
min: SignalLike<number | undefined>;
/** The maximum allowed value. */
max: SignalLike<number | undefined>;
/** The amount to increment or decrement by. */
step: SignalLike<number>;
/** The amount to increment or decrement by for page up/down. */
pageStep: SignalLike<number | undefined>;
/** Whether the spinbutton is disabled. */
disabled: SignalLike<boolean>;
/** Whether the spinbutton is readonly. */
readonly: SignalLike<boolean>;
/** Whether to wrap the value at boundaries. */
wrap: SignalLike<boolean>;
/** Human-readable value text for aria-valuetext. */
valueText: SignalLike<string | undefined>;
/** Reference to the input element. */
inputElement: SignalLike<HTMLElement | undefined>;
}
/** Controls the state of a spinbutton. */
export class SpinButtonPattern {
/** The inputs for this spinbutton pattern. */
readonly inputs: SpinButtonInputs;
/** The tab index of the spinbutton input. */
readonly tabIndex = computed(() => (this.inputs.disabled() ? -1 : 0));
/** The current numeric value for aria-valuenow. */
readonly ariaValueNow = computed(() => this.inputs.value());
/** Whether the current value is invalid (outside min/max bounds). */
readonly invalid = computed(() => {
const value = this.inputs.value();
const min = this.inputs.min();
const max = this.inputs.max();
return (min !== undefined && value < min) || (max !== undefined && value > max);
});
/** Whether the value is at the minimum. */
readonly atMin = computed(() => {
const min = this.inputs.min();
return min !== undefined && this.inputs.value() <= min;
});
/** Whether the value is at the maximum. */
readonly atMax = computed(() => {
const max = this.inputs.max();
return max !== undefined && this.inputs.value() >= max;
});
/** The keydown event manager for the spinbutton. */
readonly keydown = computed(() => {
return new KeyboardEventManager()
.on('ArrowUp', () => this.increment())
.on('ArrowDown', () => this.decrement())
.on('Home', () => this.goToMin())
.on('End', () => this.goToMax())
.on('PageUp', () => this.incrementByPage())
.on('PageDown', () => this.decrementByPage());
});
constructor(inputs: SpinButtonInputs) {
this.inputs = inputs;
}
/** Whether the spinbutton value can be modified. */
private _canModify(): boolean {
return !this.inputs.disabled() && !this.inputs.readonly();
}
/** Validates the spinbutton configuration and returns a list of violations. */
validate(): string[] {
const min = this.inputs.min();
const max = this.inputs.max();
if (min !== undefined && max !== undefined && min > max) {
return [`Spinbutton has invalid bounds: min (${min}) is greater than max (${max}).`];
}
return [];
}
/** Sets the spinbutton to its default initial state. */
setDefaultState(): void {}
/** Handles keydown events for the spinbutton. */
onKeydown(event: KeyboardEvent): void {
if (this._canModify()) {
this.keydown().handle(event);
}
}
/** Handles pointerdown events for the spinbutton. */
onPointerdown(_event: PointerEvent): void {
const element = this.inputs.inputElement();
if (element && !this.inputs.disabled()) {
element.focus();
}
}
/** Increments the value by the step amount. */
increment(): void {
if (this._canModify()) {
this._adjustValue(this.inputs.step());
}
}
/** Decrements the value by the step amount. */
decrement(): void {
if (this._canModify()) {
this._adjustValue(-this.inputs.step());
}
}
/** Increments the value by the page step amount. */
incrementByPage(): void {
if (this._canModify()) {
this._adjustValue(this.inputs.pageStep() ?? this.inputs.step() * 10);
}
}
/** Decrements the value by the page step amount. */
decrementByPage(): void {
if (this._canModify()) {
this._adjustValue(-(this.inputs.pageStep() ?? this.inputs.step() * 10));
}
}
/** Sets the value to the minimum. */
goToMin(): void {
const min = this.inputs.min();
if (this._canModify() && min !== undefined) {
this.inputs.value.set(min);
}
}
/** Sets the value to the maximum. */
goToMax(): void {
const max = this.inputs.max();
if (this._canModify() && max !== undefined) {
this.inputs.value.set(max);
}
}
/** Adjusts the value by the given delta, respecting bounds and wrap behavior. */
private _adjustValue(delta: number): void {
const min = this.inputs.min();
const max = this.inputs.max();
let newValue = this.inputs.value() + delta;
if (this.inputs.wrap() && min !== undefined && max !== undefined) {
const range = max - min + 1;
newValue = min + ((((newValue - min) % range) + range) % range);
} else {
if (min !== undefined) newValue = Math.max(min, newValue);
if (max !== undefined) newValue = Math.min(max, newValue);
}
this.inputs.value.set(newValue);
}
}