Skip to content

Commit 66b3018

Browse files
authored
Update PickerItem.tsx to allow values of type number (#74)
* Update PickerItem.tsx Allow values for the picker to be strings or numbers, so that I don't have to do custom transformation logic on the values. This is particularly useful for a picker for numeric values like height, where I really want to store it as a number and not a string. * Update corresponding types for value in Picker.tsx to support string | number also * Added type coersion in test. This works, but with this in mind it maybe better to allow the values to be generics if they aren't strings? Would be interested in thoughts
1 parent dfc1f6e commit 66b3018

File tree

4 files changed

+9044
-5
lines changed

4 files changed

+9044
-5
lines changed

examples/containers/ModalPicker.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default function ModalPicker() {
2323

2424
const { year, month } = newValue
2525
const newDayArray = getDayArray(Number(year), Number(month))
26-
const newDay = newDayArray.includes(newValue.day) ? newValue.day : newDayArray[newDayArray.length - 1]
26+
const newDay = newDayArray.includes(newValue.day as string) ? newValue.day : newDayArray[newDayArray.length - 1]
2727
setPickerValue({ ...newValue, day: newDay })
2828
}, [])
2929

lib/components/Picker.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ const DEFAULT_ITEM_HEIGHT = 36
55
const DEFAULT_WHEEL_MODE = 'off'
66

77
interface Option {
8-
value: string
8+
value: string | number
99
element: MutableRefObject<HTMLElement | null>
1010
}
1111

1212
export interface PickerValue {
13-
[key: string]: string
13+
[key: string]: string | number
1414
}
1515

1616
export interface PickerRootProps<TType extends PickerValue> extends Omit<HTMLProps<HTMLDivElement>, 'value' | 'onChange'> {
@@ -44,7 +44,7 @@ export function usePickerData(componentName: string) {
4444

4545
const PickerActionsContext = createContext<{
4646
registerOption(key: string, option: Option): () => void
47-
change(key: string, value: string): boolean
47+
change(key: string, value: string | number): boolean
4848
} | null>(null)
4949
PickerActionsContext.displayName = 'PickerActionsContext'
5050

lib/components/PickerItem.tsx

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

99
export interface PickerItemProps extends Omit<HTMLProps<HTMLDivElement>, 'value' | 'children'> {
1010
children: ReactNode | ((renderProps: PickerItemRenderProps) => ReactNode)
11-
value: string
11+
value: string | number
1212
}
1313

1414
// eslint-disable-next-line

0 commit comments

Comments
 (0)