NativeBase icon indicating copy to clipboard operation
NativeBase copied to clipboard

Property color of Picker's Item fires onValueChange

Open Leafgard opened this issue 7 years ago • 4 comments

I have gone through these following points

  • Check latest documentation: https://docs.nativebase.io/
  • Check for existing open/closed issues for a possible duplicate before creating a new issue: https://github.com/GeekyAnts/NativeBase/issues
  • Use the latest NativeBase release: https://github.com/GeekyAnts/NativeBase/releases
  • Check examples from NativeBase KitchenSink https://github.com/GeekyAnts/NativeBase-KitchenSink
  • For discussion purpose make use of NativeBase Slack: http://slack.nativebase.io/
  • For queries related to theme, check Theme Variables from Docs and live NativeBase Theme Editor http://nativebase.io/customizer/

Issue Description

node, npm, react-native, react and native-base version, expo version if used, xcode version

Node: v12.6.0 npm: v6.9.0 react-native: v0.59.10 native-base: ^2.12.1

Project created without Expo, and using Visual Studio Code as editor.

Expected behaviour

Basically, what happens:

  1. I have a dark/white theme switch, which toggles brightness value
  2. I have a Picker containing all the colors available as primary colors for my app (Generated using colors object) Here is a colors object snippet:
red: {
    primary: '#EF5350',
    dark: '#FF867C',
    light: '#B61827'
},
pink: {
    primary: '#EC407A',
    dark: '#FF77A9',
    light: '#B4004E'
}
  1. When I toggle the dark/white theme switch, it should update the color of each Picker Item, depending of the theme I actually chose (Lighter color on dark theme, darker color on white theme according to my previous colors object) WITHOUT calling onValueChange.

Actual behaviour

When I dynamically change the color property of Picker's Items, it fires onValueChange.

Steps to reproduce

<Picker
    mode="dropdown"
    iosIcon={<MaterialIcon name="arrow-down-drop-circle-outline" />}
    selectedValue={this.state.secondarySelected}
    onValueChange={this.onSecondaryChange.bind(this)}>
    { Object.keys(colors).map((val, i) => {
        return <Picker.Item
        label={val.charAt(0).toUpperCase() + val.slice(1)}
        value={i}
        key={i}
        color={colors[val][brightness]} /> // Here
      }) }
</Picker>

On brightness property update (light <=> dark), it refreshes the Picker's Items and fires onValueChange function.

How I fixed this in my case

I fixed this by setting a static color to my Picker's Item's (Not depending anymore of brightness variable):

<Picker
    mode="dropdown"
    iosIcon={<MaterialIcon name="arrow-down-drop-circle-outline" />}
    selectedValue={this.state.secondarySelected}
    onValueChange={this.onSecondaryChange.bind(this)}>
    { Object.keys(colors).map((val, i) => {
        return <Picker.Item
        label={val.charAt(0).toUpperCase() + val.slice(1)}
        value={i}
        key={i}
        color={colors[val].primary} /> // Here
      }) }
</Picker>

Is the bug present in both iOS and Android or in any one of them?

I could only test on Android, but I suppose iOS to have the same issue!

Any other additional info which would help us debug the issue quicker.

I think that the color property of Picker's Item causes value to change, but isn't really

Leafgard avatar Jul 24 '19 18:07 Leafgard

Additional info

I think that just refreshing picker's content causes onValueChange to be triggered because without my brightness variable, I guess that Picker's content isn't changing.. I mean, he has no reason to change!

Leafgard avatar Jul 24 '19 18:07 Leafgard

There is no color property of Picker.Item. If you wish to change the item color, you need to pass it in Picker, see docs: https://docs.nativebase.io/Components.html#picker-text-and-item-text-styles-headref

Srishti-Sharma avatar Jul 29 '19 13:07 Srishti-Sharma

There is ! Picker.Item was changing text color when passing color property... but ok .. I did it because I wanted each item to have a different text color and it was working really well.. Anyway

Leafgard avatar Jul 29 '19 13:07 Leafgard

Anyway, looks like that's my bad, so thanks.

Leafgard avatar Jul 29 '19 13:07 Leafgard