如何禁止选择相同的日期以在rangepicker antd中选择同一日期?

发布于 2025-02-11 22:54:32 字数 84 浏览 0 评论 0 原文

我希望用户无法为开始和结束选择同一日期。例如,如果用户选择开始日期为30/06/2022,则他只能选择01/07/2022或更多。在API中找不到任何东西

I want users to be unable to select the same date for start and end. For example if user selects 30/06/2022 for start date then he can select only 01/07/2022 or further. Could not find anything in the API

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

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

发布评论

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

评论(2

挽容 2025-02-18 22:54:36
const App = () => {
  const [startDate, setStartDate] = useState();

  return (
    <RangePicker
      onCalendarChange={(dates) => {
        setStartDate(dates?.[0]);
      }}
      disabledDate={(currentDate) => currentDate.isSame(startDate)}
    />
  );
};

const App = () => {
  const [startDate, setStartDate] = useState();

  return (
    <RangePicker
      onCalendarChange={(dates) => {
        setStartDate(dates?.[0]);
      }}
      disabledDate={(currentDate) => currentDate.isSame(startDate)}
    />
  );
};

codesandbox.io

独自唱情﹋歌 2025-02-18 22:54:36

上面的答案允许在用户从第二个输入(结束日期)开始使用相同的日期,

增强的代码看起来像看起来像这是:

    disabledDate={(current) => ((startDate && current.isSame(startDate, 'day')) || (endDate && current.isSame(endDate, 'day')))}

但是,这是ANTD范围选择器方法的限制,该方法与不显示哪个输入触发(开始或结束日期)有关。因此,例如,我选择了 5月22日至5月25日范围,并决定选择 5月22日至5月30日。在这种情况下,我必须仅在第二个输入中单击,因为第一个输入将封锁 5月22日选择,并且不允许进一步进行。

The answer above allows selecting same date when user starting doing so from the 2nd input ( end date )

The enhanced code can look like this:

    disabledDate={(current) => ((startDate && current.isSame(startDate, 'day')) || (endDate && current.isSame(endDate, 'day')))}

But, here comes the limitation of this method of the antd range picker which is related to not showing which input was triggered ( start or end date ). So for example, I have selected May 22 - May 25 Range and decide to select May 22 - May 30. In this case, I must click only on 2nd input, because 1st input is gonna block May 22 from selection and won't allow to proceed further.

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