Skip to content

Can't select single-day range on multirange selection #2340

Closed
@enzo-santos

Description

@enzo-santos

Use case

The current behavior of the date picker does not allow users to select a date range that spans only a single day. When a user clicks on May 5th to set the start date, and then clicks again on May 5th to also set it as the end date, followed by a click on May 7th, the picker resets the selection. Instead of interpreting the first two clicks as defining a one-day range (May 5th–May 5th), it treats the second click on May 5th as a new start date, and then the third click on May 7th as the end date. This results in an unintended selection of May 5th–May 7th. The expected behavior is for the second click on the same date to confirm a single-day range, rather than resetting the selection process.

Proposal

I understand that in some scenarios disallowing single-day ranges is acceptable, but other scenarios (such as mine) are essential having it as a feature.

I detected where that happens (at least in multirange selection): method void _drawRangesSelectionForMonth(dynamic) of class _PickerViewState. My working solution is:

void _drawRangesSelectionForMonth(dynamic selectedDate) {
    _pickerStateDetails.selectedRanges ??= <dynamic>[];
    int count = _pickerStateDetails.selectedRanges!.length;
    dynamic lastRange;
    if (count > 0) {
      lastRange = _pickerStateDetails.selectedRanges![count - 1];
    }
+   final bool allowSingleDaySelection = true;
    if (lastRange != null &&
        lastRange.startDate != null &&
        (lastRange.endDate == null ||
-           isSameDate(lastRange.startDate, lastRange.endDate))) {
+           (!allowSingleDaySelection && isSameDate(lastRange.startDate, lastRange.endDate)))) {
      dynamic startDate = lastRange.startDate;
      dynamic endDate = selectedDate;
      if (startDate.isAfter(endDate) == true) {
        final dynamic temp = startDate;
        startDate = endDate;
        endDate = temp;
      }

      final dynamic newRange = widget.picker.isHijri
          ? HijriDateRange(startDate, endDate)
          : PickerDateRange(startDate, endDate);
      _pickerStateDetails.selectedRanges![count - 1] = newRange;
    } else {
      _pickerStateDetails.selectedRanges!.add(widget.picker.isHijri
          ? HijriDateRange(selectedDate, null)
          : PickerDateRange(selectedDate, null));
    }

    count = _pickerStateDetails.selectedRanges!.length;
    _removeInterceptRanges(
        _pickerStateDetails.selectedRanges,
        _pickerStateDetails
            .selectedRanges![_pickerStateDetails.selectedRanges!.length - 1]);
    lastRange = _pickerStateDetails
        .selectedRanges![_pickerStateDetails.selectedRanges!.length - 1];
    if (count != _pickerStateDetails.selectedRanges!.length &&
        (lastRange.endDate == null ||
            isSameDate(lastRange.endDate, lastRange.startDate))) {
      _pickerStateDetails.selectedRanges!.removeLast();
    }
  }

My proposal is to add allowSingleDaySelection as a parameter of SfDateRangePicker, to make it easier to turn it on and off.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions