简体   繁体   English

单击 react-date-range 定义的范围使用回调

[英]Using a callback on click of react-date-range defined ranges

在此处输入图像描述

How can we add a callback when clicked the encircled ranges?单击包围的范围时,我们如何添加回调? I want to change the input placeholder based on the range picked.我想根据选择的范围更改输入占位符。 For example the user clicked "This week" so the placeholder also on the right should be changed to "This week".例如,用户单击“本周”,因此右侧的占位符也应更改为“本周”。 I've red the documentation but I cant find thing I need.我已经将文档标记为红色,但找不到我需要的东西。 Thank you so much for your help in advance.非常感谢您的提前帮助。 Here's a snippet of my code.这是我的代码片段。

  const [dateState, setDateState] = useState<any>([
    { startDate: null, endDate: null, key: "selection" },
  ]);

      {openDateRange && (
        <DateRangePicker
          className="date-range"
          editableDateInputs={true}
          onChange={(item) => setDateState([item.selection])}
          moveRangeOnFirstSelection={false}
          ranges={dateState}
        />
      )}

I had the same issue, here is my workaround ;我有同样的问题,这是我的解决方法;

while selecting ranges it updates both selection values in the very first click.在选择范围时,它会在第一次单击时更新两个选择值。

onDateRangeChanged= () => {
.....
if(ranges.selection.startDate !== ranges.selection.endDate){
      const result = calculateDateRangeFromTwoDates(ranges);
      if(result.specificTimeSpan === true)
          toggleRangePicker();
    }
.....
}

and with the calculator below u can extract whether your dates is defined range or not (of course code below can be refactored like reading from a Json file etc.)并且使用下面的计算器,您可以提取您的日期是否定义范围(当然下面的代码可以重构,例如从 Json 文件中读取等)

export const calculateDateRangeFromTwoDates = (ranges: any) => {
  const beginDate = moment(ranges.selection.startDate);
  const endDate = moment(ranges.selection.endDate);
  const today = moment();
  let text = '';
  let specificTimeSpan = true;
  const duration = moment.duration(endDate.diff(beginDate));
  const selectedDateDifference = duration.get('days');

  switch (selectedDateDifference) {
    case 30:
    case 29:
      text = moment(endDate).isSame(today, 'month') ? 'This Month' : 'Last Month';
      break;
    case 7:
    case 6:
      text = moment(endDate).isSame(today, 'week') ? 'This Week' : 'Last Week';
      break;
    case 1:
    case 0:
      text = moment(endDate).isSame(today, 'day') ? 'Today' : 'Yesterday';
      break;
    default:
      text = `
      ${moment(ranges.selection.startDate).format('MM/DD/YYYY')} - ${moment(ranges.selection.endDate).format(
        'MM/DD/YYYY',
      )}
      `;
      specificTimeSpan = false;
      break;
  }

  return ({
    text,
    specificTimeSpan
  })
};
export const getDateText = (dateRange) => {
        let text = "";
        if (dateRange && dateRange.selection.startDate && dateRange.selection.endDate) {
            const beginDate = moment(dateRange.selection.startDate);
            const endDate = moment(dateRange.selection.endDate);
            const today = moment();
            const yesterday = moment().subtract(1, "day");
            const startOfThisWeek = moment().startOf("week");
            const startOfPreviousWeek = moment().subtract(1, "week").startOf("week");
            const endOfPreviousWeek = moment().subtract(1, "week").endOf("week");
            const startOfThisMonth = moment().startOf("month");
            const startOfPreviousMonth = moment().subtract(1, "month").startOf("month");
            const endOfPreviousMonth = moment().subtract(1, "month").endOf("month");
            if (beginDate.format("DD-MM-YYYY") === today.format("DD-MM-YYYY") &&
                endDate.format("DD-MM-YYYY") === today.format("DD-MM-YYYY")) {
                text = "Today";
            } else if (beginDate.format("DD-MM-YYYY") === yesterday.format("DD-MM-YYYY") &&
                endDate.format("DD-MM-YYYY") === yesterday.format("DD-MM-YYYY")) {
                text = "Yesterday";
            } else if (beginDate.format("DD-MM-YYYY") === startOfThisWeek.format("DD-MM-YYYY") &&
                endDate.format("DD-MM-YYYY") === today.format("DD-MM-YYYY")) {
                text = "This Week";
            } else if (beginDate.format("DD-MM-YYYY") === startOfPreviousWeek.format("DD-MM-YYYY") &&
                endDate.format("DD-MM-YYYY") === endOfPreviousWeek.format("DD-MM-YYYY")) {
                text = "Last Week";
            } else if (beginDate.format("DD-MM-YYYY") === startOfThisMonth.format("DD-MM-YYYY") &&
                endDate.format("DD-MM-YYYY") === today.format("DD-MM-YYYY")) {
                text = "This Month";
            } else if (beginDate.format("DD-MM-YYYY") === startOfPreviousMonth.format("DD-MM-YYYY") &&
                endDate.format("DD-MM-YYYY") === endOfPreviousMonth.format("DD-MM-YYYY")) {
                text = "Last Month";
            } else {
                text = `${beginDate.format("DD-MM-YYYY")} / ${endDate.format("DD-MM-YYYY")}`;
            }
        }
        return text;
    };

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

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