components icon indicating copy to clipboard operation
components copied to clipboard

feat(DatePicker): select range of dates

Open alaawerfelli opened this issue 9 years ago • 148 comments

Hello, it's possible to set date range in date picker (have the ability to choose startDate and endDate and get all dates between startDate and endDate).

alaawerfelli avatar May 24 '17 08:05 alaawerfelli

+1 any news?

Carniatto avatar Feb 15 '18 14:02 Carniatto

@SaturnTeam Apologies for the slow response. Your implementation appears to line up pretty well with what I envisioned for range selection. Would you like to have a quick meeting to discuss what we need to do to move it into the main repo?

mmalerba avatar Feb 15 '18 18:02 mmalerba

+1 @SaturnTeam any news on that?

Carniatto avatar Mar 08 '18 13:03 Carniatto

@Carniatto I'm working with mmalerba on range date picker. It is slow due to different timezones, my work and what mmalerba want.

Temporally you can use my datepicker.

SaturnTeam avatar Mar 08 '18 14:03 SaturnTeam

In the demo stackblitz you cant drag to select and it also auto commits without first showing what was selected. I got a bit surprised. Perhaps a draggable selection would make that easier. Also a double month view would make it more clear IMO.

Great work though,

AllNamesRTaken avatar Mar 23 '18 16:03 AllNamesRTaken

I think I need to announce progress status this feature. It's temporary frozen https://github.com/angular/material2/pull/10489#issuecomment-375034796 But anyone can use my datepicker for a time. I'm trying to maintain code as close to original code as possible

SaturnTeam avatar Mar 24 '18 01:03 SaturnTeam

@mmalerba you're probably busy with ng-conf but I think the solution shouldn't be complicated.

Trying to force the input to show a range with masking and parsing is additional work we should avoid...

We have a working input that knows how to handle a date and sync it to/from a datapicker, so let's use 2!

something like this:

<mat-form-field>
  <input matInput placeholder="Choose a date" [matDatepicker]="resultPicker">
  <mat-datepicker #resultPicker></mat-datepicker>
</mat-form-field>
<mat-form-field>
  <input matInput [matDatepickerRangeEnd]="resultPicker">
  <mat-datepicker-toggle matSuffix [for]="resultPicker"></mat-datepicker-toggle>
</mat-form-field>

Which looks something like this: image

The use can define it's own separator in the middle, remove gap, create gaps whatever.

input[matDatepickerRangeEnd] is a directive similar to input[matDatepicker] but it will register an range end input...

min/max etc will be taken from the first input only

There are some changes to MatDatepicker, especially handling the new range end input element, new selection events for it etc... and some logic to handle selections / value changes.

e.g when a user is in range mode, if he clicks on a date we don't close the popup/dialog if it's the first selection, we will close on the 2nd...

And of course, internal changes in the month view to show range of selection and 2 selected dates.

I'v done this by extending the base classes and adding functionality and it works without much work. Because I use the same input directive the logic for parsing and handling changes is the same, I just save it in a different variable (range end...)

This design might also help with implementing dual datapicker range selection, like usually seen in airline scheduling sites...

I would be more then happy to quickly push this with a PR, this approach solves all the issues you mentioned in #10489 (comment) because it uses the same logic for range and non range pickers.

shlomiassaf avatar Apr 17 '18 23:04 shlomiassaf

@mmalerba This is the implementation using inheritance: StackBlitz source + demo

I'll point the obvious, 95% of the code is due to inheritance boilerplate in angular templates and component metadata in derived components. Some boilerplates is due to extensive use of private modifiers and not protected.

I estimate the entire change is ~75-85 lines of code.

From the above, the majority goes to TS code in the components. SCSS changes are minimal, around ~8 LOC. HTML changes are also minimal, ~10 LOC.

Will happily post a PR for this.

You'r comments in #10489 (comment) are taken care of because it uses the same logic the already exist to handle dates.

The only thing to take care of is a keyboard input of invalid date range (e.g. end date < start date)

shlomiassaf avatar Apr 21 '18 20:04 shlomiassaf

@shlomiassaf I do like the direction that's going, but it's strange that you have two different form-fields that pretend to be one. Something we could do instead would be to wrap then in another component/directive that acts as a MatFormFieldControl. I imagine the DOM would look something like this:

<mat-form-field>
  <mat-date-range [matDatepicker]="picker" [formControl]="range">
    <input matStartDate>
    <input matEndDate>
  </mat-date-range>
  <mat-datepicker #picker><mat-datepicker>
</mat-form-field>

mmalerba avatar May 01 '18 15:05 mmalerba

@mmalerba I agree, my implementation was just to demonstrate the idea and that it's possible.

I'm working with this solution right now, it does require some work to tame 2 MatFormField s...

Your solution sound good but now we need to think of forms integration more carefully.

What do you mean with [formControl]="range", it looks like FormGroup to me, since we have 2 inputs... If we use FormControl it will require us to use transformation functions from 2 date inputs into 1 text and vice versa.

I know that MatFormField is limited to 1 control, so its a problem...

shlomiassaf avatar May 01 '18 16:05 shlomiassaf

I was imagining it being an object:

class DateRange<D> {
  start: D;
  end: D;
}

The mat-date-range would coordinate with the child inputs to keep this in sync.

mmalerba avatar May 01 '18 16:05 mmalerba

Funny, that's exactly what I did just with 2 form fields and sync outside

Sent from my iPhone

On 1 May 2018, at 19:50, mmalerba [email protected] wrote:

I was imagining it being an object:

class DateRange { start: D; end: D; } The mat-date-range would coordinate with the child inputs to keep this in sync.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

shlomiassaf avatar May 01 '18 16:05 shlomiassaf

Btw, today I found that flights.google.com recently got an update which kind of use similar ideas from UI point of view. Just happy, that we are thinking in "Material"-way. :wink:

image

Den-dp avatar May 18 '18 17:05 Den-dp

@brazilia28 See https://github.com/SaturnTeam/saturn-datepicker . your implementation also doesn't support input from keyboard without datepicker

SaturnTeam avatar Jun 07 '18 23:06 SaturnTeam

Wow, so happy to see this. I need both date, date range and day of week. Clicking on a MTWTF should select all the corresponding dates for that day of week. Return of an array of dates (allowing specific deselection) and an array of days of week would sure float my boat :) Thanks

DanPride avatar Jun 14 '18 02:06 DanPride

Google Analytics also has a Material Design Datepicker. We need this please!! bildschirmfoto 2018-06-29 um 14 44 45

SnakeME avatar Jun 29 '18 12:06 SnakeME

What I don't understand if there is a million developers out there supposedly,... why are there less than a dozen of us talking about this fundamental capability ???

DanPride avatar Jun 30 '18 15:06 DanPride

Hi guys, could you please share a possible release date for this feature with us? Thanks 🔢

yuliankarapetkov avatar Jul 19 '18 07:07 yuliankarapetkov

+1 for this feature request. Massive gap in Angular Material's input functionality. Date range picker is pretty standard.

botoxparty avatar Aug 06 '18 03:08 botoxparty

I am just now diving into typescript. I am wondering how much it would take to edit the existing date picker. I have decades of experience Any Idea how big a task this would be for multiple selection?

Dan Pride 1-605-777-0491 1-360-850-9322 (Mobile) http://danielpride.com www.linkedin.com/in/danielpride/ http://archaeolibrary.com/ http://headlessuber.com http://kingsolomonsgate.com http://solomonschariot.com

On Thu, Jul 19, 2018 at 12:59 AM, Yulian Karapetkov < [email protected]> wrote:

Hi guys, could you please share a possible release date for this feature with us? Thanks 🔢

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/angular/material2/issues/4763#issuecomment-406189723, or mute the thread https://github.com/notifications/unsubscribe-auth/ACUUNu6fEgmpL5KNuzuoAtC6QlJ2qOarks5uIDxygaJpZM4Nkx8N .

DanPride avatar Aug 06 '18 19:08 DanPride