{"id":3893,"date":"2022-09-14T19:26:29","date_gmt":"2022-09-14T10:26:29","guid":{"rendered":"https:\/\/blog.wsd.sh\/?p=3893"},"modified":"2022-10-19T14:10:10","modified_gmt":"2022-10-19T05:10:10","slug":"materialui-custom-alert","status":"publish","type":"post","link":"https:\/\/blog.wsd.sh\/?p=3893","title":{"rendered":"<small>How to Use  the Material-UI Alert Component<\/small>"},"content":{"rendered":"<p>1. Display result<br \/>\n<img decoding=\"async\" src=\"https:\/\/blog.wsd.sh\/wp-content\/uploads\/2022\/09\/20220914-1.png\" alt=\"\" class=\"alignnone size-medium wp-image-3896\" \/><\/p>\n<p>2. node modules<br \/>\n@mui\/material@^5.10.3<br \/>\nreact@18.2.0<\/p>\n<p>3. Reference<br \/>\n<a href=\"https:\/\/mui.com\/material-ui\/react-alert\/#basic-alerts\">https:\/\/mui.com\/material-ui\/react-alert\/#basic-alerts<\/a><br \/>\n<a href=\"https:\/\/smartdevpreneur.com\/using-and-styling-the-material-ui-alert-component\/\">https:\/\/smartdevpreneur.com\/using-and-styling-the-material-ui-alert-component\/<\/a><\/p>\n<p>4. Contents<br \/>\n(1) Create CustomAlert<br \/>\n(2) Installation of CustomAlert<br \/>\n(3) Call CustomAlert<br \/>\n(4) Note<\/p>\n<p>5. Create CustomAlert<br \/>\n5.1 Material-UI Alert in a Dialog Component<br \/>\n&#8211; redisplay only when open<br \/>\n&#8211; severity = {props.AlertObject.severity}<br \/>\n&#8211; color = {props.AlertObject.severity} <\/p>\n<p>5.2 CreateAlertObject<br \/>\n&#8211; open<br \/>\n&#8211; title<br \/>\n&#8211; message<br \/>\n&#8211; severity<\/p>\n<p>5.3 Add Properties<br \/>\n&#8211; <b>autoHideDuration<\/b> , default = null;<br \/>\n&#8211; <b>closeButton<\/b> , default = true;<\/p>\n<p>6. all code<br \/>\n&#8211; CustomAlert.tsx<\/p>\n<pre>\r\n$ cat CustomAlert.tsx\r\nimport Dialog from '@mui\/material\/Dialog';\r\nimport Alert from '@mui\/material\/Alert';\r\nimport AlertTitle from '@mui\/material\/AlertTitle';\r\nimport { useState } from 'react';\r\n\r\nconst CreateAlertObject = (props) => {\r\n  return new AlertObject_class(props);\r\n}\r\n\r\nexport { CreateAlertObject }\r\nexport default CustomAlert;\r\n\r\n\/\/---------------------------  function ------------------------------\r\nfunction CustomAlert(props) {\r\n\r\n  const obj = props.AlertObject;\r\n\r\n  if (obj.open == false) {\r\n    return;\r\n  }\r\n\r\n const handleClose = () => {\r\n    obj.setOpen(false);\r\n    return;\r\n  };\r\n\r\n  return (\r\n    &lt;>\r\n      &lt;Dialog open={obj.open} onClick={handleClose}>\r\n        {{obj.getCloseButton() ?\r\n        (&lt;Alert\r\n          sx={{\r\n            width: '{obj.width}',\r\n            margin: '{obj.margin}',\r\n          }}\r\n          severity={obj.severity}\r\n          color={obj.severity}\r\n          onClose={() => {}}\r\n        >\r\n          &lt;AlertTitle>{obj.title}&lt;\/AlertTitle>\r\n          {obj.message}\r\n        &lt;\/Alert>)\r\n        :\r\n        (&lt;Alert\r\n          sx={{\r\n            width: '{obj.width}',\r\n            margin: '{obj.margin}',\r\n          }}\r\n          severity={obj.severity}\r\n          color={obj.severity}\r\n          onClose={() => {}}\r\n        >\r\n          &lt;AlertTitle>{obj.title}&lt;\/AlertTitle>\r\n          {obj.message}\r\n        &lt;\/Alert>)\r\n        }\r\n      &lt;\/Dialog>\r\n    &lt;\/>\r\n  );\r\n}\r\n\r\nfunction AlertObject_class(props) {\r\n\r\n  const [open, setOpen] = useState(false);\r\n\r\n  const [title, setTitle] = useState('');\r\n  const [message, setMessage] = useState('');\r\n  const [severity, setSeverity] = useState('success');\r\n\r\n  const [width, setWidth] = useState('100%');\r\n  const [margin, setMargin] = useState('auto');\r\n\r\n  this.open = open;\r\n  this.setOpen = setOpen;\r\n\r\n  this.title = title;\r\n  this.setTitle = setTitle;\r\n  this.message = message;\r\n  this.setMessage = setMessage;\r\n  this.severity = severity;\r\n  this.setSeverity = setSeverity;\r\n\r\n  this.margin = margin;\r\n  this.setMargin = setMargin; \r\n\r\n  this.display = dispaly.bind(this);\r\n  this.hidden = hidden.bind(this);\r\n\r\n  this.autoHideDuration = null;\r\n  this.setAutoHideDuration = (time) => this.autoHideDuration = time;\r\n  this.initAutoHideDuration = () => this.autoHideDuration = null;\r\n\r\n  this.closeButton = true;\r\n  this.setCloseButton = (bool) => this.closeButton = bool;\r\n  this.getCloseButton = () => this.closeButton ;\r\n\r\n  if(props != null) {\r\n     if(typeof props.autoHideDuration === \"number\" &&\r\n         props.autoHideDuration > 0) {\r\n         this.autoHideDuration = props.autoHideDuration;\r\n     }\r\n\r\n     if(props.closeButton == true || props.closeButton == false ) {\r\n         this.setCloseButton ( props.closeButton);\r\n     }\r\n  }\r\n\r\n  return this;\r\n\r\n  function display(message, title, severity ) {\r\n\r\n    const severitys = [ \"success\" , \"info\" , \"warning\" , \"error\" ];\r\n\r\n    if(arguments > 2) {\r\n      if(!severitys.includes(severity)) {\r\n        console.log(severity +\" is Irregular argument\");\r\n        return;\r\n      }\r\n    }\r\n\r\n    this.setMessage( message );\r\n    if(title) this.setTitle( title );\r\n    if(severity) this.setSeverity( severity );\r\n\r\n    this.setOpen( true );\r\n\r\n    if(this.autoHideDuration != null) {\r\n      setTimeout(function () {\r\n        this.setOpen( false )\r\n      }.bind(this), this.autoHideDuration);\r\n    }\r\n  }\r\n  \r\n  function hidden() {\r\n    if(times) {\r\n      setTimeout(function () {\r\n          this.setOpen( false );\r\n      }.bind(this) , times);\r\n    } else {\r\n      this.setOpen( false );\r\n    }\r\n  }\r\n};\r\n<\/pre>\n<p>7. Installation of CustomAlert<br \/>\n(1) Without <b>autoHideDuration<\/b><\/p>\n<pre>\r\nimport CustomAlert, { CreateAlertObject } from '.\/CustomAlert'\r\n\r\nconst AlertObject = CreateAlertObject();\r\n\r\n&lt;CustomAlert AlertObject={AlertObject} \/>\r\n<\/pre>\n<p>(2)  with <b>autoHideDuration<\/b><\/p>\n<pre>\r\nimport CustomAlert, { CreateAlertObject } from '.\/CustomAlert'\r\n\r\nconst AlertObject = CreateAlertObject({autoHideDuration:6000});\r\n\r\n&lt;CustomAlert AlertObject={AlertObject} \/>\r\n<\/pre>\n<p>(3)  with <b>autoHideDuration<\/b> and <b>closeButton<\/b><\/p>\n<pre>\r\nimport CustomAlert, { CreateAlertObject } from '.\/CustomAlert'\r\n\r\nconst AlertObject = CreateAlertObject({autoHideDuration:6000,\r\n                                       closeButton:false});\r\n\r\n&lt;CustomAlert AlertObject={AlertObject} \/>\r\n<\/pre>\n<p>8. Call CustomAlert<br \/>\n&#8211; message : message<br \/>\n&#8211; title : &#8220;Warning&#8221;<br \/>\n&#8211; severity : &#8220;success&#8221; | &#8220;info&#8221; |  &#8220;warning&#8221; | &#8220;error&#8221;<\/p>\n<table>\n<tbody>\n<tr>\n<th>\n before\n<\/th>\n<th>\n after\n<\/th>\n<\/tr>\n<tr>\n<td>\n<pre>\r\nalert(message)\r\n<\/pre>\n<\/td>\n<td>\n<pre>\r\nAlertObject.display(message, \"Warning\", \"warning\");\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>9. Note<br \/>\nBut need to create AlertObject for each page.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Display result 2. node modules @mui\/material@^5.10.3 react@18.2.0 3. Reference https:\/\/mui.com\/material-ui\/&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts\/3893"}],"collection":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3893"}],"version-history":[{"count":104,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts\/3893\/revisions"}],"predecessor-version":[{"id":3895,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts\/3893\/revisions\/3895"}],"wp:attachment":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}