1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-12 08:54:00 +02:00
joplin/ReactNativeClient/lib/components/checkbox.js

72 lines
1.8 KiB
JavaScript
Raw Normal View History

2019-07-29 15:43:53 +02:00
const React = require('react');
const Component = React.Component;
2019-07-29 15:58:33 +02:00
const { View, TouchableHighlight } = require('react-native');
const Icon = require('react-native-vector-icons/Ionicons').default;
2020-02-09 16:51:12 +02:00
Icon.loadFont();
2017-05-24 22:51:50 +02:00
2017-07-21 23:40:02 +02:00
const styles = {
2017-05-24 22:51:50 +02:00
checkboxIcon: {
fontSize: 20,
height: 22,
2019-10-09 21:35:13 +02:00
// marginRight: 10,
2017-05-24 22:51:50 +02:00
},
2017-07-21 23:40:02 +02:00
};
2017-05-16 21:12:37 +02:00
class Checkbox extends Component {
2017-05-24 22:51:50 +02:00
constructor() {
super();
this.state = {
checked: false,
2019-07-29 15:43:53 +02:00
};
2017-05-24 22:51:50 +02:00
}
UNSAFE_componentWillMount() {
this.setState({ checked: this.props.checked });
2017-05-24 22:51:50 +02:00
}
UNSAFE_componentWillReceiveProps(newProps) {
2017-07-15 20:13:31 +02:00
if ('checked' in newProps) {
this.setState({ checked: newProps.checked });
}
}
2017-06-06 22:01:43 +02:00
onPress() {
const newChecked = !this.state.checked;
2017-05-24 22:51:50 +02:00
this.setState({ checked: newChecked });
if (this.props.onChange) this.props.onChange(newChecked);
}
render() {
const iconName = this.state.checked ? 'md-checkbox-outline' : 'md-square-outline';
const style = this.props.style ? Object.assign({}, this.props.style) : {};
2017-07-15 20:37:17 +02:00
style.justifyContent = 'center';
style.alignItems = 'center';
const checkboxIconStyle = Object.assign({}, styles.checkboxIcon);
2017-07-21 23:40:02 +02:00
if (style.color) checkboxIconStyle.color = style.color;
2017-08-19 23:59:08 +02:00
if (style.paddingTop) checkboxIconStyle.marginTop = style.paddingTop;
if (style.paddingBottom) checkboxIconStyle.marginBottom = style.paddingBottom;
if (style.paddingLeft) checkboxIconStyle.marginLeft = style.paddingLeft;
if (style.paddingRight) checkboxIconStyle.marginRight = style.paddingRight;
2017-07-21 23:40:02 +02:00
const thStyle = {
justifyContent: 'center',
alignItems: 'center',
};
2019-07-29 15:43:53 +02:00
if (style && style.display === 'none') return <View />;
2017-11-19 02:23:18 +02:00
2019-10-09 21:35:13 +02:00
// if (style.display) thStyle.display = style.display;
2017-07-21 23:40:02 +02:00
2017-05-24 22:51:50 +02:00
return (
2017-07-21 23:40:02 +02:00
<TouchableHighlight onPress={() => this.onPress()} style={thStyle}>
2019-07-29 15:43:53 +02:00
<Icon name={iconName} style={checkboxIconStyle} />
2017-05-24 22:51:50 +02:00
</TouchableHighlight>
);
}
2017-05-16 21:12:37 +02:00
}
2019-07-29 15:43:53 +02:00
module.exports = { Checkbox };