-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
useStepper.ts
259 lines (238 loc) · 8.02 KB
/
useStepper.ts
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import React from 'react';
import type { HTMLAttributes, KeyboardEvent } from 'react';
import { useIsomorphicId } from './useId';
import type { Steps, StepperOrientation } from '../types';
/**
* Props for build stepper.
*/
type StepperProps = {
/** List of steps. */
steps: Steps[];
/** Current step selected. */
currentStep?: number;
/** Orientation. Default is horizontal. */
orientation?: StepperOrientation;
};
/**
* Types for the stepper state.
*/
type StepperState = {
/** returns the current step. */
currentStep: number;
/** Unique ID to the progress bar. */
progressId: string;
/** These ID you can use with a span element to represent the progress bar label. */
labelId: string;
/** Check if has previuos step. */
hasPreviousStep?: boolean;
/** Check if has next step. */
hasNextStep?: boolean;
/** returns the total steps. */
totalSteps: number;
};
/**
* Hook to use stepper.
*/
type UseStepper = {
/** Props to use into any React element to represent the stepper. */
stepperProps: HTMLAttributes<HTMLElement>;
/** Props to use into any React Element to represent the steps. */
stepsProps: HTMLAttributes<HTMLElement>[];
/** props to use into a HTMLElement to represents a progress bar. */
progressProps: HTMLAttributes<HTMLElement>;
/** the stepper state with current step, disabled steps and other props. */
state: StepperState;
/** Next step */
nextStep: () => void;
/** Previous step */
prevStep: () => void;
/** Choise a step to go to. */
setStep: (step: number) => void;
};
const useStepper = ({
orientation = 'horizontal',
...rest
}: StepperProps): UseStepper => {
const { steps, currentStep } = rest;
// IDs
const stepperId = useIsomorphicId();
const progressId = useIsomorphicId();
const labelId = `${useIsomorphicId()}-label`;
// States & Ref's
const [_currentStep, setCurrentStep] = React.useState(currentStep ?? 0);
const hasPreviousStep = React.useRef(false);
const hasNextStep = React.useRef(false);
const stepElementsRef = React.useRef<HTMLElement[]>([]);
const currentStepFocused = React.useRef<number | null>(null);
const totalSteps = React.useRef(steps.length);
// Check previous step
hasPreviousStep.current = _currentStep < steps.length - 1;
hasPreviousStep.current = _currentStep > 0;
// Check next step
hasNextStep.current = _currentStep < steps.length - 1;
// Handlers
const guessNextAvailableStep = React.useCallback(
(currentIndex: number): number => {
const nextStep = steps[currentIndex + 1];
const isNextStepDisabled = nextStep?.disabled;
if (isNextStepDisabled) {
return guessNextAvailableStep(currentIndex + 1);
}
return currentIndex + 1;
},
[steps]
);
const guessPreviousAvailableStep = React.useCallback(
(currentIndex: number) => {
const previousStep = steps[currentIndex - 1];
const isPreviousStepDisabled = previousStep?.disabled;
if (isPreviousStepDisabled) {
return guessPreviousAvailableStep(currentIndex - 1);
}
return currentIndex - 1;
},
[steps]
);
const nextStep = React.useCallback(() => {
if (_currentStep === steps?.length - 1) return;
setCurrentStep((currentStep) => {
return guessNextAvailableStep(currentStep);
});
}, [_currentStep, guessNextAvailableStep, steps?.length]);
const prevStep = React.useCallback(() => {
if (_currentStep === 0) return;
setCurrentStep((currentStep) => {
return guessPreviousAvailableStep(currentStep);
});
}, [_currentStep, guessPreviousAvailableStep]);
const _getNextArrayItem = (el: HTMLElement) => {
const currentIndex = stepElementsRef.current.indexOf(el);
const nextItem = (currentIndex + 1) % stepElementsRef.current.length;
return stepElementsRef.current[nextItem];
};
const _getPreviousArrayItem = (el: HTMLElement) => {
const currentIndex = stepElementsRef.current.indexOf(el);
const previousItem =
(currentIndex - 1 + stepElementsRef.current.length) %
stepElementsRef.current.length;
return stepElementsRef.current[previousItem];
};
// handle keydown
const handleKeydown = React.useCallback(
(event: KeyboardEvent<HTMLElement>, index: number) => {
const kbLeftRightArrowsKeys = [37, 39];
const kbEnterSpaceKeys = [13, 32];
const previousElement = stepElementsRef.current[index - 1];
const nextElement = stepElementsRef.current[index + 1];
const nextElToFocus = _getNextArrayItem(nextElement);
const prevElToFocus = _getPreviousArrayItem(previousElement);
// Only focus if the next element is not disabled
const isNextElementDisabled = nextElement?.getAttribute('aria-disabled');
const isNextElementDisabledBoolean = isNextElementDisabled === 'true';
const isPreviousElementDisabled =
previousElement?.getAttribute('aria-disabled');
const isPreviousElementDisabledBoolean =
isPreviousElementDisabled === 'true';
if (kbLeftRightArrowsKeys.includes(event?.keyCode)) {
event?.preventDefault();
// Focus prev or next step when user press left or right arrow
if (event?.keyCode === 39) {
if (isNextElementDisabledBoolean) {
nextElToFocus?.focus();
currentStepFocused.current = stepElementsRef.current.findIndex(
(el) => el.id === nextElToFocus.id
);
return;
}
stepElementsRef.current[index + 1]?.focus();
currentStepFocused.current = index + 1;
}
if (event?.keyCode === 37) {
if (isPreviousElementDisabledBoolean) {
prevElToFocus?.focus();
currentStepFocused.current = stepElementsRef.current.findIndex(
(el) => el.id === prevElToFocus.id
);
return;
}
stepElementsRef.current[index - 1]?.focus();
currentStepFocused.current = index - 1;
}
}
if (kbEnterSpaceKeys.includes(event?.keyCode)) {
// If user press enter or space, go to next step
setCurrentStep(currentStepFocused.current as number);
}
},
[]
);
// build the stepper props.
const stepperProps: HTMLAttributes<HTMLElement> = React.useMemo(
() => ({
role: 'tablist',
'aria-orientation': orientation,
}),
[orientation]
);
// Build the steps props.
const stepsProps = React.useMemo(() => {
// TODO: Maybe return aria-controls?
return steps?.map((step, index) => {
return {
role: 'tab',
tabIndex: index === _currentStep ? 0 : -1,
id: `${stepperId}-${index}`,
'aria-posinset': index + 1,
'aria-setsize': steps.length,
'aria-disabled': step?.disabled,
'aria-current': index === _currentStep ? 'step' : undefined,
'aria-selected': index === _currentStep,
onKeyDown: (event) => handleKeydown(event, index),
onClick: () => (step.disabled ? null : setCurrentStep(index)),
ref: (element: HTMLElement) =>
(stepElementsRef.current[index] = element),
} as HTMLAttributes<HTMLElement>;
});
}, [_currentStep, handleKeydown, stepperId, steps]);
// build the progress bar props.
const progressProps = React.useMemo<HTMLAttributes<HTMLElement>>(
() => ({
'aria-valuenow': _currentStep,
'aria-valuemin': 0,
'aria-valuemax': steps.length - 1,
role: 'progressbar',
'aria-labelledby': labelId,
tabIndex: -1, // prevent focus
}),
[_currentStep, labelId, steps]
);
// build the stepper state.
const state = React.useMemo(
() => ({
labelId,
progressId: progressId as string,
currentStep: _currentStep,
hasPreviousStep: hasPreviousStep.current,
hasNextStep: hasNextStep.current,
totalSteps: totalSteps.current,
}),
[progressId, labelId, _currentStep]
);
return {
stepperProps,
stepsProps,
progressProps,
state,
nextStep,
prevStep,
setStep: setCurrentStep,
};
};
export {
useStepper,
StepperProps,
StepperState,
UseStepper,
// Reexport internal types
Steps,
};