在 MUI StaticDatePicker 日历中突出显示日期
P粉513316221
P粉513316221 2023-08-30 17:38:38
0
1
694
<p>我正在尝试将 MUI StaticDatePicker 实现到我的 React 网站。我想要的是使用蓝色圆圈或徽章突出显示日历中的几天。我尝试了各种方法来实现这一点,但我无法获得日历中突出显示的日期输出。如果有人知道该怎么做,请帮助我。在下面的代码中,我尝试突出显示日历中的highlightDays状态值。</p> <p>我在我的网站中使用了dayjs库处理时间和日期相关的数据。但无论出于什么原因,我看不到我的 renderDay 正在运行。最后,我想要实现的是阻止今天之前的日期,即当前日期之前的日期,并在日历中突出显示即将发生的活动日期。</p>
从“react”导入 { React, useState };
从“dayjs”导入dayjs;
从“@mui/material”导入{Box,TextField};
从“../pages/index.mjs”导入{加载};
从“./index.mjs”导入{EventCard};
从“@mui/x-date-pickers/AdapterDayjs”导入{ AdapterDayjs };
从“@mui/x-date-pickers/LocalizationProvider”导入{ LocalizationProvider };
从“@mui/x-date-pickers/StaticDatePicker”导入{ StaticDatePicker };
从“../hooks/useEvent.mjs”导入{useReadAllEvent};
从“@mui/x-date-pickers”导入{ PickersDay };
从“@mui/material”导入{徽章};

const SessionBooking = ({ 医生 }) =>; {
  const [值,setValue] = useState(“”);

  const [highlightDays, setHighlightDays] = useState([
    “2023年6月1日”,
    “2023年6月2日”,
    “2023年6月4日”,
  ]);

  控制台.log(值);

  const { 数据: eventSet, isLoading } = useReadAllEvent({
    onSuccess: (消息) => {},
    onError: (消息) => {},
    会话:{ id:医生,今天:dayjs().format(“YYYY-MM-DD”) },
  });

  if (isLoading) return <正在加载/>;

  控制台.log(事件集);

  常量事件 = eventSet.map(
    ({ key, countPatient, start, end, maxPatients }) =>; (
      <活动卡
        键={键}
        开始={开始}
        结束={结束}
        患者={countPatient}
        最大值={最大患者}
      >>
    )
  );

  返回 (
    <盒子
      MT={2}
      SX={{
        显示:“网格”,
        网格模板列:{
          xs: "重复(1, 1fr)",
          sm: "重复(1, 1fr)",
          md: "重复(2, 1fr)",
        },
        间隙:1,
      }}
    >
      <盒子>
        <LocalizationProvider dateAdapter={AdapterDayjs}>
          <静态日期选择器
            方向=“纵向”
            openTo="日";
            值={值}
            onChange={(newValue) =>; {
              设置值(新值);
            }}
            renderInput={(params) =>; }
            renderDay={(day, _value, DayComponentProps) =>; {
              const isSelected =
                !DayComponentProps.outsideCurrentMonth &&
                highlightDays.includes(dayjs(day).format(“YYYY-MM-DD”));

              返回 (
                <徽章
                  key={day.toString()}
                  重叠=“圆形”
                  徽章内容={已选择? “-” : 不明确的}颜色=“原色”
                >
                  />
                </徽章>
              );
            }}
          >>
        </本地化提供商>
      </框>

      {事件}
    </框>
  );
};

导出默认SessionBooking;</pre>
<pre class="brush:php;toolbar:false;"></pre></p>            
P粉513316221
P粉513316221

全部回复(1)
P粉818306280

就像 steve 所说的 在新版本中不接受 renderDay 属性。相反必须使用插槽。 StaticDatePicker 的代码

import React, { useState } from "react";
import dayjs from "dayjs";
import { useReadAllEvent } from "../hooks/useEvent.mjs";
import { Box } from "@mui/material";
import { styled } from "@mui/material/styles";
import { Loading } from "../pages/index.mjs";
import { EventCard } from "./index.mjs";
import { AdapterDayjs } from "@mui/x-date-pickers/AdapterDayjs";
import { LocalizationProvider } from "@mui/x-date-pickers/LocalizationProvider";
import { PickersDay } from "@mui/x-date-pickers/PickersDay";
import { StaticDatePicker } from "@mui/x-date-pickers/StaticDatePicker";

const HighlightedDay = styled(PickersDay)(({ theme }) => ({
  "&.Mui-selected": {
    backgroundColor: theme.palette.primary.main,
    color: theme.palette.primary.contrastText,
  },
}));

//higlight the dates in highlightedDays arra
const ServerDay = (props) => {
  const { highlightedDays = [], day, outsideCurrentMonth, ...other } = props;

  const isSelected =
    !props.outsideCurrentMonth &&
    highlightedDays.includes(day.format("YYYY-MM-DD"));

  return (
    <HighlightedDay
      {...other}
      outsideCurrentMonth={outsideCurrentMonth}
      day={day}
      selected={isSelected}
    />
  );
};

const SessionBooking = ({ doctor }) => {
  const [value, setValue] = useState("");
  const [highlightedDays, setHighlitedDays] = useState([
    "2023-07-01",
    "2023-07-09",
    "2023-07-21",
    "2024-07-12",
  ]);


  const today = dayjs();

  return (
    
      <Box>
        <LocalizationProvider dateAdapter={AdapterDayjs}>
          <StaticDatePicker
            defaultValue={today}
            minDate={today}
            maxDate={today.add(1, "month")}
            slots={{
              day: ServerDay,
            }}
            slotProps={{
              day: {
                highlightedDays,
              },
            }}
          />
        </LocalizationProvider>
      </Box>
  );
};

export default SessionBooking;
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板