Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Initial current month is incorrect when value from v-model is already set #64

Closed
isometriq opened this issue Sep 19, 2017 · 3 comments
Closed
Assignees
Labels

Comments

@isometriq
Copy link

OS, browser, uiv version

Windows 10
Chrome 60
uiv 0.11.9

Which component

DatePicker

Steps to reproduce the problem

  1. Use datepicker inside a dropdown like in the docs
  2. Set the v-model to a predefined value, ie
//...
<date-picker v-model="date"></date-picker>
//...
data: ()=({
    date: '2017-08-25'
})

Expected behavior

When opening the dropdown, expecting currentMonth to be set to 8 (august).

Actual behavior

currentMonth is set to current date's, which is 9 (september) ..as I type now.

I've checked the source code. It seems that currentMonth and currentYear is set when component is mounted. I see that there is a watch for the value. However, I suspect that the hook will not be called until data changes.

I could use computed to make use of reactive value

computed: {
   dateValue: {
      get() {
        let ts = this.dateParser(val)
        if (!isNaN(ts)) {
          let date = new Date(ts)
          if (date.getHours() !== 0) {
            date = new Date(ts + date.getTimezoneOffset() * 60 * 1000)
          }
          return date;
        }
        return this.now;
      }
      set() {
          if (this.limit && ((this.limit.from && date < this.limit.from) || (this.limit.to && date >= this.limit.to))) 
          {
            this.$emit('input', oldVal || '')
          } else {
            this.currentMonth = this.date.getMonth()
            this.currentYear = this.date.getFullYear()
          }
      }
   }
}
@isometriq
Copy link
Author

...or easier you could set currentMonth in mounted based on this.value

@isometriq
Copy link
Author

tested and I confirm watch hook is only called when data changes, not on init

@wxsms wxsms added the bug label Sep 20, 2017
@wxsms wxsms self-assigned this Sep 20, 2017
@wxsms
Copy link
Member

wxsms commented Sep 21, 2017

Thanks for the bug report. Will be fixed in next release.

@wxsms wxsms closed this as completed Sep 21, 2017
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants