Skip to content

Commit 3151cfd

Browse files
committed
Update the value type to string
1 parent 8d7d45d commit 3151cfd

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

examples/containers/ModalPicker.tsx

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
1-
import { Fragment, useState } from 'react'
1+
import { Fragment, useCallback, useState } from 'react'
22
import { Dialog, Transition } from '@headlessui/react'
33
import Picker, { PickerValue } from 'react-mobile-picker'
44

55
function getDayArray(year: number, month: number) {
66
const dayCount = new Date(year, month, 0).getDate()
7-
return Array.from({ length: dayCount }, (_, i) => i + 1)
7+
return Array.from({ length: dayCount }, (_, i) => String(i + 1).padStart(2, '0'))
88
}
99

1010
export default function ModalPicker() {
1111
const [isOpen, setIsOpen] = useState(false)
1212
const [pickerValue, setPickerValue] = useState<PickerValue>({
13-
year: 1989,
14-
month: 8,
15-
day: 12,
13+
year: '1989',
14+
month: '08',
15+
day: '12',
1616
})
1717

18+
const handlePickerChange = useCallback((newValue: PickerValue, key: string) => {
19+
if (key === 'day' ) {
20+
setPickerValue(newValue)
21+
return
22+
}
23+
24+
const { year, month } = newValue
25+
const newDayArray = getDayArray(Number(year), Number(month))
26+
const newDay = newDayArray.includes(newValue.day) ? newValue.day : newDayArray[newDayArray.length - 1]
27+
setPickerValue({ ...newValue, day: newDay })
28+
}, [])
29+
1830
return (
1931
<>
2032
<button
@@ -60,11 +72,11 @@ export default function ModalPicker() {
6072
<div className="mt-2">
6173
<Picker
6274
value={pickerValue}
63-
onChange={setPickerValue}
75+
onChange={handlePickerChange}
6476
wheelMode="natural"
6577
>
6678
<Picker.Column name="year">
67-
{ Array.from({ length: 100 }, (_, i) => 1923 + i).map((year) => (
79+
{ Array.from({ length: 100 }, (_, i) => `${1923 + i}`).map((year) => (
6880
<Picker.Item key={year} value={year}>
6981
{({ selected }) => (
7082
<div className={selected ? 'font-semibold text-neutral-900' : 'text-neutral-400'}>{year}</div>
@@ -73,7 +85,7 @@ export default function ModalPicker() {
7385
))}
7486
</Picker.Column>
7587
<Picker.Column name="month">
76-
{ Array.from({ length: 12 }, (_, i) => i + 1).map((month) => (
88+
{ Array.from({ length: 12 }, (_, i) => String(i + 1).padStart(2, '0')).map((month) => (
7789
<Picker.Item key={month} value={month}>
7890
{({ selected }) => (
7991
<div className={selected ? 'font-semibold text-neutral-900' : 'text-neutral-400'}>{month}</div>
@@ -82,7 +94,7 @@ export default function ModalPicker() {
8294
))}
8395
</Picker.Column>
8496
<Picker.Column name="day">
85-
{ getDayArray(pickerValue.year, pickerValue.month).map((day) => (
97+
{ getDayArray(Number(pickerValue.year), Number(pickerValue.month)).map((day) => (
8698
<Picker.Item key={day} value={day}>
8799
{({ selected }) => (
88100
<div className={selected ? 'font-semibold text-neutral-900' : 'text-neutral-400'}>{day}</div>

lib/components/Picker.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface Option {
1111
}
1212

1313
export interface PickerValue {
14-
[key: string]: any
14+
[key: string]: string
1515
}
1616

1717
export interface PickerRootProps extends Omit<HTMLProps<HTMLDivElement>, 'value' | 'onChange'> {

lib/components/PickerItem.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface PickerItemRenderProps {
99

1010
export interface PickerItemProps extends Omit<HTMLProps<HTMLDivElement>, 'value' | 'children'> {
1111
children: ReactNode | ((renderProps: PickerItemRenderProps) => ReactNode)
12-
value: any
12+
value: string
1313
}
1414

1515
function isFunction(functionToCheck: any): functionToCheck is Function {

0 commit comments

Comments
 (0)