vn-verdnaturachat/app/containers/Toast.tsx

73 lines
1.5 KiB
TypeScript
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 { 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: {
fontSize: 14,
...sharedStyles.textRegular,
...sharedStyles.textAlignCenter
2019-07-23 14:02:57 +00:00
}
});
export const LISTENER = 'Toast';
interface IToastProps {
theme: string;
}
class Toast extends React.Component<IToastProps, any> {
private listener: any;
private toast: any;
2019-12-04 16:39:53 +00:00
2019-07-23 14:02:57 +00:00
componentDidMount() {
this.listener = EventEmitter.addEventListener(LISTENER, this.showToast);
2019-07-23 14:02:57 +00:00
}
shouldComponentUpdate(nextProps: any) {
2019-12-04 16:39:53 +00:00
const { theme } = this.props;
if (nextProps.theme !== theme) {
return true;
}
2019-07-23 14:02:57 +00:00
return false;
}
componentWillUnmount() {
EventEmitter.removeListener(LISTENER, this.listener);
2019-07-23 14:02:57 +00:00
}
getToastRef = (toast: any) => (this.toast = toast);
2020-02-17 16:06:08 +00:00
showToast = ({ message }: any) => {
2020-02-17 16:06:08 +00:00
if (this.toast && this.toast.show) {
this.toast.show(message, 1000);
}
};
2019-07-23 14:02:57 +00:00
render() {
2019-12-04 16:39:53 +00:00
const { theme } = this.props;
2019-07-23 14:02:57 +00:00
return (
<EasyToast
2020-02-17 16:06:08 +00:00
ref={this.getToastRef}
// @ts-ignore
2019-07-23 14:02:57 +00:00
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);