-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from webdriverio/feat/add-testproperties
This PR makes the app more accessible and also beter to use for automation
- Loading branch information
Showing
11 changed files
with
253 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import React, { Component } from 'react'; | ||
import { | ||
ActivityIndicator, | ||
StyleSheet, | ||
Text, | ||
TouchableOpacity, | ||
View, | ||
ViewPropTypes | ||
} from 'react-native'; | ||
import PropTypes from 'prop-types'; | ||
import { testProperties } from '../config/TestProperties'; | ||
|
||
class Button extends Component { | ||
static propTypes = { | ||
backgroundColor: PropTypes.string, | ||
containerStyle: ViewPropTypes.style, | ||
disabled: PropTypes.bool, | ||
loading: PropTypes.bool, | ||
onPress: PropTypes.func.isRequired, | ||
testID: PropTypes.string, | ||
text: PropTypes.string, | ||
textStyle: Text.propTypes.style, | ||
}; | ||
|
||
static defaultProps = { | ||
backgroundColor: 'transparent', | ||
containerStyle: null, | ||
disabled: false, | ||
loading: false, | ||
testID: null, | ||
text: null, | ||
textStyle: null, | ||
onDisabledPress: () => {}, | ||
}; | ||
|
||
onButtonPress = () => { | ||
const { disabled, onDisabledPress, onPress, loading } = this.props; | ||
|
||
if (loading) return; | ||
|
||
if (disabled) { | ||
return onDisabledPress(); | ||
} | ||
|
||
return onPress(); | ||
}; | ||
|
||
get textStyle() { | ||
const { icon, loading, textStyle, disabled } = this.props; | ||
const style = [styles.text]; | ||
|
||
if (icon || loading) { | ||
style.push(styles.textOffset); | ||
} | ||
|
||
style.push(textStyle); | ||
|
||
if (disabled) { | ||
style.push(styles.disabledText); | ||
} | ||
|
||
return style; | ||
} | ||
|
||
renderText = () => { | ||
const { text } = this.props; | ||
|
||
if (this.props.loading) { | ||
return null; | ||
} | ||
|
||
if (text) { | ||
return <Text style={this.textStyle}>{text}</Text>; | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
renderIcon = () => { | ||
if (this.props.loading) { | ||
return <ActivityIndicator size="small"/>; | ||
} | ||
|
||
return this.props.icon; | ||
}; | ||
|
||
render() { | ||
const { | ||
text, | ||
disabled, | ||
testID, | ||
backgroundColor, | ||
containerStyle, | ||
} = this.props; | ||
|
||
return ( | ||
<TouchableOpacity | ||
style={[styles.container, containerStyle]} | ||
onPress={this.onButtonPress} | ||
activeOpacity={disabled ? 1 : undefined} | ||
{...testProperties(`button-${testID || text}`, true)} | ||
> | ||
<View style={[styles.content, disabled ? styles.disabledContent : { backgroundColor }]}> | ||
{this.renderIcon()} | ||
{this.renderText()} | ||
</View> | ||
</TouchableOpacity> | ||
); | ||
} | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
minHeight: 40, | ||
}, | ||
content: { | ||
flex: 1, | ||
flexDirection: 'row', | ||
paddingHorizontal: 15, | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
}, | ||
disabledContent: { | ||
backgroundColor: '#E0E6E8', | ||
}, | ||
text: { | ||
textAlign: 'center', | ||
color: '#000', | ||
alignSelf: 'center', | ||
}, | ||
textOffset: { | ||
marginLeft: 5, | ||
}, | ||
disabledText: { | ||
color: '#252525', | ||
}, | ||
}); | ||
|
||
export default Button; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Add a unique test id for iOS and Android | ||
* | ||
* @param {string} id | ||
* @param {boolean} disableAccessible All touchable elements are accessible, meaning that it | ||
* groups its children into a single selectable component, sometimes this is not needed for testing | ||
* | ||
* @return {object} | ||
*/ | ||
import { IS_IOS } from './Constants'; | ||
|
||
function testProperties(id, disableAccessible = false) { | ||
const disableAccessibility = disableAccessible ? { accessible: false } : {}; | ||
|
||
if (IS_IOS) { | ||
return { ...disableAccessibility, testID: id }; | ||
} | ||
|
||
return { ...disableAccessibility, accessibilityLabel: id }; | ||
} | ||
|
||
export { testProperties }; |
Oops, something went wrong.