Skip to content

feat(datepicker): Add Moment.js adapter #6860

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

Merged
merged 15 commits into from
Sep 12, 2017
Merged

Conversation

mmalerba
Copy link
Contributor

@mmalerba mmalerba commented Sep 5, 2017

No description provided.

@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Sep 5, 2017
@mmalerba
Copy link
Contributor Author

mmalerba commented Sep 5, 2017

There's still something in the build configuration that isn't right. We get an error TypeError: moment_1.default is not a function I believe this is a result of the synthetic default import...

this.setLocale(dateLocale || moment.locale());
}

setLocale(locale: any) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the type be more specific than any here since you know you're working with whatever moment supports?


// Temporarily change the global locale to get the data we need, then change it back.
let globalLocale = moment.locale();
moment.locale(locale);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of changing the locale, can't you pass an argument to moment.localeData()?

const momentLocaleData = moment.localeData(locale);
this._localeData = {
  firstDayOfWeek: momentLocaleData.firstDayOfWeek(),
  longMonths: momentLocaleData.months(),
  ...
};

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somehow missed the fact that you can do that

export class MomentDateAdapter extends DateAdapter<moment.Moment> {
// Note: all of the methods that accept a `moment.Moment` input parameter immediately call
// `this.clone` on it. This is to ensure that we're working with a `moment.Moment` that has the
// correct locale setting while avoiding mutating the original object passed to us.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this necessary for things that read and return a primitive, e.g. getYear? How would the instance be mutated there? Might be helpful to include an example of what you're trying to avoid

moment.locale(globalLocale);
}

getYear(date: moment.Moment): number {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make an alias at the beginning of the file for brevity?

type Moment = moment.Moment;


createDate(year: number, month: number, date: number): moment.Moment {
// Check for invalid month and date (except upper bound on date which we have to check after
// creating the Date).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mention what moment would otherwise do if the month is out of range?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moment actually does the right thing, just want to be able to give better errors to the user

// Add some locales for testing. These definitions come from Moment.js's fr.js and ja.js locale
// files. (We don't want to the version of moment that comes with locales because it's a lot of
// extra bytes to include in our tests.)
moment.defineLocale('fr', {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this constant data into a separate file and import it?

@mmalerba
Copy link
Contributor Author

mmalerba commented Sep 7, 2017

Comments addressed, PTAL. Super huge thank you to @devversion for helping with a bunch of build issues

@mmalerba mmalerba force-pushed the date-adapter branch 4 times, most recently from 5d39384 to a0e60b3 Compare September 8, 2017 16:34
Copy link
Member

@jelbourn jelbourn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just one more request for comment

import {DateAdapter, MAT_DATE_LOCALE} from '@angular/material';

// Depending on whether rollup is used, moment needs to be imported differently.
// TODO(mmalerba): See if we can clean this up at some point.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you expand this comment to explain more of what's happening?

Copy link
Member

@devversion devversion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the trouble with the build system. Bazel 🔜

@@ -2,6 +2,8 @@
// IDEs and TSLint. For IDEs it ensures that `experimentalDecorator` warnings are not showing up.
{
"compilerOptions": {
// Needed for Moment.js since it doesn't have a default export.
"allowSyntheticDefaultImports": true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need that anymore right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need it if we want webstorm to not complain about import {default as moment} from 'moment', not sure if we care about that or not...

@christele15
Copy link

Good morning,
I am a newbie to Angular, and I am sorry if that might be a newbie question but I was tasked to use the datepicker and trigger an event if the current date selected is less than 27 weeks, more than 27 weeks or 27 weeks, is this something that might be doable, and is there anything to guide me ?

@devversion devversion added the action: merge The PR is ready for merge by the caretaker label Sep 11, 2017
@mmalerba mmalerba force-pushed the date-adapter branch 2 times, most recently from 8e626c9 to f46106f Compare September 12, 2017 18:50
@mmalerba mmalerba merged commit 9545427 into angular:master Sep 12, 2017
mmalerba added a commit that referenced this pull request Sep 13, 2017
mmalerba added a commit that referenced this pull request Sep 13, 2017
* Revert "feat(datepicker): Add Moment.js adapter (#6860)"

This reverts commit 9545427.

* Revert "fix(menu): multiple close events for a single close (#6961)"

This reverts commit 1cccd4b.
mmalerba added a commit that referenced this pull request Sep 13, 2017
mmalerba added a commit that referenced this pull request Sep 13, 2017
* Revert "Revert "fix(menu): multiple close events for a single close" (#7036)"

This reverts commit dcfe515.

* Revert "feat(datepicker): Add Moment.js adapter (#6860)"

This reverts commit 9545427.

* Revert "fix(menu): multiple close events for a single close (#6961)"

This reverts commit 1cccd4b.

* Revert "fix(menu): nested trigger staying highlighted after click (#6853)"

This reverts commit 04bf3d1.

* Revert "feat(viewport-ruler): add common window resize handler (#6680)"

This reverts commit 881630f.
@another-guy
Copy link

While having a moment.js based date adapter is better than nothing, there are still little questions after reviewing the code.

Was date-fns considered as an immutable alternative? Using it would help reducing the potential defect count, and would get rid of the necessity to manually this.clone(...) the date in every method.

My related post and a comment on the related subject got zero attention, even though I provided a code snippet with ISO8601 date adapter that is the first step to a pull request.

@mmalerba
Copy link
Contributor Author

@another-guy I'd certainly be open to adding a date-fns one as well, but last I checked it didn't support custom parsing formats which is the main thing lacking in the NativeDateAdapter, that's why I prioritized adding the MomentDateAdapter. We also expect users to create their own DateAdapters if they want something a little different than what we offer by default.

@another-guy
Copy link

@mmalerba Good to know. I was sure there is a strong reason to use moment.js over the date-fns. Thanks for the answer!

josephperrott pushed a commit to josephperrott/components that referenced this pull request Sep 15, 2017
* create moment adapter

* add Moment to karma.conf.js

* use MAT_DATE_LOCALE

* tests are now running

* add in tests

* WIP: demo

* add synthetic default imports for moment

* add locale switching to demo

* fix moment import issues with tests

* fix aot

* address comments

* fix closure issue

* add additional explanation of rollup issue

* remove moment adapter demo since it is preventing sync into g3 (will add
back as a docs example in future PR).

* fix bad rebase
josephperrott pushed a commit to josephperrott/components that referenced this pull request Sep 15, 2017
…#7036)

* Revert "feat(datepicker): Add Moment.js adapter (angular#6860)"

This reverts commit 9545427.

* Revert "fix(menu): multiple close events for a single close (angular#6961)"

This reverts commit 1cccd4b.
josephperrott pushed a commit to josephperrott/components that referenced this pull request Sep 15, 2017
…lar#7054)

* Revert "Revert "fix(menu): multiple close events for a single close" (angular#7036)"

This reverts commit dcfe515.

* Revert "feat(datepicker): Add Moment.js adapter (angular#6860)"

This reverts commit 9545427.

* Revert "fix(menu): multiple close events for a single close (angular#6961)"

This reverts commit 1cccd4b.

* Revert "fix(menu): nested trigger staying highlighted after click (angular#6853)"

This reverts commit 04bf3d1.

* Revert "feat(viewport-ruler): add common window resize handler (angular#6680)"

This reverts commit 881630f.
@andlcool
Copy link

Thanks for the moment date adapter. This will come in handy.

Are there examples on how to implement this? Thanks.

@mmalerba mmalerba deleted the date-adapter branch April 3, 2018 15:19
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker cla: yes PR author has agreed to Google's Contributor License Agreement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants