Skip to content

Commit 4ff6945

Browse files
committed
feat: add prop default-value for calendar default date (#94)
1 parent 3bcedf5 commit 4ff6945

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export default {
9595
| placeholder | input placeholder text | `string` ||
9696
| width | input size | `string`\|`number` | 210 |
9797
| append-to-body | append the popup to body | `boolean` | false |
98+
| default-value | default date of the calendar | `Date` | new Date() |
9899
| popupStyle | popup style(override the top, left style) | `object` ||
99100
| not-before | Disable all dates before new Date(not-before) | `string`\|`Date` | ''|
100101
| not-after | Disable all dates after new Date(not-after) | `string`\|`Date`| '' |

README.zh-CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export default {
9595
| placeholder | 输入框placeholder | `string` ||
9696
| width | 设置宽度 | `string`\|`number` | 210 |
9797
| append-to-body | 弹出层元素插入body下面 | `boolean` | false |
98+
| default-value | 日历的默认值 | `Date` | new Date() |
9899
| popupStyle | 弹出层的样式(可以覆盖left,top样式) | `object` ||
99100
| not-before | 禁止选择这个时间之前的时间 | `string`\|`Date` | ''|
100101
| not-after | 禁止选择这个时间之前=后的时间 | `string`\|`Date`| '' |

src/calendar.vue

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ export default {
107107
default: 'YYYY-MM-DD'
108108
},
109109
// below user set
110+
defaultValue: {
111+
validator: function (val) {
112+
return isValidDate(val)
113+
}
114+
},
110115
firstDayOfWeek: {
111116
default: 7,
112117
type: Number,
@@ -143,7 +148,7 @@ export default {
143148
}
144149
},
145150
data () {
146-
const now = new Date()
151+
const now = this.getNow(this.value)
147152
const calendarYear = now.getFullYear()
148153
const calendarMonth = now.getMonth()
149154
const firstYear = Math.floor(calendarYear / 10) * 10
@@ -235,13 +240,17 @@ export default {
235240
this.updateNow(this.value)
236241
}
237242
},
243+
getNow (value) {
244+
return value ? new Date(value) : (
245+
(this.defaultValue && isValidDate(this.defaultValue)) ? new Date(this.defaultValue) : new Date()
246+
)
247+
},
238248
// 根据value更新日历
239249
updateNow (value) {
240-
const now = value ? new Date(value) : new Date()
241-
const oldNow = new Date(this.now)
242-
this.now = now
243-
if (this.visible) {
244-
this.dispatch('DatePicker', 'calendar-change', [now, oldNow])
250+
const oldNow = this.now
251+
this.now = this.getNow(value)
252+
if (this.visible && this.now !== oldNow) {
253+
this.dispatch('DatePicker', 'calendar-change', [new Date(this.now), new Date(oldNow)])
245254
}
246255
},
247256
getCriticalTime (value) {

test/index.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,18 @@ describe('datepicker', () => {
380380
})
381381

382382
describe('calendar-panel', () => {
383+
it('prop: defaultValue', () => {
384+
wrapper = mount(CalendarPanel, {
385+
propsData: {
386+
value: null,
387+
defaultValue: '2018-10-01'
388+
}
389+
})
390+
const vm = wrapper.vm
391+
expect(vm.calendarYear).toBe(2018)
392+
expect(vm.calendarMonth).toBe(9)
393+
})
394+
383395
it('click: prev/next month', () => {
384396
wrapper = mount(CalendarPanel)
385397

0 commit comments

Comments
 (0)