How to attach accessiblityLabel to CheckBox from react-native-elements - react-native

I'm trying to prepare my React native code for testing with Appium 2.
We use CheckBox from react-native-elements.
I tried to add accessibilityLabel like this
import {CheckBox} from 'react-native-elements'
<Checkbox
accessibilityLabel={accessibilityLabel}
checkedIcon={<CheckboxCheckedIcon />}
uncheckedIcon={<CheckboxUncheckedIcon />}
onPress={() => onChange?.(!checked)}
checked={checked}
containerStyle={{height: SIZE, width: SIZE, margin: 0, padding: 2,...customContainerStyle}}
/>
But when I open it with Appium inspector the accessibility id is not there. So I think it is not propagated correctly.
is it even possible?
how do you achieve accessibility/testability of such checkbox?
thanks for any tips&tricks

Related

How to change color in Input from react native elements

I have an Input from react native elements which looks like this
<Input
placeholderTextColor={constants.inputPlaceholderFontColor}
inputContainerStyle={{marginTop: 30, borderBottomColor: constants.dimmedFontColor}}
placeholder='Spieleranzahl'
keyboardType='numeric'
leftIconContainerStyle={{marginRight: 10, marginBottom: 8}}
leftIcon={
<Icon
name='user'
size={ 24 }
color= {constants.iconColor}/>
}
onChangeText={input => this.setState({numberOfPlayers: input})}
I tried to set the color by
style={{color: 'white'}}
inputStyle={{color: 'white'}}
inputContainerStyle={{color: 'white'}}
The documentation says: "This component inherits all native TextInput props that come with a standard React Native TextInput element, along with the following..." so I dont understand why the style property doesn't work because it works with the standard TextInput component.
Also, the documentation says about inputStyle: "style that will be passed to the style props of the React Native TextInput" so that should also work because this is the way to set color on the standard Text component.
Am I missing something?
I've created an example on snack.expo and inputStyle works perfectly on both iOS and Android. Most probably there is another issue, that's why I would recommend to reimplement my simple example and see if it works.
Update: Maybe only your placeholdertext is shown. I can't see the place in your code, where you pass the value prop to your input.
Demo:
https://snack.expo.io/Yunjp2ozw
Output:
Code:
export default function App() {
const [text, setText] = React.useState('Test');
return (
<View style={styles.container}>
<Input
value={text}
onChangeText={(text) => setText(text)}
inputStyle={{'color': 'red'}}
/>
</View>
);
}

How to change button fontSize in React Native?

How can I change the following button's font size? Using the style attribute with fontSize doesn't work.
<Button
style={{fontSize: 32}}
uppercase={false}
mode="contained">
Some text
</Button>
The React Native Button is very limited in what you can do, see; https://facebook.github.io/react-native/docs/button
It does not have a style prop, and you don't set fontSize.
If you want to have more control over the appearance you should use one of the TouchableXXXX' components like TouchableOpacity They are really easy to use :-)
I have make a button for you . hope it will useful for you
<TouchableOpacity style={{height:50,backgroundColor:"skyblue",alignItems:'center',justifyContent:'center'}}>
<Text style={{fontSize:32,}}>Some Text</Text>
</TouchableOpacity>
I use the Text component inside TouchableXXX for every button, it's more flexible and works fine, you can also try to make you own button component and passing props that you want to control (fontsize, colors ...):
<TouchableHighlight onPress={handelPress} style={styles.buttonStyle}>
<Text style={styles.buttonTextStyle}>Click here</Text>
</TouchableHighlight>
If you are using 'react-native-elements' button, use titleStyle to set font size.
import {Input, Button} from 'react-native-elements';
<Button
titleStyle={{
color: "white",
fontSize: 16,
}}
buttonStyle={{
backgroundColor: "white",
borderRadius: 60,
flex: 1,
}}
title="Click here"
/>
For React-native Button, you can use use TouchableOpacity.
<TouchableOpacity style={{height:50}}>
<Text style={{fontSize:36}}>Click here</Text>
</TouchableOpacity>

How to position the dropdown menu of React Native Picker

I'm trying to display a dropdown menu in a React Native android app. I used React Native Picker for the purpose, and it seems very limited in styling and positioning the dropdown menu. I cannot get the menu to pop up below the carret (the down arrow button) position.
I tried setting the margin with hope to push the menu down, to no avail.
<Picker
// selectedValue={stateValue}
style={{
height: 36,
width: 261,
}}
onValueChange={itemValue => {
console.log('item value ', itemValue);
}}
>
<Picker.Item key={-1} label={'Search By...'} value="first" />
{this.searchCategory.map((item, index) => (
<Picker.Item key={index} label={item} value={item} />
))}
</Picker>
</View>
The menu always covers the Picker component. I want it to appear below the Picker.
Actual behavior:
Expected behavior:
After some research, this is an Android limitation and it seems there's little we can do with React Native Picker. Some custom packages may give us more control such as https://www.npmjs.com/package/react-native-picker-select, or https://github.com/sohobloo/react-native-modal-dropdown.

visibility hidden on react native not working, to take space even not shown?

