-
Notifications
You must be signed in to change notification settings - Fork 6.8k
feat(datepicker): @Output for year and month selected in multiyear/year #9678
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the change! I think people will be really happy about this. We should probably add a section to the docs about it, along with embedded examples.
|
||
<h2>Datepicker emulating year picker</h2> | ||
<p> | ||
<input matInput [matDatepicker]="datePicker5" class="datepicker-input-invisible"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This hidden input stuff feels kind of hacky. What if we just use the MomentDateAdapter
and provide a custom MAT_DATE_FORMATS
Our custom formats could just be a copy of the defaults but with the parse and display formats for dateInput
changed to MM/YYYY
or YYYY
respectively.
As for how to fit this into the demo, you could make some directives demo-moment-month-year
, demo-moment-year
that provide the appropriate things for their children
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll work on it.
src/lib/datepicker/calendar.ts
Outdated
@@ -107,6 +107,18 @@ export class MatCalendar<D> implements AfterContentInit, OnDestroy, OnChanges { | |||
/** Emits when the currently selected date changes. */ | |||
@Output() readonly selectedChange = new EventEmitter<D>(); | |||
|
|||
/** | |||
* Emits the year chosen in multiyear view. | |||
* This doesn't implies in change on the selected date. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't imply a change in the selected date.
src/lib/datepicker/calendar.ts
Outdated
|
||
/** | ||
* Emits the month chosen in year view. | ||
* This doesn't implies in change on the selected date. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't imply a change in the selected date.
src/lib/datepicker/datepicker.ts
Outdated
@@ -164,6 +164,18 @@ export class MatDatepicker<D> implements OnDestroy { | |||
*/ | |||
@Output() readonly selectedChanged: EventEmitter<D> = new EventEmitter<D>(); | |||
|
|||
/** | |||
* Emits selected year in multiyear view. | |||
* This doesn't implies in change on the selected date. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same change to wording here
src/lib/datepicker/year-view.ts
Outdated
@@ -65,6 +65,9 @@ export class MatYearView<D> implements AfterContentInit { | |||
/** Emits when a new month is selected. */ | |||
@Output() readonly selectedChange = new EventEmitter<D>(); | |||
|
|||
/** Emits the selected month. This doesn't implies in change on the selected date */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same change to wording
@Output() readonly selectedChange = new EventEmitter<D>(); | ||
|
||
/** Emits the selected year. This doesn't implies in change on the selected date */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same change to wording
Well, it's done. There are two things I'm not sure whether I should touch:
|
|
dateFilter = | ||
(date: Date) => !(date.getFullYear() % 2) && (date.getMonth() % 2) && !(date.getDate() % 2) | ||
|
||
onDateInput = (e: MatDatepickerInputEvent<Date>) => this.lastDateInput = e.value; | ||
onDateChange = (e: MatDatepickerInputEvent<Date>) => this.lastDateChange = e.value; | ||
|
||
dateCtrl = new FormControl(); | ||
|
||
constructor() { } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: can omit constructor since its not doing anything
} | ||
|
||
chosenYearFromYearMonthHandler(year: number) { | ||
try { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
did you find that these try/catch blocks were necessary for some reason? if the date doesn't parse I believe the forms validation should know about it and you can just display a mat-error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's something I put in the code and for no reason I left them there. I've just taken it away.
You can go on and aprove this one. |
I have just rebased it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1 more nit then I'll add merge-ready
dateFilter = | ||
(date: Date) => !(date.getFullYear() % 2) && (date.getMonth() % 2) && !(date.getDate() % 2) | ||
|
||
onDateInput = (e: MatDatepickerInputEvent<Date>) => this.lastDateInput = e.value; | ||
onDateChange = (e: MatDatepickerInputEvent<Date>) => this.lastDateChange = e.value; | ||
|
||
dateCtrl = new FormControl(); | ||
chosenYearHandler(year: number, datepicker: MatDatepicker<Date>) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: these should probably actually be MatDatepicker<Moment>
(or MatDatepicker<any>
since your code doesn't care)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm gonna use Moment
for that just to expose the fact we're using Moment.js
.
src/lib/datepicker/datepicker.md
Outdated
@@ -51,6 +51,23 @@ year containing the `startAt` date. | |||
|
|||
<!-- example(datepicker-start-view) --> | |||
|
|||
#### Watching the views for changes on selected years and months | |||
|
|||
When a year or a month is selected in **multi-year and year views** respecively, the `yearSelected` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...in the multi-year
and year
views...
src/lib/datepicker/datepicker.md
Outdated
#### Watching the views for changes on selected years and months | ||
|
||
When a year or a month is selected in **multi-year and year views** respecively, the `yearSelected` | ||
and `monthSelected` outputs emit the chosen year (full year) and month (0 - 11). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
now that I think about this some more, wouldn't it make sense to emit normalized dates
(e.g. new Date(year, 0, 1)
and new Date(year, month, 1)
)? especially for the month case, the month number alone doesn't give you all the info you need
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, I'm gonna update this and push it tonight.
@@ -0,0 +1,7 @@ | |||
<mat-form-field> | |||
<input matInput [matDatepicker]="dp" placeholder="Choose a year and a month" [formControl]="date"> | |||
<mat-datepicker-toggle matSuffix [for]="dp" (yearSelected)="chosenYearHandler($event)" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These listeners should go on the datepicker, not the toggle
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ops!
I'm pretty happy with how this turned out. It's actually not that difficult to implement month and year selector with these outputs :) |
I've changed the first paragraph of |
src/lib/datepicker/datepicker.md
Outdated
#### Watching the views for changes on selected years and months | ||
|
||
When a year or a month is selected in `multi-year` and `year` views respecively, the `yearSelected` | ||
and `monthSelected` outputs emit the normalized chosen year and normalized month. By "normalized" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...outputs emit a normalized date representing the chosen year or month. By "normalized" we mean that the dates representing a year will have their month set to January and their day set to the 1st. Dates representing months will have their day set to the 1st of the month. For example, if...
src/lib/datepicker/datepicker.md
Outdated
we mean that it will emit an object, not just a number. For example, if `<mat-datepicker>` is | ||
configured to work with javascript native Date objects, the `yearSelected` will emit | ||
`new Date(2017,0,1)` if the user selects 2017 in `multi-year` view. Similarly, `monthSelected` | ||
will emit `new Date(2017,1,0)` if the user selects **Jan** in `year` view and the current date |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Jan --> January
src/lib/datepicker/datepicker.md
Outdated
configured to work with javascript native Date objects, the `yearSelected` will emit | ||
`new Date(2017,0,1)` if the user selects 2017 in `multi-year` view. Similarly, `monthSelected` | ||
will emit `new Date(2017,1,0)` if the user selects **Jan** in `year` view and the current date | ||
value of the connected `<input>` was something like `new Date(2017,MM,dd)` (the month and year |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
month and day
src/lib/datepicker/datepicker.md
Outdated
associated `<input>`. | ||
|
||
The following example uses `yearSelected` and `monthSelected` outputs to emulate a month and year | ||
picker (if you're not familiar with the usage of `MomentDateAdapter` and `formats customization` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
formats customization
---> MAT_DATE_FORMATS
src/lib/datepicker/datepicker.md
Outdated
|
||
The following example uses `yearSelected` and `monthSelected` outputs to emulate a month and year | ||
picker (if you're not familiar with the usage of `MomentDateAdapter` and `formats customization` | ||
you can read more about them below in this document to fully understand the example). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
link the words "read more about them" to the correct header in this document.
tslint.json
Outdated
@@ -81,7 +81,7 @@ | |||
// Disallows importing the whole RxJS library. Submodules can be still imported. | |||
"import-blacklist": [true, "rxjs", "rxjs/operators"], | |||
// Avoids inconsistent linebreak styles in source files. Forces developers to use LF linebreaks. | |||
"linebreak-style": [true, "LF"], | |||
"linebreak-style": [false, "LF"], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
were the changes to this file made intentionally? I don't think we want to change these settings
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Absolutely not. I'm used to switch those to false just to run tslint locally, and this time I forgot to switch them back after tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the changes!
Just fixed a small nit... |
@julianobrasil This is breaking google's build when I try to presubmit it because we don't sync the moment adapter into google3, but we try to verify that the demo app builds. Could you revert the changes to the demo app? (the examples are what's important to users anyways, demo app is just for the sake of our own manual testing) |
I removed the demo changes, but there's still this error in the build process:
edit: I had forgotten one last reference to |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
@Output
s to emit the selected year in multiyear view and the selected month in month view.Closes #9458