|
| 1 | +import React from "react"; |
| 2 | +import PropTypes from "prop-types"; |
| 3 | +import classnames from "classnames"; |
| 4 | + |
| 5 | +const colorSchemes = { |
| 6 | + black: "bg-black text-gray-200 border-gray-900", |
| 7 | + white: "bg-white text-gray-600 border-gray-100", |
| 8 | + gray: "bg-gray-200 text-gray-800 border-gray-300", |
| 9 | + red: "bg-red-200 text-red-800 border-red-300", |
| 10 | + orange: "bg-orange-200 text-orange-800 border-orange-300", |
| 11 | + yellow: "bg-yellow-200 text-yellow-800 border-yellow-300", |
| 12 | + green: "bg-green-200 text-green-800 border-green-300", |
| 13 | + teal: "bg-teal-200 text-teal-800 border-teal-300", |
| 14 | + blue: "bg-blue-200 text-blue-800 border-blue-300", |
| 15 | + indigo: "bg-indigo-200 text-indigo-800 border-indigo-300", |
| 16 | + purple: "bg-purple-200 text-purple-800 border-purple-300", |
| 17 | + pink: "bg-pink-200 text-pink-800 border-pink-300" |
| 18 | +}; |
| 19 | + |
| 20 | +const Alert = ({ controlled, color, icon, children }) => { |
| 21 | + const [show, setShow] = React.useState(true); |
| 22 | + const closeAlert = () => { |
| 23 | + setShow(false); |
| 24 | + }; |
| 25 | + if (!show) { |
| 26 | + return null; |
| 27 | + } |
| 28 | + return ( |
| 29 | + <div |
| 30 | + className={classnames( |
| 31 | + "px-5 py-3 border border-solid rounded relative mb-4", |
| 32 | + colorSchemes[color] |
| 33 | + )} |
| 34 | + > |
| 35 | + {icon !== "" ? ( |
| 36 | + <span className="text-xl inline-block mr-5 align-middle"> |
| 37 | + <i className={icon}></i> |
| 38 | + </span> |
| 39 | + ) : null} |
| 40 | + {icon ? ( |
| 41 | + <span className="inline-block align-middle mr-8">{children}</span> |
| 42 | + ) : ( |
| 43 | + children |
| 44 | + )} |
| 45 | + {controlled ? null : ( |
| 46 | + <button |
| 47 | + className="absolute bg-transparent text-2xl font-semibold leading-none right-0 top-0 outline-none focus:outline-none opacity-50 px-5 py-3 hover:opacity-75 hover:text-black" |
| 48 | + onClick={closeAlert} |
| 49 | + > |
| 50 | + <span>×</span> |
| 51 | + </button> |
| 52 | + )} |
| 53 | + </div> |
| 54 | + ); |
| 55 | +}; |
| 56 | + |
| 57 | +Alert.defaultProps = { |
| 58 | + controlled: false, |
| 59 | + color: "pink", |
| 60 | + icon: "" |
| 61 | +}; |
| 62 | + |
| 63 | +Alert.propTypes = { |
| 64 | + // if set to false, then a closing button will be rendered so that the alert can be closed |
| 65 | + // if set to true, then the closing button will not be rendered |
| 66 | + controlled: PropTypes.bool, |
| 67 | + // set the background, border and text color for the alert |
| 68 | + color: PropTypes.oneOf([ |
| 69 | + "black", |
| 70 | + "white", |
| 71 | + "gray", |
| 72 | + "red", |
| 73 | + "orange", |
| 74 | + "yellow", |
| 75 | + "green", |
| 76 | + "teal", |
| 77 | + "blue", |
| 78 | + "indigo", |
| 79 | + "purple", |
| 80 | + "pink" |
| 81 | + ]), |
| 82 | + // adds a font icon to the left of the message |
| 83 | + // for example, if you have included into your project font-awesome free |
| 84 | + // you could send "fa fa-heart" |
| 85 | + icon: PropTypes.string, |
| 86 | + children: PropTypes.node.isRequired |
| 87 | +}; |
| 88 | + |
| 89 | +export default Alert; |
0 commit comments