verdnatura-chat/app/containers/Toast.js

65 lines
1.3 KiB
JavaScript
Raw Normal View History

2019-07-23 14:02:57 +00:00
import React from 'react';
import { StyleSheet } from 'react-native';
import EasyToast from 'react-native-easy-toast';
2019-12-04 16:39:53 +00:00
import PropTypes from 'prop-types';
2019-07-23 14:02:57 +00:00
2019-12-04 16:39:53 +00:00
import { themes } from '../constants/colors';
2019-07-23 14:02:57 +00:00
import sharedStyles from '../views/Styles';
import EventEmitter from '../utils/events';
2019-12-04 16:39:53 +00:00
import { withTheme } from '../theme';
2019-07-23 14:02:57 +00:00
const styles = StyleSheet.create({
toast: {
maxWidth: 300,
padding: 10
},
text: {
...sharedStyles.textRegular,
fontSize: 14,
textAlign: 'center'
}
});
export const LISTENER = 'Toast';
2019-12-04 16:39:53 +00:00
class Toast extends React.Component {
static propTypes = {
theme: PropTypes.string
}
2019-07-23 14:02:57 +00:00
componentDidMount() {
EventEmitter.addEventListener(LISTENER, this.showToast);
}
2019-12-04 16:39:53 +00:00
shouldComponentUpdate(nextProps) {
const { theme } = this.props;
if (nextProps.theme !== theme) {
return true;
}
2019-07-23 14:02:57 +00:00
return false;
}
componentWillUnmount() {
EventEmitter.removeListener(LISTENER);
}
showToast = ({ message }) => {
this.toast.show(message, 1000);
}
render() {
2019-12-04 16:39:53 +00:00
const { theme } = this.props;
2019-07-23 14:02:57 +00:00
return (
<EasyToast
ref={toast => this.toast = toast}
position='center'
2019-12-04 16:39:53 +00:00
style={[styles.toast, { backgroundColor: themes[theme].toastBackground }]}
textStyle={[styles.text, { color: themes[theme].buttonText }]}
2019-07-23 14:02:57 +00:00
opacity={0.9}
/>
);
}
}
2019-12-04 16:39:53 +00:00
export default withTheme(Toast);