2023-10-07 15:14:09 -06:00
|
|
|
import React from 'react';
|
|
|
|
|
|
|
|
import {
|
|
|
|
StyleSheet,
|
|
|
|
Text,
|
|
|
|
TextStyle,
|
|
|
|
TouchableOpacity,
|
|
|
|
View,
|
|
|
|
} from 'react-native';
|
|
|
|
|
|
|
|
/*
|
2023-11-22 07:03:57 -07:00
|
|
|
* ToggleControl displays a 2 states clickable text
|
|
|
|
*/
|
2023-10-07 15:14:09 -06:00
|
|
|
|
|
|
|
interface ToggleControlType {
|
|
|
|
// boolean indicating if text is selected state
|
2023-11-22 07:03:57 -07:00
|
|
|
isSelected?: boolean;
|
2023-10-07 15:14:09 -06:00
|
|
|
// value of text when selected
|
2023-11-22 07:03:57 -07:00
|
|
|
selectedText?: string;
|
2023-10-07 15:14:09 -06:00
|
|
|
// value of text when NOT selected
|
2023-11-22 07:03:57 -07:00
|
|
|
unselectedText?: string;
|
2023-10-07 15:14:09 -06:00
|
|
|
// default text if no only one text field is needed
|
2023-11-22 07:03:57 -07:00
|
|
|
text?: string;
|
2023-10-07 15:14:09 -06:00
|
|
|
// callback called when pressing the component
|
2023-11-22 07:03:57 -07:00
|
|
|
onPress: () => void;
|
2023-10-07 15:14:09 -06:00
|
|
|
}
|
|
|
|
|
2023-11-22 07:03:57 -07:00
|
|
|
const ToggleControl = ({
|
|
|
|
isSelected,
|
|
|
|
selectedText,
|
|
|
|
unselectedText,
|
|
|
|
text,
|
|
|
|
onPress,
|
|
|
|
}: ToggleControlType) => {
|
2023-10-07 15:14:09 -06:00
|
|
|
const selectedStyle: TextStyle = StyleSheet.flatten([
|
|
|
|
styles.controlOption,
|
|
|
|
{fontWeight: 'bold'},
|
|
|
|
]);
|
|
|
|
|
|
|
|
const unselectedStyle: TextStyle = StyleSheet.flatten([
|
|
|
|
styles.controlOption,
|
|
|
|
{fontWeight: 'normal'},
|
|
|
|
]);
|
|
|
|
|
2023-11-22 07:03:57 -07:00
|
|
|
const style = isSelected ? selectedStyle : unselectedStyle;
|
|
|
|
const _text = text ? text : isSelected ? selectedText : unselectedText;
|
|
|
|
return (
|
|
|
|
<View style={styles.resizeModeControl}>
|
|
|
|
<TouchableOpacity onPress={onPress}>
|
2023-10-07 15:14:09 -06:00
|
|
|
<Text style={style}>{_text}</Text>
|
|
|
|
</TouchableOpacity>
|
2023-11-22 07:03:57 -07:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
};
|
2023-10-07 15:14:09 -06:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
controlOption: {
|
|
|
|
alignSelf: 'center',
|
|
|
|
fontSize: 11,
|
|
|
|
color: 'white',
|
|
|
|
paddingLeft: 2,
|
|
|
|
paddingRight: 2,
|
|
|
|
lineHeight: 12,
|
|
|
|
},
|
|
|
|
resizeModeControl: {
|
|
|
|
flex: 1,
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
export default ToggleControl;
|