Skip to content

Instantly share code, notes, and snippets.

@vadimkorr
Last active August 23, 2019 14:44
Show Gist options
  • Save vadimkorr/a039aaefa5a9bee16a3a13202b023830 to your computer and use it in GitHub Desktop.
Save vadimkorr/a039aaefa5a9bee16a3a13202b023830 to your computer and use it in GitHub Desktop.
Passing type via props
import React from "react";
import PropTypes from "prop-types";
import { StyleSheet, TouchableOpacity, Text } from "react-native";
import { FontAwesome } from "@expo/vector-icons";
const ICON_SQUARE_SIZE_PX = 100;
// returns configuration depending on notification type
const getConfigByType = type => {
switch (type) {
case "success":
return {
iconName: "check-circle",
colorPrimary: "#cbf0c4",
colorAccent: "#3c763d"
};
default:
case "info":
return {
iconName: "info-circle",
colorPrimary: "#b1edf7",
colorAccent: "#31708f"
};
case "warning":
return {
iconName: "exclamation-circle",
colorPrimary: "#fcf8bb",
colorAccent: "#8a6d3b"
};
case "error":
return {
iconName: "times-circle",
colorPrimary: "#f2c8c8",
colorAccent: "#a94442"
};
}
};
// one component for all types of notifications
export class NotificationBase extends React.Component {
constructor(props) {
super(props);
// takes extra 'type' prop which determines look of notification
const { type } = this.props;
this.config = getConfigByType(type);
}
render() {
const { title, message, onClosePress } = this.props;
return (
<TouchableOpacity
style={[
styles.mainContainer,
{ backgroundColor: this.config.colorPrimary }
]}
onPress={onClosePress}
>
<FontAwesome
style={styles.icon}
name={this.config.iconName}
size={ICON_SQUARE_SIZE_PX}
color={this.config.colorAccent}
/>
<Text style={[styles.title, { color: this.config.colorAccent }]}>
{title}
</Text>
<Text style={[styles.message, { color: this.config.colorAccent }]}>
{message}
</Text>
</TouchableOpacity>
);
}
}
NotificationBase.propTypes = {
type: PropTypes.oneOf(["success", "info", "warning", "error"]).isRequired,
title: PropTypes.string.isRequired,
message: PropTypes.string.isRequired,
onClosePress: PropTypes.func
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment