Skip to content

Commit 5bb0529

Browse files
committed
fix: .
1 parent 63c6637 commit 5bb0529

File tree

3 files changed

+49
-31
lines changed

3 files changed

+49
-31
lines changed

packages/react-fancy-switch/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@omit/react-fancy-switch",
33
"private": false,
4-
"version": "0.1.0",
4+
"version": "0.1.1",
55
"type": "module",
66
"license": "MIT",
77
"description": "A fancy switch component",

packages/react-fancy-switch/src/components/fancy-switch.tsx

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,20 @@ export const FancySwitch = React.forwardRef<HTMLDivElement, FancySwitchProps>(
106106
}
107107
}, [activeIndex, highlighterIncludeMargin])
108108

109+
const getNextOption = React.useCallback(
110+
(currentIndex: number) => {
111+
return (currentIndex + 1) % options.length
112+
},
113+
[options.length]
114+
)
115+
116+
const getPreviousOption = React.useCallback(
117+
(currentIndex: number) => {
118+
return (currentIndex - 1 + options.length) % options.length
119+
},
120+
[options.length]
121+
)
122+
109123
const handleChange = React.useCallback(
110124
(index: number) => {
111125
radioRefs.current[index]?.focus()
@@ -115,33 +129,25 @@ export const FancySwitch = React.forwardRef<HTMLDivElement, FancySwitchProps>(
115129
[memoizedOptions, onChange]
116130
)
117131

118-
const handleKeyDown = (
119-
event: React.KeyboardEvent<HTMLDivElement>,
120-
index: number
121-
) => {
122-
switch (event.key) {
123-
case 'ArrowDown':
124-
case 'ArrowRight':
125-
event.preventDefault()
126-
handleChange(getNextOption(index))
127-
break
128-
case 'ArrowUp':
129-
case 'ArrowLeft':
130-
event.preventDefault()
131-
handleChange(getPreviousOption(index))
132-
break
133-
default:
134-
break
135-
}
136-
}
137-
138-
const getNextOption = (currentIndex: number) => {
139-
return (currentIndex + 1) % options.length
140-
}
141-
142-
const getPreviousOption = (currentIndex: number) => {
143-
return (currentIndex - 1 + options.length) % options.length
144-
}
132+
const handleKeyDown = React.useCallback(
133+
(event: React.KeyboardEvent<HTMLDivElement>, index: number) => {
134+
switch (event.key) {
135+
case 'ArrowDown':
136+
case 'ArrowRight':
137+
event.preventDefault()
138+
handleChange(getNextOption(index))
139+
break
140+
case 'ArrowUp':
141+
case 'ArrowLeft':
142+
event.preventDefault()
143+
handleChange(getPreviousOption(index))
144+
break
145+
default:
146+
break
147+
}
148+
},
149+
[handleChange, getNextOption, getPreviousOption]
150+
)
145151

146152
React.useImperativeHandle(ref, () => containerRef.current as HTMLDivElement)
147153

website/src/App.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ import {
1212
} from '@/components/ui/form'
1313
import { cn } from './lib/utils'
1414
import { FancySwitch } from '@/components/custom/fancy-switch'
15+
import { useEffect } from 'react'
1516

1617
const orderTypes: OptionValue[] = ['Delivery', 'Pickup', 'Shipping']
17-
const options: { label: string; value: number }[] = [
18-
{ label: 'Publish', value: 1 },
19-
{ label: 'Draft', value: 0 }
18+
const options = [
19+
{ label: 'Publish', value: 1, test: 'H' },
20+
{ label: 'Draft', value: 0, test: 'U' }
2021
]
2122
const pets: { text: string; id: number }[] = [
2223
{ text: 'Car, (AKA Cat)', id: 1 },
@@ -48,6 +49,17 @@ function App() {
4849
console.log(data)
4950
}
5051

52+
useEffect(() => {
53+
const handleKeyDown = (e: KeyboardEvent) => {
54+
if (e.key === 'ArrowUp' || e.key === 'ArrowDown') {
55+
console.log('ArrowUp/ArrowDown')
56+
}
57+
}
58+
59+
window.addEventListener('keydown', handleKeyDown)
60+
return () => window.removeEventListener('keydown', handleKeyDown)
61+
}, [])
62+
5163
return (
5264
<div className="flex min-h-screen place-items-center justify-center p-4">
5365
<div className="mx-auto max-w-4xl">

0 commit comments

Comments
 (0)