NativeBase icon indicating copy to clipboard operation
NativeBase copied to clipboard

Conditional Style?

Open ChiWuSage opened this issue 9 years ago • 5 comments

How can we do conditional style with Nativebase component?

Here is my code. <ListItem onPress={} style={[nativeBaseStyle.notSelected, this.isSelected() && nativeBaseStyle.isSelected]}> <Text>{rowData.firstName}</Text></ListItem>

NativeBaseStyle looks like this. export default{ isSelected:{ backgroundColor: 'blue', }, notSelected:{ backgroundColor: 'transparent', }, }

It works with React Native component but not NativeBase component.

react-native, react and native-base version

react-native 0.41.2 native-base 2.0.12

Expected behaviour

Different style should be loaded base on the condition

Actual behaviour

Gave me the warnings.

Warning: Failed prop type: Invalid prop 'style' of type 'array' supplied to 'Styled(ListItem)', expected 'object'.

Is the bug present in both ios and android or in any one of them?

Only Tested on Android

ChiWuSage avatar May 18 '17 00:05 ChiWuSage

It doesn't work for me either. Looks like NativeBase doesn't support array style format. But you may use ES6:

style={{...nativeBaseStyle.notSelected, ...(this.isSelected() && nativeBaseStyle.isSelected)}}

MichalRemis avatar May 25 '17 15:05 MichalRemis

Hey @ChiWuSage , We are planning to incorporate array style format in newer versions of NativeBase, for now you can use the ES6 as pointed by @remo-sk above. Thanks :)

Jasbir23 avatar May 26 '17 09:05 Jasbir23

#575

SupriyaKalghatgi avatar May 29 '17 10:05 SupriyaKalghatgi

With the latest of NativeBase version 2.3.0. Now all components accepts the styling same way as any other Rn components which includes conditional styling

shivrajkumar avatar Jul 24 '17 06:07 shivrajkumar

@shivrajkumar i'm trying to do ternary condition on ListItem and still throwing me a warning and not working. This is weird.

<ListItem
       style={ this.state.locationService ? scopedStyles.disabledProperty : ''}>
       ...

Shows warning: Failed prop type: Invalid prop style supplied to Styled(ListItem)

msqar avatar Jul 03 '19 01:07 msqar