禁用一周中的日子,用react-datepicker

发布于 2025-01-31 01:29:32 字数 655 浏览 5 评论 0原文

我只想禁用我选择的一周中的日子,或者以其他方式启用我

在这里需要的日子是说明性图像

“这是一个说明性映像”

我在文档中找到了此代码,但它不适用于我

    () => {
  const [startDate, setStartDate] = useState(null);
  const isWeekday = (date) => {
    const day = getDay(date);
    return day !== 0 && day !== 6;
  };
  return (
    <DatePicker
      selected={startDate}
      onChange={(date) => setStartDate(date)}
      filterDate={isWeekday}
      placeholderText="Select a weekday"
    />
  );
};

I only want to disable the days of the week that I choose, or otherwise enable the days that I need

here is an illustrative image

here is an illustrative image

I found this code in the documentation but it doesn't work for me

    () => {
  const [startDate, setStartDate] = useState(null);
  const isWeekday = (date) => {
    const day = getDay(date);
    return day !== 0 && day !== 6;
  };
  return (
    <DatePicker
      selected={startDate}
      onChange={(date) => setStartDate(date)}
      filterDate={isWeekday}
      placeholderText="Select a weekday"
    />
  );
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阳光下慵懒的猫 2025-02-07 01:29:32

您可以将一周中的一天更改为此示例来自行的文档返回日!== 0&amp;&amp; Day!== 6;通过更改最后一个数字。

例如,将Day!== 6更改为day!== 7将仅排除周日,等等

。 >&amp;&amp; Day!== [Day Number] 回到回调。例如day!== 0&amp;&amp; Day!== 2&amp;&amp; Day!== 5;将排除周日,星期二&amp;星期五。

You can change the day of the week to disable in this example from the docs on the line return day !== 0 && day !== 6; by changing the last number.

For instance, changing day !== 6 to day !== 7 will exclude only Sundays, etc.

If you want to exclude multiple days of the week, add && day !== [day number] to the callback. For instance day !== 0 && day !== 2 && day !== 5; will exclude Sundays, Tuesdays & Fridays.

柠檬色的秋千 2025-02-07 01:29:32

我有很简单的解决方案:

        import { addDays, getDay, subDays } from "date-fns";
        import React from "react";
        import DatePicker from "react-datepicker";


        type DaysOfTheWeek = "Sunday" | "Monday" | "Tuesday" | "Wednesday" | "Thursday" | "Friday" | "Saturday";

        interface Props {
          value: Date;
          error?: boolean;
          isDisabled?: boolean;
          selected?: string;
          onSelect: (value: Date) => void;
          onChange: (value: Date) => void;
          startDate?: Date;
          endDate?: Date;
          className?: string;
          isClearable?: boolean;
          exclude?: DaysOfTheWeek[];
        }

        const DAYS_OF_THE_WEEK: DaysOfTheWeek[] = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

        const DateSelect = (props: Props) => {
          const { onChange, value, onSelect, isDisabled, startDate, endDate, className, isClearable = true, exclude } = props;

          const handleExcludedDates = React.useCallback(
            (date: Date) => {
              if (!exclude?.length) return true;
              const day = getDay(date);
              return !exclude?.includes(DAYS_OF_THE_WEEK[day]);
            },
            [exclude]
          );
          return (
            <DatePicker
              className={className}
              selected={value}
              onChange={onChange}
              onSelect={onSelect}
              disabled={isDisabled}
              isClearable={isClearable}
              minDate={startDate}
              startDate={addDays(startDate ?? new Date(), 1)}
              endDate={endDate}
              filterDate={handleExcludedDates}
              excludeDates={[new Date(), subDays(new Date(), 1)]}
            />
          );
        };

        export default DateSelect;


现在您可以轻松地通过一周的日子


    <DateSelect exclude={"Monday","Friday"} {...demOtherPropsDem} />

I have solution that is quite simple:

        import { addDays, getDay, subDays } from "date-fns";
        import React from "react";
        import DatePicker from "react-datepicker";


        type DaysOfTheWeek = "Sunday" | "Monday" | "Tuesday" | "Wednesday" | "Thursday" | "Friday" | "Saturday";

        interface Props {
          value: Date;
          error?: boolean;
          isDisabled?: boolean;
          selected?: string;
          onSelect: (value: Date) => void;
          onChange: (value: Date) => void;
          startDate?: Date;
          endDate?: Date;
          className?: string;
          isClearable?: boolean;
          exclude?: DaysOfTheWeek[];
        }

        const DAYS_OF_THE_WEEK: DaysOfTheWeek[] = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

        const DateSelect = (props: Props) => {
          const { onChange, value, onSelect, isDisabled, startDate, endDate, className, isClearable = true, exclude } = props;

          const handleExcludedDates = React.useCallback(
            (date: Date) => {
              if (!exclude?.length) return true;
              const day = getDay(date);
              return !exclude?.includes(DAYS_OF_THE_WEEK[day]);
            },
            [exclude]
          );
          return (
            <DatePicker
              className={className}
              selected={value}
              onChange={onChange}
              onSelect={onSelect}
              disabled={isDisabled}
              isClearable={isClearable}
              minDate={startDate}
              startDate={addDays(startDate ?? new Date(), 1)}
              endDate={endDate}
              filterDate={handleExcludedDates}
              excludeDates={[new Date(), subDays(new Date(), 1)]}
            />
          );
        };

        export default DateSelect;


Now you can easily pass an array of the days of the week


    <DateSelect exclude={"Monday","Friday"} {...demOtherPropsDem} />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文