TronNatthakorn / react-native-wheel-pick

Apache License 2.0
177 stars 85 forks source link

`onValueChange` callback is not passed the correct type #20

Closed tw0po1nt closed 1 year ago

tw0po1nt commented 1 year ago

I am using this project with TypeScript, and it works well, but there is one problem that is slightly annoying:

I have the following:

interface TimeSlot {
    label: string;
    value: string;
}

const pickerData: TimeSlot[] = ...;
const [selectedSlot, setSelectedSlot] = useState<TimeSlot | undefined>();

// Render
return (
    <Picker
          selectedValue={selectedSlot}
          textSize={26}
          textColor='black'
          pickerData={todaySlots}
          onValueChange={(value: TimeSlot) => console.log(value) } />
);

I expect the value param passed to onValueChange to be of type TimeSlot and indeed, the checker tells me it is. But when I console.log(value), I get a string "2022-07-13T19:00:22.730Z" instead of {"label": "14:00", "value": "2022-07-13T19:00:22.730Z"}

This seems like either a bug or a documentation issue. But the type checker would make me believe it should work like I have it above, but it isn't.

TronNatthakorn commented 1 year ago

@tw0po1nt onValueChange will only return value as string. and label only use for UI show to user.

This time, this library not support for your case. And I have no more time to fix for your case.

In the future if I have time I will be back and also support value other type than string. (This time only string)

Documentation is not clear, too. (I will edit note for other for this time.) I am not sure when I free from other work to support this.

Pull request are also welcome, :)

One more thing It will also affect selectedValue prop.

tw0po1nt commented 1 year ago

I did find a workaround, which is good. Essentially, I just search my data source for the value passed to the callback, and then update my state backing the selectedValue. If I have some time this weekend, I'll see about implementing the behavior I proposed in the initial issue, and opening a PR. Would gladly contribute!