Rocket.Chat.ReactNative/app/containers/Toast.tsx

75 lines
1.6 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';
import { themes } from '../lib/constants';
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?: Function;
private toast: EasyToast | null | undefined;
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() {
if (this.listener) {
EventEmitter.removeListener(LISTENER, this.listener);
}
2019-07-23 14:02:57 +00:00
}
getToastRef = (toast: EasyToast | null) => (this.toast = toast);
2020-02-17 16:06:08 +00:00
showToast = ({ message }: { message: string }) => {
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'
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);