美文网首页
关于antd DatePicker showTime的禁用问题

关于antd DatePicker showTime的禁用问题

作者: 燕自浩 | 来源:发表于2023-07-24 19:05 被阅读0次

前言:今天遇到一个挺有意思的事情,功能不难个人从事前端开发四年之久还是第一次遇到,比较冷门,一般就是简单的禁用DatePicker的日期,今天遇到了不仅要禁用日期还要禁用时分秒,这就比较有意思了,特此记录一下。

  // 禁用日期
  const disabledDate = current => {
    return current && formatDate(current) < formatDate(moment());
  };

  const range = useCallback((start, end) => {
    const result = [];
    for (let i = start; i < end; i++) {
      result.push(i);
    }
    return result;
  }, []);

  // 禁用时分秒
  const disabledDateTime = useCallback((selectCurrentDate) => {
    if (formatDate(selectCurrentDate) > formatDate(moment())) return {};
    const hour = moment().hour();
    const minute = moment().minute();
    const second = moment().second();
    return {
      disabledHours: () => range(0, 24).splice(0, hour),
      disabledMinutes: h => {
        return h > hour ? undefined : range(0, minute);
      },
      disabledSeconds: (h, m) => {
        return ((h > hour) || (h === hour & m > minute)) ? undefined : range(0, second);
      },
    };
  }, [moment]);

  <DatePicker
    style={{ width: '100%' }}
    disabledDate={disabledDate}
    disabledTime={disabledDateTime}
    showTime={{ hideDisabledOptions: true, defaultValue: moment(moment(), timeFormat) }}
  />

由于代码简单不做过多描述,只记录代码

相关文章

网友评论

      本文标题:关于antd DatePicker showTime的禁用问题

      本文链接:https://www.haomeiwen.com/subject/oslcpdtx.html