{"id":4061,"date":"2022-09-28T15:18:34","date_gmt":"2022-09-28T06:18:34","guid":{"rendered":"https:\/\/blog.wsd.sh\/?p=4061"},"modified":"2022-10-02T07:36:47","modified_gmt":"2022-10-01T22:36:47","slug":"materialui-3-month-calendar-with-wednesdays-as-regular-holidays","status":"publish","type":"post","link":"https:\/\/blog.wsd.sh\/?p=4061","title":{"rendered":"<small> MaterialUI: 3-month calendar with Wednesdays as regular holidays<\/small>"},"content":{"rendered":"<p>1. 3-month calendar with Wednesdays as regular holidays<br \/>\n&#8211; use calendar-picker<br \/>\nreference: <a href=\"https:\/\/mui.com\/x\/api\/date-pickers\/calendar-picker\/\">https:\/\/mui.com\/x\/api\/date-pickers\/calendar-picker\/<\/a><\/p>\n<p>2. Results display<br \/>\n<img decoding=\"async\" loading=\"lazy\" width=\"976\" height=\"296\" src=\"https:\/\/blog.wsd.sh\/wp-content\/uploads\/2022\/09\/20220928-1.png\" alt=\"\" class=\"alignnone size-medium wp-image-4092\" srcset=\"https:\/\/blog.wsd.sh\/wp-content\/uploads\/2022\/09\/20220928-1.png 976w, https:\/\/blog.wsd.sh\/wp-content\/uploads\/2022\/09\/20220928-1-300x91.png 300w, https:\/\/blog.wsd.sh\/wp-content\/uploads\/2022\/09\/20220928-1-768x233.png 768w\" sizes=\"(max-width: 976px) 100vw, 976px\" \/><\/p>\n<p>3. Modules<br \/>\n@mui\/material@5.10.3<br \/>\n@mui\/x-date-pickers@5.0.0-beta.7<br \/>\nreact@18.2.0<\/p>\n<p>4. Contents<br \/>\n(1) Day of the week character change<br \/>\n(2) Change header style<br \/>\n(3) DefaultCalendarMonth<br \/>\n(4) Wednesdays as regular holidays<br \/>\n(5) DisablePast and max Date<br \/>\n(6) All code<br \/>\n(7) Note<\/p>\n<p>5. Day of the week character change<\/p>\n<pre>\r\nconst formatter = (day) => {\r\n  const days = {'Su':'Sun','Mo':'Mon','Tu':'Tue','We':'Wed','Th':'The','Fr':'Fri','Sa':'Sat'};\r\n  return days[day];\r\n}\r\n\r\ndayOfWeekFormatter={(day) => formatter(day)}\r\n<\/pre>\n<p>6. Change style<br \/>\n&#8211; year and month in header are hidden<br \/>\n&#8211; color sunday:red, saturday; blue<\/p>\n<pre>\r\nconst StyledCalendarPicker = styled(CalendarPicker)(({ theme }) => ({\r\n  '& .css-nk89i7-MuiPickersCalendarHeader-root': {\r\n    display: 'none'\r\n  },\r\n  '& .css-qklzlb-MuiDayPicker-header' :{\r\n    backgroundColor: '#ababab',\r\n  },\r\n  '& .css-raiqh1-MuiTypography-root-MuiDayPicker-weekDayLabel': {\r\n    color: 'white',\r\n    fontSize: '13px',\r\n  },\r\n  '& .css-raiqh1-MuiTypography-root-MuiDayPicker-weekDayLabel:first-of-type': {\r\n    color: 'red',\r\n  },\r\n  '& .css-raiqh1-MuiTypography-root-MuiDayPicker-weekDayLabel:last-of-type': {\r\n    color: 'blue',\r\n  },\r\n  '& .css-bkrceb-MuiButtonBase-root-MuiPickersDay-root': {\r\n    width: '36px',\r\n  }\r\n}));\r\n<\/pre>\n<p>7. DefaultCalendarMonth<\/p>\n<pre>\r\n  const date0 = dayjs();\r\n  const date1 = dayjs().add('1', 'M');\r\n  const date2 = dayjs().add('2', 'M');\r\n\r\n  defaultCalendarMonth={date0}\r\n\r\n  defaultCalendarMonth={date1}\r\n\r\n  defaultCalendarMonth={date2}\r\n<\/pre>\n<p>8. Wednesdays as regular holidays<\/p>\n<pre>\r\nconst isWeekend = (date: Dayjs) => {\r\n  const day = date.day();\r\n\r\n  return day == 3;\r\n};\r\n\r\nshouldDisableDate={isWeekend}\r\n<\/pre>\n<p>9. DisablePast and max Date<\/p>\n<pre>\r\ndisablePast={true}\r\n\r\nmaxDate={date2}\r\n<\/pre>\n<p>10. All code<\/p>\n<pre>\r\nimport * as React from 'react';\r\nimport dayjs, { Dayjs } from 'dayjs';\r\nimport TextField from '@mui\/material\/TextField';\r\nimport { AdapterDayjs } from '@mui\/x-date-pickers\/AdapterDayjs';\r\nimport { LocalizationProvider } from '@mui\/x-date-pickers\/LocalizationProvider';\r\nimport { CalendarPicker } from '@mui\/x-date-pickers\/CalendarPicker';\r\nimport { styled } from '@mui\/material\/styles';\r\n\r\n\r\nconst StyledCalendarPicker = styled(CalendarPicker)(({ theme }) => ({\r\n  '& .css-nk89i7-MuiPickersCalendarHeader-root': {\r\n    display: 'none'\r\n  },\r\n  '& .css-qklzlb-MuiDayPicker-header' :{\r\n    backgroundColor: '#ababab',\r\n  },\r\n  '& .css-raiqh1-MuiTypography-root-MuiDayPicker-weekDayLabel': {\r\n    color: 'white',\r\n    fontSize: '13px',\r\n  },\r\n  '& .css-raiqh1-MuiTypography-root-MuiDayPicker-weekDayLabel:first-child': {\r\n    color: 'red',\r\n  },\r\n  '& .css-raiqh1-MuiTypography-root-MuiDayPicker-weekDayLabel:last-child': {\r\n    color: 'blue',\r\n  },\r\n  '& .css-bkrceb-MuiButtonBase-root-MuiPickersDay-root': {\r\n    width: '40px',\r\n  }\r\n}));\r\n\r\nconst isWeekend = (date: Dayjs) => {\r\n  const day = date.day();\r\n\r\n  return day == 3;\r\n};\r\n\r\nconst Y_FORMAT = \"YYYY\"\r\nconst M_FORMAT = \"M\"\r\n\r\nexport default function StaticDatePickerLandscape() {\r\n\r\n  const [value, setValue] = React.useState<Dayjs | null>(null);\r\n\r\n  const handleOnChange = (newValue) => {\r\n    console.log(newValue.$y + \"-\" + (newValue.$M + 1) + \"-\" + newValue.$D);\r\n  }\r\n\r\n  const formatter = (day) => {\r\n      const days = {'Su':'Sun','Mo':'Mon','Tu':'Tue','We':'Wed','Th':'The','Fr':'Fri','Sa':'Sat'};\r\n\r\n      return days[day];\r\n  }\r\n\r\n  const date0 = dayjs();\r\n  const date1 = dayjs().add('1', 'M');\r\n  const date2 = dayjs().add('2', 'M');\r\n\r\n  const Y0 = date0.format(Y_FORMAT);\r\n  const M0 = date0.format(M_FORMAT);\r\n  const Y1 = date1.format(Y_FORMAT);\r\n  const M1 = date1.format(M_FORMAT);\r\n  const Y2 = date2.format(Y_FORMAT);\r\n  const M2 = date2.format(M_FORMAT);\r\n\r\n  const HEADER_PADDING = '0px 80px 0px 110px';\r\n\r\n  return (\r\n    &lt;LocalizationProvider dateAdapter={AdapterDayjs}>\r\n      &lt;table>\r\n        &lt;thead>\r\n          &lt;tr>\r\n            &lt;td style={{ textAlign: 'center' }}>\r\n              &lt;span style={{ padding: HEADER_PADDING }}>\r\n                {M0}\r\n              &lt;\/span>\r\n              &lt;small>{Y0}&lt;\/small>\r\n            &lt;\/td>\r\n            &lt;td style={{ textAlign: 'center' }}>\r\n              &lt;span style={{ padding: HEADER_PADDING }}>\r\n                {M1}\r\n              &lt;span>\r\n              &lt;small>{Y1}&lt;\/small>\r\n            &lt;\/td>\r\n            &lt;td style={{ textAlign: 'center' }}>\r\n              &lt;pan style={{ padding: HEADER_PADDING }}>\r\n                {M2}\r\n              &lt;\/span>\r\n              &lt;small>{Y2}&lt;\/small>\r\n            &lt;\/td>\r\n          &lt;\/tr>\r\n        &lt;\/thead>\r\n        &lt;tbody>\r\n          &lt;tr>\r\n            &lt;td>\r\n              &lt;StyledCalendarPicker\r\n                openTo=\"day\"\r\n                shouldDisableDate={isWeekend}\r\n                onChange={(newValue) => {\r\n                  setValue(newValue);\r\n                  handleOnChange(newValue);\r\n                }}\r\n                disablePast={true}\r\n                dayOfWeekFormatter={(day) => formatter(day)}\r\n              \/>\r\n            &lt;\/td>\r\n            &lt;td>\r\n              &lt;StyledCalendarPicker\r\n                openTo=\"day\"\r\n                defaultCalendarMonth={date1}\r\n                shouldDisableDate={isWeekend}\r\n                onChange={(newValue) => {\r\n                  setValue(newValue);\r\n                  handleOnChange(newValue);\r\n                }}\r\n                disablePast={true}\r\n                disableHighlightToday={true}\r\n                dayOfWeekFormatter={(day) => formatter(day)}\r\n              \/>\r\n            &lt;\/td>\r\n            &lt;td>\r\n              &lt;StyledCalendarPicker\r\n                openTo=\"day\"\r\n                defaultCalendarMonth={date2}\r\n                shouldDisableDate={isWeekend}\r\n                onChange={(newValue) => {\r\n                  setValue(newValue);\r\n                  handleOnChange(newValue);\r\n                }}\r\n                disablePast={true}\r\n                maxDate={date2}\r\n                dayOfWeekFormatter={(day) => formatter(day)}\r\n              \/>\r\n            &lt;\/td>\r\n          &lt;\/tr>\r\n        &lt;\/tbody>\r\n      &lt;\/table>\r\n    &lt;\/LocalizationProvider>\r\n  );\r\n}\r\n<\/source>\r\n<\/pre>\n<p>10. Note<br \/>\n&#8211; Focus remains on calendar<br \/>\n&#8211; Please move focus to another element<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. 3-month calendar with Wednesdays as regular holidays &#8211; use calendar-picker reference: https:\/\/mui.com&#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\/4061"}],"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=4061"}],"version-history":[{"count":42,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts\/4061\/revisions"}],"predecessor-version":[{"id":4065,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts\/4061\/revisions\/4065"}],"wp:attachment":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4061"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4061"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4061"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}