Skip to content
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

fix(datepicker): support NgbDateAdapter in NgbInputDatepicker #2003

Closed

Conversation

maxokorokov
Copy link
Member

@maxokorokov maxokorokov commented Dec 1, 2017

  1. Support for the NgbDateAdapter in the datepicker with input
  2. Default NgbDateStructAdapter is more forgiving now ('2017-11' transformed into {year: 2017, month: 11, day: 1})so I changed one test case for this as well.

@msosa, could you take a look?

Fixes #2002

@msosa
Copy link
Contributor

msosa commented Dec 1, 2017

LGTM, this is what I had but there seems to be an issue with the current NgbDateNativeAdapter in the demo when using the manualDateChange. Sort of crashes in the formModel, but maybe that's just a problem with the adapter itself?

I added typeof date.getFullYear === 'function' in the ternary and the error doesn't show but change it in the input still seems a little weird. Not sure if there really anything that can be done about that

@maxokorokov
Copy link
Member Author

@msosa I think it's the adapter implementation issue.

I think your example crashes when you set something like a string to the model value → your native adapter implementation will crash. It might happen in the demo if you add both ngbDatepicker and ngbInputDatepicker and share the same model between them (not your case by any chance?).

Actually the NgbDateStructAdapter also not working well, it's just not crashing. I'll open another PR to ignore invalid inputs for it.

@msosa
Copy link
Contributor

msosa commented Dec 5, 2017

Ah yes I am using both ngDatepicker and ngbInputDatepicker together on the same model.

Awesome, thanks!

@maxokorokov maxokorokov added this to the 1.0.0-beta.7 milestone Dec 8, 2017
@battistaar
Copy link

I found a problem with validation, the input field is always invalid using a custom NgbDateAdapter. I think the problem is in the validate method of NgbInputDatepicker.
I temporarely fixed it doing

validate(c: AbstractControl): {[key: string]: any} {   
   // convert value to NgbDateStruct using the adapter
   const value = this._ngbDateAdapter.fromModel(c.value);

    if (value === null || value === undefined) {
      return null;
    }

    if (!this._calendar.isValid(value)) {
      return {'ngbDate': {invalid: c.value}};
    }

I'm quite busy at the moment, I'll do more tests next week.

@maxokorokov
Copy link
Member Author

@battistaar yes, you're right, thanks. Forgot to add conversion here. It's obviously working without it for the default adapter...

Will push and update the demo to add datepicker with input

@maxokorokov maxokorokov force-pushed the dp_input_adapter branch 2 times, most recently from 013f232 to 4383b28 Compare December 8, 2017 18:03
@maxokorokov
Copy link
Member Author

maxokorokov commented Dec 8, 2017

@liviu-c
Copy link

liviu-c commented Dec 15, 2017

I'm trying to make the model as native Date, but the input format to be "dd-mm-yyyy" and not the standard iso.

when I pick a date from datepicker, the model changes back to ngbDateStruct... instead of Date.
Am I doing something wrong?

http://plnkr.co/edit/6xICvZtBCPNraavnhaz2?p=preview

@msosa
Copy link
Contributor

msosa commented Dec 15, 2017

Yeah you're not actually using the adapter, look at how the demo does it

providers: [{provide: NgbDateAdapter, useClass: NgbDateNativeAdapter}]

@liviu-c
Copy link

liviu-c commented Dec 16, 2017

oh crap. I got the syntax wrong :) thx for the comment.
it's now working fine : http://plnkr.co/edit/7jU6TMamLQfqjZUNDyQN?p=preview

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants