【问题标题】:How to disable dates before today React?如何禁用今天之前的日期 React?
【发布时间】:2020-05-26 09:38:19
【问题描述】:

我正在尝试禁用今天之前的日期,因此无法选择它。 这是代码: https://codesandbox.io/s/simple-react-calendar-r1h3b?file=/src/calendar.tsx

如何实现?

【问题讨论】:

  • Day.onClick 之前检查date setDate

标签: javascript reactjs react-hooks


【解决方案1】:

我在这里禁用了过去的日期:https://codesandbox.io/s/simple-react-calendar-1n9zk?file=/src/calendar.tsx:2706-2718

const Day = styled.div`
// ...

  ${props =>
      props.isPast &&
      css`
        color: #eee;
        pointer-events: none;
      `}

// ...
`
export function Calendar() {
// ...
             <Day
                key={index}
                isToday={d === today.getDate()}
                isPast={new Date(year, month, d) < today}
                isSelected={d === day}
                onClick={() => setDate(new Date(year, month, d))}
              >
                {d > 0 ? d : ''}
              </Day>
// ...
}

【讨论】:

  • 感谢您的回答。这个解决方案的问题是,当您下个月搬家时,下个月的日期仍然被禁用。
  • 你可以检查六月例如
  • isPast={new Date(year, month, d) &lt; today}
  • 此条件运行良好。我已经编辑了沙盒以及我的答案。
猜你喜欢
  • 2013-07-26
  • 1970-01-01
  • 2011-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-22
  • 1970-01-01
  • 2021-07-02
相关资源
最近更新 更多