mattermost-mobile/app/components/radio_button/radio_button.js
Miguel Alatzar 2d81b497cf [MM-23520] Port mattermost-redux (#4088)
* Remove mattermost-redux

* Move mm-redux files into app/redux

* Add @redux path to tsconfig.json

* Fix imports

* Install missing dependencies

* Fix tsc errors

* Fix i18n_utils test

* Fix more imports

* Remove redux websocket

* Fix tests

* Rename @redux

* Apply changes from mattermost-redux PR 1103

* Remove mattermost-redux mention in template

* Add missing imports

* Rename app/redux/ to app/mm-redux/

* Remove test file

* Fix fetching Sidebar GM profiles

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
2020-04-17 21:12:09 -07:00

170 lines
4.6 KiB
JavaScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {connect} from 'react-redux';
import React, {PureComponent} from 'react';
import PropTypes from 'prop-types';
import {Animated, Text, View} from 'react-native';
import Icon from 'react-native-vector-icons/MaterialIcons';
import {getTheme} from '@mm-redux/selectors/entities/preferences';
import {makeStyleSheetFromTheme} from 'app/utils/theme';
const DISABLED_OPACITY = 0.26;
const DEFAULT_OPACITY = 1;
class RadioButton extends PureComponent {
static propTypes = {
label: PropTypes.string,
theme: PropTypes.object,
value: PropTypes.string,
checked: PropTypes.bool,
disabled: PropTypes.bool,
onCheck: PropTypes.func,
};
constructor(props) {
super(props);
this.state = {
scaleValue: new Animated.Value(0),
opacityValue: new Animated.Value(0),
};
this.responder = {
onStartShouldSetResponder: () => true,
onResponderGrant: this.highlight,
onResponderRelease: this.handleResponderEnd,
onResponderTerminate: this.unHighlight,
};
}
highlight = () => {
Animated.timing(
this.state.scaleValue,
{
toValue: 1,
duration: 150,
},
).start();
Animated.timing(
this.state.opacityValue,
{
toValue: 0.1,
duration: 100,
},
).start();
};
unHighlight = () => {
Animated.timing(
this.state.scaleValue,
{
toValue: 0.001,
duration: 1500,
},
).start();
Animated.timing(
this.state.opacityValue,
{
toValue: 0,
},
).start();
};
handleResponderEnd = () => {
const {checked, disabled, onCheck, value} = this.props;
if (!checked && !disabled && onCheck) {
onCheck(value);
}
this.unHighlight();
};
render() {
const {scaleValue, opacityValue} = this.state;
const {theme, checked, disabled} = this.props;
const styles = getStyleSheet(theme);
const color = checked ? theme.buttonBg : theme.centerChannelColor;
const opacity = disabled ? DISABLED_OPACITY : DEFAULT_OPACITY;
return (
<View
style={styles.container}
{...this.responder}
>
<Animated.View
style={[
styles.ripple,
{
transform: [
{scale: scaleValue},
],
opacity: opacityValue,
backgroundColor: color,
},
]}
/>
<Icon
name={checked ? 'radio-button-checked' : 'radio-button-unchecked'}
size={24}
color={color}
style={{
opacity,
}}
/>
<View style={styles.labelContainer}>
<Text
style={[
styles.label,
{opacity: disabled ? DISABLED_OPACITY : DEFAULT_OPACITY},
]}
>
{this.props.label}
</Text>
</View>
</View>
);
}
}
const getStyleSheet = makeStyleSheetFromTheme((theme) => {
return {
container: {
flexDirection: 'row',
alignItems: 'center',
backgroundColor: theme.centerChannelBg,
marginBottom: 15,
},
ripple: {
position: 'absolute',
width: 48,
height: 48,
borderRadius: 56,
top: 6,
},
labelContainer: {
flex: 1,
flexDirection: 'row',
justifyContent: 'center',
},
label: {
color: theme.centerChannelColor,
flex: 1,
fontSize: 17,
marginLeft: 15,
textAlignVertical: 'center',
includeFontPadding: false,
},
};
});
function mapStateToProps(state, ownProps) {
return {
...ownProps,
theme: getTheme(state),
};
}
export default connect(mapStateToProps)(RadioButton);