mattermost-mobile/app/components/widgets/settings/radio_setting.js
Elias Nahum 5ea470a235
V1 dependencies and bump to RN 0.67.2 (#5908)
* update dependencies

* eslint fixes

* Upgrade to RN 67

* update other deps

* Update to RN 0.67.2

* fix Android build (mmkv)

* Fix crash when root message is deleted from the thread screen

* Fix gif emoji playing at high speed on iOS ProMotion capable devices
2022-02-02 15:28:57 -03:00

183 lines
5.1 KiB
JavaScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import PropTypes from 'prop-types';
import React, {PureComponent} from 'react';
import {Text, TouchableOpacity, View} from 'react-native';
import CompassIcon from '@components/compass_icon';
import {makeStyleSheetFromTheme, changeOpacity} from '@utils/theme';
export default class RadioSetting extends PureComponent {
static propTypes = {
id: PropTypes.string.isRequired,
label: PropTypes.node.isRequired,
options: PropTypes.array.isRequired,
default: PropTypes.string,
onChange: PropTypes.func.isRequired,
theme: PropTypes.object.isRequired,
helpText: PropTypes.node,
errorText: PropTypes.node,
};
constructor(props) {
super(props);
this.state = {
value: typeof props.default === 'undefined' ? props.options[0] : props.default,
};
}
handleChange = (item) => {
const {onChange, id} = this.props;
onChange(id, item);
this.setState({value: item});
};
renderCheckMark = (value, style) => {
if (value === this.state.value) {
return (
<CompassIcon
name='check'
style={style}
/>
);
}
return null;
};
renderRowSeparator = (idx, separatorStyle) => {
const {options} = this.props;
if (idx === options.length - 1) {
return null;
}
return <View style={separatorStyle}/>;
};
render() {
const {
theme,
label,
helpText,
errorText,
} = this.props;
const style = getStyleSheet(theme);
let helpTextContent;
if (helpText) {
helpTextContent = (
<Text style={style.helpText}>
{helpText}
</Text>
);
}
let errorTextContent;
if (errorText) {
errorTextContent = (
<Text style={style.errorText}>
{errorText}
</Text>
);
}
let additionalTextContent;
if (errorText || helpText) {
additionalTextContent = (
<View >
{helpTextContent}
{errorTextContent}
</View>
);
}
const options = [];
for (const [i, {value, text}] of this.props.options.entries()) {
options.push(
<TouchableOpacity
onPress={() => this.handleChange(value)}
key={value}
>
<View style={style.container}>
<View style={style.rowContainer}>
<Text>{text}</Text>
</View>
{this.renderCheckMark(value, style.checkMark)}
</View>
{this.renderRowSeparator(i, style.separator)}
</TouchableOpacity>,
);
}
return (
<View>
<View style={style.titleContainer}>
<Text style={style.title}>{label}</Text>
<Text style={style.asterisk}>{' *'}</Text>
</View>
<View style={style.items}>
{options}
</View>
{additionalTextContent}
</View>
);
}
}
const getStyleSheet = makeStyleSheetFromTheme((theme) => {
return {
container: {
flexDirection: 'row',
alignItems: 'center',
paddingHorizontal: 15,
},
rowContainer: {
alignItems: 'center',
flex: 1,
flexDirection: 'row',
height: 45,
},
items: {
backgroundColor: theme.centerChannelBg,
borderTopWidth: 1,
borderBottomWidth: 1,
borderTopColor: changeOpacity(theme.centerChannelColor, 0.1),
borderBottomColor: changeOpacity(theme.centerChannelColor, 0.1),
},
helpText: {
fontSize: 12,
color: changeOpacity(theme.centerChannelColor, 0.5),
marginHorizontal: 15,
marginVertical: 10,
},
errorText: {
fontSize: 12,
color: theme.errorTextColor,
marginHorizontal: 15,
marginTop: 10,
},
asterisk: {
color: theme.errorTextColor,
fontSize: 14,
},
title: {
fontSize: 14,
color: theme.centerChannelColor,
marginLeft: 15,
},
titleContainer: {
flexDirection: 'row',
marginTop: 15,
marginBottom: 10,
},
separator: {
backgroundColor: changeOpacity(theme.centerChannelColor, 0.1),
flex: 1,
height: 1,
marginLeft: 15,
},
checkMark: {
fontSize: 12,
color: theme.linkColor,
},
};
});