Enterprise-ng: DatePicker: getValue returns string instead of Date object

Created on 29 Mar 2021  路  7Comments  路  Source: infor-design/enterprise-ng

Describe the bug
Calling getValue from SohoDatePickerComponent returns the selected date as string. The method is defined as public getValue(): Date and should return a value with type Date.

To Reproduce
Steps to reproduce the behavior:

  1. Change the source code in enterprise-ng/src/app/datepicker/datepicker.demo.ts and add a new console output in onChange method: console.log('DatePickerDemoComponent.onChange(date of datepicker): ' + this.datepicker?.getValue());.
  2. Start the application with npm start.
  3. Open the accordion with letter D on the left side and choose Date Picker.
  4. Select a different value in Standard Date field to trigger a change event.
  5. Open developer console window in your browser.
  6. See the log message like DatePickerDemoComponent.onChange(date of datepicker): 12/13/2016.

Expected behavior
In general I expect a Date object after calling getValue instead of string. The steps "To Reproduce" should end with the following log message: DatePickerDemoComponent.onChange(date of datepicker): [object Object]. And the type of object should be Date to call according methods on it.

Version

  • ids-enterprise-ng: 9.1.5

Screenshots
no screenshots

Platform

  • Device (if applicable): Mac mini
  • OS Version: macOS Big Sur 11.2.3
  • Browser Name: safari
  • Browser Version: Version 14.0.3 (16610.4.3.1.7)

Additional context
no additional context

[3] type

All 7 comments

The only thing we could change is the type. It should actually be saying its a string here https://github.com/infor-design/enterprise-ng/blob/master/projects/ids-enterprise-ng/src/lib/datepicker/soho-datepicker.component.ts#L389

The component underneath returns a string here because you may want the formatted value and thats whats shown in the field.

You can use the Locale functions to convert it to a date. For example

Soho.Locale.parseDate('2020-02-26 涓嬪崍12:00', { pattern: 'yyyy/M/d ah:mm' })
// or with no params for the current locales format
Soho.Locale.parseDate(component.getValue());

Okay I understand. Changing the type of getValue would be very helpful for the right expectation of the returned value. Maybe it's possible to add another method which returns the value from https://github.com/infor-design/enterprise/blob/c5d013bd6c7386ff63046fc51478dd2c076cdbf4/src/components/datepicker/datepicker.js#L1954? So everybody is able to use the standard without doing own parsing stuff.

Ok, yeah i think maybe a getDateValue() could be an idea. So

  • one new method (or option to value) in the enterprise-component
  • add the new method type in NG
  • change the current type to string

To avoid a breaking change the option to value seems better for me. The current method can be changed into

   public getValue(asDate: boolean = false): string | Date {
      if (asDate) {
         return  this.jQueryElement.getCurrentDate();
      }
      return this.internalValue;
   }

or something like that. So we have the same behavior if we call getValue() like today and the additional Date value with getValue(true).

OK, As this is open source you could try a pull request. This seems good to me. You could also even do it all in the NG project as in...

public getValue(asDate: boolean = false): string | Date { if (asDate) { return Soho.Locale.parseDate(this.internalValue); } return this.internalValue; }

Since we are reworking the datepicker API and i'd rather do this sort of change to enterprise then.

I will try a pull request with pleasure @tmcconechy! Your notice about reworking the datepicker API is new to me so I try to solve the parsing in NG project. My idea before was to use existing functionality in enterprise but I skip this.

yeah please do! the new stuff will be a while. so if you want a fix soon that would be the best way

Was this page helpful?
0 / 5 - 0 ratings