2016-10-11 00:18:42 +00:00
|
|
|
/**
|
2018-09-11 22:27:47 +00:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2016-10-11 00:18:42 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-10-11 00:18:42 +00:00
|
|
|
*
|
2018-05-11 02:06:46 +00:00
|
|
|
* @format
|
2016-10-11 00:18:42 +00:00
|
|
|
* @flow
|
|
|
|
*/
|
2018-05-11 02:06:46 +00:00
|
|
|
|
2016-10-11 00:18:42 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const Platform = require('Platform');
|
|
|
|
const React = require('React');
|
|
|
|
const StyleSheet = require('StyleSheet');
|
|
|
|
const Text = require('Text');
|
|
|
|
const TouchableNativeFeedback = require('TouchableNativeFeedback');
|
|
|
|
const TouchableOpacity = require('TouchableOpacity');
|
|
|
|
const View = require('View');
|
|
|
|
|
2018-12-03 07:49:12 +00:00
|
|
|
const invariant = require('invariant');
|
2016-10-11 00:18:42 +00:00
|
|
|
|
2018-09-24 02:24:57 +00:00
|
|
|
import type {PressEvent} from 'CoreEventTypes';
|
|
|
|
|
|
|
|
type ButtonProps = $ReadOnly<{|
|
|
|
|
/**
|
|
|
|
* Text to display inside the button
|
|
|
|
*/
|
|
|
|
title: string,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler to be called when the user taps the button
|
|
|
|
*/
|
|
|
|
onPress: (event?: PressEvent) => mixed,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Color of the text (iOS), or background color of the button (Android)
|
|
|
|
*/
|
|
|
|
color?: ?string,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TV preferred focus (see documentation for the View component).
|
|
|
|
*/
|
|
|
|
hasTVPreferredFocus?: ?boolean,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Text to display for blindness accessibility features
|
|
|
|
*/
|
|
|
|
accessibilityLabel?: ?string,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If true, disable all interactions for this component.
|
|
|
|
*/
|
|
|
|
disabled?: ?boolean,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to locate this view in end-to-end tests.
|
|
|
|
*/
|
|
|
|
testID?: ?string,
|
|
|
|
|}>;
|
|
|
|
|
2016-10-11 00:18:42 +00:00
|
|
|
/**
|
|
|
|
* A basic button component that should render nicely on any platform. Supports
|
|
|
|
* a minimal level of customization.
|
|
|
|
*
|
|
|
|
* <center><img src="img/buttonExample.png"></img></center>
|
|
|
|
*
|
|
|
|
* If this button doesn't look right for your app, you can build your own
|
2017-01-31 20:07:15 +00:00
|
|
|
* button using [TouchableOpacity](docs/touchableopacity.html)
|
|
|
|
* or [TouchableNativeFeedback](docs/touchablenativefeedback.html).
|
2016-10-11 00:18:42 +00:00
|
|
|
* For inspiration, look at the [source code for this button component](https://github.com/facebook/react-native/blob/master/Libraries/Components/Button.js).
|
|
|
|
* Or, take a look at the [wide variety of button components built by the community](https://js.coach/react-native?search=button).
|
|
|
|
*
|
|
|
|
* Example usage:
|
|
|
|
*
|
|
|
|
* ```
|
2017-09-21 19:20:09 +00:00
|
|
|
* import { Button } from 'react-native';
|
|
|
|
* ...
|
|
|
|
*
|
2016-10-11 00:18:42 +00:00
|
|
|
* <Button
|
|
|
|
* onPress={onPressLearnMore}
|
|
|
|
* title="Learn More"
|
|
|
|
* color="#841584"
|
|
|
|
* accessibilityLabel="Learn more about this purple button"
|
|
|
|
* />
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-09-24 02:24:57 +00:00
|
|
|
class Button extends React.Component<ButtonProps> {
|
2016-10-11 00:18:42 +00:00
|
|
|
render() {
|
|
|
|
const {
|
|
|
|
accessibilityLabel,
|
|
|
|
color,
|
|
|
|
onPress,
|
|
|
|
title,
|
2017-10-18 19:16:31 +00:00
|
|
|
hasTVPreferredFocus,
|
2016-10-15 02:39:00 +00:00
|
|
|
disabled,
|
2017-01-09 10:37:56 +00:00
|
|
|
testID,
|
2016-10-11 00:18:42 +00:00
|
|
|
} = this.props;
|
|
|
|
const buttonStyles = [styles.button];
|
|
|
|
const textStyles = [styles.text];
|
2017-06-27 18:23:53 +00:00
|
|
|
if (color) {
|
|
|
|
if (Platform.OS === 'ios') {
|
|
|
|
textStyles.push({color: color});
|
|
|
|
} else {
|
|
|
|
buttonStyles.push({backgroundColor: color});
|
|
|
|
}
|
2016-10-11 00:18:42 +00:00
|
|
|
}
|
2018-07-26 06:37:16 +00:00
|
|
|
const accessibilityStates = [];
|
2016-10-15 02:39:00 +00:00
|
|
|
if (disabled) {
|
|
|
|
buttonStyles.push(styles.buttonDisabled);
|
|
|
|
textStyles.push(styles.textDisabled);
|
2018-07-26 06:37:16 +00:00
|
|
|
accessibilityStates.push('disabled');
|
2016-10-15 02:39:00 +00:00
|
|
|
}
|
2016-10-11 00:18:42 +00:00
|
|
|
invariant(
|
|
|
|
typeof title === 'string',
|
|
|
|
'The title prop of a Button must be a string',
|
|
|
|
);
|
2018-05-11 02:06:46 +00:00
|
|
|
const formattedTitle =
|
|
|
|
Platform.OS === 'android' ? title.toUpperCase() : title;
|
|
|
|
const Touchable =
|
|
|
|
Platform.OS === 'android' ? TouchableNativeFeedback : TouchableOpacity;
|
2016-10-11 00:18:42 +00:00
|
|
|
return (
|
|
|
|
<Touchable
|
|
|
|
accessibilityLabel={accessibilityLabel}
|
2018-07-26 06:37:16 +00:00
|
|
|
accessibilityRole="button"
|
|
|
|
accessibilityStates={accessibilityStates}
|
2017-10-18 19:16:31 +00:00
|
|
|
hasTVPreferredFocus={hasTVPreferredFocus}
|
2017-01-09 10:37:56 +00:00
|
|
|
testID={testID}
|
2016-10-15 02:39:00 +00:00
|
|
|
disabled={disabled}
|
2016-10-11 00:18:42 +00:00
|
|
|
onPress={onPress}>
|
|
|
|
<View style={buttonStyles}>
|
2018-05-11 02:06:46 +00:00
|
|
|
<Text style={textStyles} disabled={disabled}>
|
|
|
|
{formattedTitle}
|
|
|
|
</Text>
|
2016-10-11 00:18:42 +00:00
|
|
|
</View>
|
|
|
|
</Touchable>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
button: Platform.select({
|
|
|
|
ios: {},
|
|
|
|
android: {
|
|
|
|
elevation: 4,
|
2017-06-27 18:23:53 +00:00
|
|
|
// Material design blue from https://material.google.com/style/color.html#color-color-palette
|
|
|
|
backgroundColor: '#2196F3',
|
2016-10-11 00:18:42 +00:00
|
|
|
borderRadius: 2,
|
|
|
|
},
|
|
|
|
}),
|
2018-10-09 06:19:24 +00:00
|
|
|
text: {
|
|
|
|
textAlign: 'center',
|
|
|
|
padding: 8,
|
|
|
|
...Platform.select({
|
|
|
|
ios: {
|
|
|
|
// iOS blue from https://developer.apple.com/ios/human-interface-guidelines/visual-design/color/
|
|
|
|
color: '#007AFF',
|
|
|
|
fontSize: 18,
|
|
|
|
},
|
|
|
|
android: {
|
|
|
|
color: 'white',
|
|
|
|
fontWeight: '500',
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
2016-10-15 02:39:00 +00:00
|
|
|
buttonDisabled: Platform.select({
|
|
|
|
ios: {},
|
|
|
|
android: {
|
|
|
|
elevation: 0,
|
|
|
|
backgroundColor: '#dfdfdf',
|
2018-05-11 02:06:46 +00:00
|
|
|
},
|
2016-10-15 02:39:00 +00:00
|
|
|
}),
|
|
|
|
textDisabled: Platform.select({
|
|
|
|
ios: {
|
|
|
|
color: '#cdcdcd',
|
|
|
|
},
|
|
|
|
android: {
|
|
|
|
color: '#a1a1a1',
|
2018-05-11 02:06:46 +00:00
|
|
|
},
|
2016-10-15 02:39:00 +00:00
|
|
|
}),
|
2016-10-11 00:18:42 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = Button;
|