I have TouchableOpacity in a space between flex container that I want to take space even not shown,
My code:
<TouchableOpacity
style={showClear && { visibility: 'hidden' }}
onPress={() => this.props.clearCompleted()}>
<Text>Clear Completed</Text>
</TouchableOpacity>
display: none works but it doesn't take space, the code above dont work but does in web?
In my case, I needed to use the element, so I did something like this:
<TextInput style={{opacity: 0, height: 0}} {...props} />
I hope this works for someone else with my problem.
Update
React Native's StyleSheet now supports toggling visibility using display: 'none' and display:flex.
Not all CSS are supported in React Native, that include visibility: hidden or display:none.
To hide a component, not to render it at all, render empty View or null. Or you want to switch a component visibility, verify react's state
<View>
{ !showClear && (
<TouchableOpacity
onPress={() => this.props.clearCompleted()}>
<Text>Clear Completed</Text>
</TouchableOpacity>
}
</View>
showClear is kept in state
As Leu mentioned, you can just render null.
Another option if you want to keep the area used by TouchableOpacity is setting up opacity: 0.0 in style, but then you have to remember to set also disabled={false} in props of the TouchableOpacity, to avoid call clicking action on invisible area
Just setting the opacity to 0 was enough for my use case.
If we want to make sure element takes space on DOM but is kept hidden to user,
Step 1: Hide it from user using opacity: 0 .
Step 2: Disable interactions of hidden element.
Our JSX should be like below:
<IconButton
icon="close"
disabled
style={styles.hiddenElement}
onPress={() => console.log("Pressed")}
/>
Our stylesheet should look like below:
const styles = StyleSheet.create({
...
hiddenElement: {
opacity: 0,
},
...
});

React Native Picker Styling - ANDROID

I'm trying to add some styling to react-native picker, like underlying and set placeholder, but unable to do so.
const styles = StyleSheet.create({
picker: {
textDecorationLine: 'underline'
}
});
<Picker
style={[styles.picker]}>
<Picker.Item label="Country" value="" />
<Picker.Item label="United States" value="US" />
<Picker.Item label="India" value="IN" />
</Picker>
If i use value="", then it shows country as a selectable value, which i don't want.
textDecorationLine is not able to set the underline styling to picker.
Basically, i am looking to create something like this,
where, i can set the color of placeholder as well.
Thanks in adv.
Picker and Picker item's styling is handled natively on Android. You need to define style for Android's SpinnerItem in android/app/src/res/styles.xml see: How to style the standard react-native android picker?
I tried to test the underline but couldn't seem to find anything that worked. Here is couple of workarounds Android spinner with underline appcompat
However, I would simply use react native's components to our advantage. I'd create a new component that wraps React Native's Picker and put the picker in a View with the underline style that renders a placeholder if the Picker's value is undefined.
You can also use this one for make more attractive to your react-native picker.You can add styles to your view so it can easily make changes to your picker.
import React, { Component } from 'react';
import { View, Picker } from 'react-native';
export default class DropdownDemo extends Component{
state = { user: '' }
updateUser = (user) => {
this.setState({ user: user })
}
render(){
return(
<View
style={{
width: 300,
marginTop: 15,
marginLeft:20,
marginRight:20,
borderColor: 'black',
borderBottomWidth:1,
borderRadius: 10,
alignSelf: 'center'
}}>
<Picker
selectedValue={this.state.user}
onValueChange={this.updateUser}
>
<Picker.Item label="Male" value="Male" />
<Picker.Item label="Female" value="Female" />
<Picker.Item label="Other" value="Other" />
</Picker>
</View>
);
}
}
You need to modify your activity theme as follows:
<!-- Base application theme. -->
<style name="AppTheme">
<!-- Customize your theme here. -->
<item name="colorAccent">#color/colorAccent</item>
<item name="android:spinnerStyle">#style/Base.Widget.AppCompat.Spinner.Underlined</item>
...
</style>
The colorAccent controls the color of the underline, and using #style/Base.Widget.AppCompat.Spinner.Underline provides the underline on your Picker.
You can use this technique to set the styling on almost any android component in React. android:editTextStyle, android:textViewStyle, etc.
Unfortunately, because the React Picker extends Spinner and not AppCompatSpinner, the styling will be a bit different if you're running on API < 21.
The issue here is an assumption that Picker has the properties and styling of TextInput, which it does not. There's a related question here: Have a placeholder for react native picker where a comment outlines what you'll need to render something akin to placeholder text.
In order to achieve something like to textDecorationLine you might apply a borderBottomWidth style to the component.
Also, remember to bind the selectedValue and onValueChange props:
selectedValue={this.state.country} onValueChange={(country) => this.setState({country: country})}>
This is the closest I can get on iOS: https://snack.expo.io/r1L7DGb1Z
Few things to note:
1) there is itemStyle property for setting the specific style for single picker item
2) In order to get down arrow, you have to mimic it manually. You would probably like to attach it's functionality with TouchableHighlight (which isn't done in the example)
3) This doesn't look similar in the Android, so you probably need to append additional, platform specific styling.
This piece of the code is working on my machine:
<View style={{borderBottomWidth:1, borderColor: 'rgb(204, 204,
204)',width: "28%"}}>
<Picker
selectedValue={this.state.pickerValue}
style={[styles.textInputBox]}
onValueChange={this.onChange}>
<Picker.Item label="+31" value="+31" />
<Picker.Item label="+41" value="+41" />
<Picker.Item label="+51" value="+51" />
<Picker.Item label="+61" value="+61" />
</Picker>
</View>