Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

feat: add better typing for items and onValueChange #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jekel1818
Copy link

No description provided.

@@ -45,7 +47,7 @@ declare module "react-native-picker-module" {
* Callback function run after value changed
* @param value
*/
onValueChange: (value: any) => void
onValueChange: (value: string) => void
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the android code, I think the only possible value is a string

final String finalValue = value;
...
callback.invoke(finalValue);

@@ -20,6 +20,8 @@ declare module "react-native-picker-module" {
hide: () => void,
}

export type Item = number | string | { label: string; value: number | string }
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the android code, I think items could have a more specific type than any

        if (mDataset.getType(position) == ReadableType.Map) {
            if (mDataset.getMap(position).getType("value") != ReadableType.Null) {
                if (mDataset.getMap(position).getType("value") == ReadableType.String) {
                    value = mDataset.getMap(position).getString("value");
                } else {
                    double number = mDataset.getMap(position).getDouble("value");
                    if (number == Math.rint(number)) {
                        value = String.valueOf((int) number);
                    } else {
                        value = String.valueOf(number);
                    }
                }
                if (mDataset.getMap(position).getType("label") == ReadableType.String) {
                    text = mDataset.getMap(position).getString("label");
                } else {
                    double number = mDataset.getMap(position).getDouble("label");
                    if (number == Math.rint(number)) {
                        text = String.valueOf((int) number);
                    } else {
                        text = String.valueOf(number);
                    }
                }
            }
        } else if (mDataset.getType(position) == ReadableType.String) {
            text = mDataset.getString(position);
            value = mDataset.getString(position);
        } else if (mDataset.getType(position) == ReadableType.Number) {
            double number = mDataset.getDouble(position);
            if (number == Math.rint(number)) {
                text = String.valueOf((int) number);
                value = String.valueOf((int) number);
            } else {
                text = String.valueOf(number);
                value = String.valueOf(number);
            }
        }

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant