File tree Expand file tree Collapse file tree 1 file changed +12
-10
lines changed Expand file tree Collapse file tree 1 file changed +12
-10
lines changed Original file line number Diff line number Diff line change @@ -201,26 +201,28 @@ const Segmented = React.forwardRef<HTMLDivElement, SegmentedProps>(
201
201
} ;
202
202
203
203
// ======================= Keyboard ========================
204
- const handleKeyDown = ( event : React . KeyboardEvent ) => {
204
+ const onOffset = ( offset : number ) => {
205
205
const total = segmentedOptions . length ;
206
- let nextIndex = currentIndex ;
206
+ const nextIndex = ( currentIndex + offset + total ) % total ;
207
+
208
+ const nextOption = segmentedOptions [ nextIndex ] ;
209
+ if ( nextOption ) {
210
+ setRawValue ( nextOption . value ) ;
211
+ onChange ?.( nextOption . value ) ;
212
+ }
213
+ } ;
207
214
215
+ const handleKeyDown = ( event : React . KeyboardEvent ) => {
208
216
switch ( event . key ) {
209
217
case 'ArrowLeft' :
210
218
case 'ArrowUp' :
211
- nextIndex = currentIndex === 0 ? total - 1 : currentIndex - 1 ;
219
+ onOffset ( - 1 ) ;
212
220
break ;
213
221
case 'ArrowRight' :
214
222
case 'ArrowDown' :
215
- nextIndex = currentIndex === total - 1 ? 0 : currentIndex + 1 ;
223
+ onOffset ( 1 ) ;
216
224
break ;
217
225
}
218
-
219
- const nextOption = segmentedOptions [ nextIndex ] ;
220
- if ( nextOption ) {
221
- setRawValue ( nextOption . value ) ;
222
- onChange ?.( nextOption . value ) ;
223
- }
224
226
} ;
225
227
226
228
return (
You can’t perform that action at this time.
0 commit comments