简体   繁体   English

如果使用react在prevstartdate之前设置startdate,如何将enddate设置为null?

[英]How to set enddate to null if startdate is set before prevstartdate using react?

i want to allow user to select the start and end dates on the calendar.我想允许用户在日历上选择开始和结束日期。 i am using react-dates for this.我为此使用了反应日期。

i want it to work like below我希望它像下面一样工作

initially start and enddates will be null.最初开始和结束日期将为空。 on first click when user selects start date say 10th oct then enddate is also set to startdate (10th oct) and dates before start date 10th oct and dates after 6 weeks from the start date are disabled for seletion now when user selects enddate say 13th oct end date is set to 13th oct and the dates that were disabled will be enabled after selecting end date.在用户选择开始日期时的第一次点击,比如 10th oct 然后 enddate 也设置为 startdate(10th oct)并且开始日期之前的日期 10th oct 和从开始日期起 6 周之后的日期现在被禁用选择,当用户选择 enddate 时,比如 13th oct结束日期设置为 13 日 oct,选择结束日期后将启用禁用的日期。

now when user clicks 15th oct this is set as start date n end date and dates before start date 15th oct and dates after 6 weeks from the start date are disabled for seletion when user clicks 17th oct as end date that is set as end date and the dates that were disabled will be enabled after selecting end date.现在,当用户点击 15th oct 时,这被设置为开始日期 n 结束日期和开始日期之前的日期 15th oct 和从开始日期起 6 周之后的日期被禁用以供选择,当用户点击 17th oct 作为结束日期设置为结束日期和选择结束日期后将启用禁用的日期。 the above mentioned works.上面提到的作品。 but user does the below it doesnt work as expected and i need a fix for this.但用户执行以下操作,它无法按预期工作,我需要对此进行修复。 user has start date set to 15th oct and end date set to 17th oct now selects startdate 13th october.用户将开始日期设置为 10 月 15 日,结束日期设置为 10 月 17 日,现在选择开始日期为 10 月 13 日。

expected: at this point i expect it to set the start and end dates to 13th oct and disable dates before 13th oct and the dates after 6 weeks from 13th oct are also disabled and wait for user to input enddate.预期:此时我希望它将开始和结束日期设置为 10 月 13 日并禁用 10 月 13 日之前的日期,并且从 10 月 13 日起 6 周后的日期也被禁用并等待用户输入结束日期。

result but it sets 13th oct as start date and 17th oct as enddate.结果,但它将 10 月 13 日设置为开始日期,将 10 月 17 日设置为结束日期。

basically when user has set some start and end dates and selects a date before startdate, the enddate should be set to new startdate and the dates before new startdate and dates after 6 weeks from new startdate should be disabled.基本上,当用户设置了一些开始和结束日期并选择了开始日期之前的日期时,结束日期应设置为新开始日期,新开始日期之前的日期和新开始日期后 6 周后的日期应禁用。 and user clicking on another date after startdate should be set as end date.并且用户在 startdate 之后单击另一个日期应设置为结束日期。

below is the working example of my code下面是我的代码的工作示例

https://codesandbox.io/s/react-dates-forked-skrj2?file=/src/index.js https://codesandbox.io/s/react-dates-forked-skrj2?file=/src/index.js

could someone help me with this.有人可以帮我解决这个问题吗? thanks.谢谢。

If I'm understanding what you want correctly, you basically have two conditions:如果我正确理解你想要什么,你基本上有两个条件:

  1. Both startDate and endDate have been set, and are equal. startDateendDate都已设置,并且是相等的。 In this case, you know you really only have a startDate selected, and should impose a limit on available ranges.在这种情况下,您知道您实际上只选择了一个startDate ,并且应该对可用范围施加限制。 When a valid date is selected, only set the endDate当选择一个有效的日期,只设置endDate
  2. All other cases.所有其他情况。 Here, you should set the startDate and endDate to the same value, and impose no limit.在这里,您应该将startDateendDate设置为相同的值,并且不施加任何限制。 This covers both the initial condition (both are null ) and the case where you have explicitly set a separate endDate .这涵盖了初始条件(均为null )和您明确设置单独的endDate

Here is an example of how you could implement this: https://codesandbox.io/s/react-dates-forked-9rj7q?file=/src/index.js以下是如何实现这一点的示例: https : //codesandbox.io/s/react-dates-forked-9rj7q?file=/src/index.js

I think you can do simply by using useEffect .我认为您可以简单地使用useEffectuseEffect You need two things for that:为此,您需要做两件事:

  1. First track start date value if value has changed from start one then it means you selected again start date for that you can create custom previous hook:第一个跟踪开始日期值,如果值从开始更改,则意味着您再次选择了开始日期,您可以创建自定义上一个挂钩:
  // The ref object is a generic container whose current property is mutable ...
  // ... and can hold any value, similar to an instance property on a class
  const ref = useRef();
  
  // Store current value in ref
  useEffect(() => {
    ref.current = value;
  }, [value]); // Only re-run if value changes
  
  // Return previous value (happens before update in useEffect above)
  return ref.current;
}

  1. Now compare if prevDate is not equal to new start date it means value has changed.现在比较是否 prevDate 不等于新的开始日期,这意味着值已更改。 Update end date.更新结束日期。 For this you need React.useEffect为此,您需要React.useEffect
const prevDate = usePrevious(startDate);

  React.useEffect(() => {
    if (prevDate && !moment(prevDate).isSame(startDate)) {
      setEndDate(startDate);
    }
  }, [startDate, endDate, prevDate]);

You can find demo here: https://codesandbox.io/s/react-dates-forked-xz0pk?file=/src/index.js:2469-2668你可以在这里找到演示: https : //codesandbox.io/s/react-dates-forked-xz0pk?file=/ src/index.js: 2469-2668

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM