diff --git a/src/components/CheckboxWithLabel.js b/src/components/CheckboxWithLabel.js index 623bbce3af4a..ae8a027f835a 100644 --- a/src/components/CheckboxWithLabel.js +++ b/src/components/CheckboxWithLabel.js @@ -68,7 +68,7 @@ const CheckboxWithLabel = (props) => { ]} > {props.label && ( - + {props.label} )} diff --git a/src/components/RadioButton.js b/src/components/RadioButton.js new file mode 100644 index 000000000000..79be7ca7e9d3 --- /dev/null +++ b/src/components/RadioButton.js @@ -0,0 +1,49 @@ +import React from 'react'; +import {View, Pressable} from 'react-native'; +import PropTypes from 'prop-types'; +import styles from '../styles/styles'; +import Icon from './Icon'; +import * as Expensicons from './Icon/Expensicons'; + +const propTypes = { + /** Whether radioButton is checked */ + isChecked: PropTypes.bool.isRequired, + + /** A function that is called when the box/label is pressed */ + onPress: PropTypes.func.isRequired, + + /** Should the input be styled for errors */ + hasError: PropTypes.bool, + + /** Should the input be disabled */ + disabled: PropTypes.bool, +}; + +const defaultProps = { + hasError: false, + disabled: false, +}; + +const RadioButton = props => ( + + + + + +); + +RadioButton.propTypes = propTypes; +RadioButton.defaultProps = defaultProps; +RadioButton.displayName = 'RadioButton'; + +export default RadioButton; diff --git a/src/components/RadioButtonWithLabel.js b/src/components/RadioButtonWithLabel.js new file mode 100644 index 000000000000..f958adb750b4 --- /dev/null +++ b/src/components/RadioButtonWithLabel.js @@ -0,0 +1,91 @@ +import React from 'react'; +import PropTypes from 'prop-types'; +import {View, TouchableOpacity} from 'react-native'; +import _ from 'underscore'; +import styles from '../styles/styles'; +import RadioButton from './RadioButton'; +import Text from './Text'; +import InlineErrorText from './InlineErrorText'; + +const propTypes = { + /** Whether the radioButton is checked */ + isChecked: PropTypes.bool.isRequired, + + /** Called when the radioButton or label is pressed */ + onPress: PropTypes.func.isRequired, + + /** Container styles */ + style: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.object), PropTypes.object]), + + /** Text that appears next to check box */ + label: PropTypes.string, + + /** Component to display for label */ + LabelComponent: PropTypes.func, + + /** Should the input be styled for errors */ + hasError: PropTypes.bool, + + /** Error text to display */ + errorText: PropTypes.string, +}; + +const defaultProps = { + style: [], + label: undefined, + LabelComponent: undefined, + hasError: false, + errorText: '', +}; + +const RadioButtonWithLabel = (props) => { + const LabelComponent = props.LabelComponent; + const defaultStyles = [styles.flexRow, styles.alignItemsCenter]; + const wrapperStyles = _.isArray(props.style) ? [...defaultStyles, ...props.style] : [...defaultStyles, props.style]; + + if (!props.label && !LabelComponent) { + throw new Error('Must provide at least label or LabelComponent prop'); + } + return ( + <> + + + props.onPress()} + style={[ + styles.ml3, + styles.pr2, + styles.w100, + styles.flexRow, + styles.flexWrap, + styles.flexShrink1, + styles.alignItemsCenter, + ]} + > + {props.label && ( + + {props.label} + + )} + {LabelComponent && ()} + + + {!_.isEmpty(props.errorText) && ( + + {props.errorText} + + )} + + ); +}; + +RadioButtonWithLabel.propTypes = propTypes; +RadioButtonWithLabel.defaultProps = defaultProps; +RadioButtonWithLabel.displayName = 'RadioButtonWithLabel'; + +export default RadioButtonWithLabel; diff --git a/src/components/RadioButtons.js b/src/components/RadioButtons.js new file mode 100644 index 000000000000..bda3636ac591 --- /dev/null +++ b/src/components/RadioButtons.js @@ -0,0 +1,49 @@ +import React from 'react'; +import {View} from 'react-native'; +import PropTypes from 'prop-types'; +import _ from 'underscore'; +import RadioButtonWithLabel from './RadioButtonWithLabel'; +import styles from '../styles/styles'; + +const propTypes = { + /** List of choices to display via radio buttons */ + items: PropTypes.arrayOf(PropTypes.shape({ + label: PropTypes.string.isRequired, + value: PropTypes.string.isRequired, + })).isRequired, + + /** Callback to fire when selecting a radio button */ + onPress: PropTypes.func.isRequired, +}; + +class RadioButtons extends React.Component { + constructor(props) { + super(props); + + this.state = { + checkedValue: '', + }; + } + + render() { + return ( + + {_.map(this.props.items, item => ( + { + this.setState({checkedValue: item.value}); + return this.props.onPress(item.value); + }} + label={item.label} + /> + ))} + + ); + } +} + +RadioButtons.propTypes = propTypes; + +export default RadioButtons; diff --git a/src/styles/styles.js b/src/styles/styles.js index da7bbcc3170e..dfd070b927d7 100644 --- a/src/styles/styles.js +++ b/src/styles/styles.js @@ -1982,12 +1982,23 @@ const styles = { backgroundColor: colors.white, }, + radioButtonContainer: { + backgroundColor: themeColors.componentBG, + borderRadius: 10, + height: 20, + width: 20, + borderColor: themeColors.icon, + borderWidth: 1, + justifyContent: 'center', + alignItems: 'center', + }, + checkboxContainer: { backgroundColor: themeColors.componentBG, borderRadius: 2, height: 20, width: 20, - borderColor: themeColors.border, + borderColor: themeColors.icon, borderWidth: 1, justifyContent: 'center', alignItems: 'center',