Skip to content

Commit 98e5de6

Browse files
committed
fix(datepicker): fix keydown event for datepicker
add monthrange and fix keydown for datepicker type date, month
1 parent f009df8 commit 98e5de6

5 files changed

Lines changed: 87 additions & 21 deletions

File tree

src/components/calendar/index.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@
164164
}
165165

166166
.c-calendar__body .c-calendar__month-table td {
167-
padding: 1.2em 1.6em;
167+
padding: 1em;
168168
}
169169

170170
.c-calendar__body table thead th,

src/components/calendar/index.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ export default {
144144
if (new Date(date) > new Date(this.maxDate)) date = this.maxDate
145145
if (new Date(date) < new Date(this.minDate)) date = this.minDate
146146
this.$emit('update', new Date(date).format(this.format), true)
147+
},
148+
updateMonthBykeydown (num, type) {
149+
const [year, month] = this.updateMonth(this.year, this.month, num, type)
150+
this.$emit('update', new Date(year, month).format(this.format), true)
147151
}
148152
}
149153
}

src/components/datepicker/daterange.vue

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
:maxDate="startMaxDate"
1010
:year="startYear"
1111
:month="startMonth"
12-
:monthshow="startMonthsShow"
12+
:monthsShow="startMonthsShow"
1313
@monthchange="startMonthChange"
1414
@yearchange="startYearChange"
1515
@monthshow="startMonthTableShow"
@@ -20,6 +20,7 @@
2020
:minDate="minDate"
2121
:maxDate="startMaxDate"
2222
:year="startYear"
23+
:month="startMonth"
2324
@change="startSelectMonth"
2425
)
2526
c-datetable(
@@ -44,7 +45,7 @@
4445
:maxDate="maxDate"
4546
:year="endYear"
4647
:month="endMonth"
47-
:monthshow="endMonthsShow"
48+
:monthsShow="endMonthsShow"
4849
@monthchange="endMonthChange"
4950
@yearchange="endYearChange"
5051
@monthshow="endMonthTableShow"
@@ -55,6 +56,7 @@
5556
:minDate="endMinDate"
5657
:maxDate="maxDate"
5758
:year="endYear"
59+
:month="endMonth"
5860
@change="endSelectMonth"
5961
)
6062
c-datetable(
@@ -98,6 +100,10 @@ export default {
98100
value: [Array, String],
99101
size: String,
100102
show: Boolean,
103+
type: {
104+
type: String,
105+
default: 'date'
106+
},
101107
minDate: {
102108
type: String,
103109
default: '1970-01-01'
@@ -107,8 +113,7 @@ export default {
107113
default: '2099-12-31'
108114
},
109115
pattern: {
110-
type: String,
111-
default: 'yyyy-MM-dd'
116+
type: String
112117
}
113118
},
114119
mixins: [Mixin],
@@ -127,14 +132,18 @@ export default {
127132
rangeObj: {
128133
endDate: '',
129134
selecting: true
130-
}
135+
},
136+
format: ''
131137
}
132138
},
133139
created () {
134140
const [start, end] = this.value
135141
this.start = start || ''
136142
this.end = end || ''
143+
this.startMonthsShow = this.isMonthRange
144+
this.endMonthsShow = this.isMonthRange
137145
this.updateDate()
146+
this.format = this.pattern ? this.pattern : this.isMonthRange ? 'yyyy-MM' : 'yyyy-MM-dd'
138147
},
139148
watch: {
140149
show (newVal) {
@@ -145,14 +154,17 @@ export default {
145154
}
146155
},
147156
computed: {
157+
isMonthRange () {
158+
return this.type === 'month'
159+
},
148160
className () {
149161
return this.size ? `is-${this.size}` : 'md'
150162
},
151163
startMaxDate () {
152-
return new Date(this.endYear, this.endMonth, 0).format(this.pattern)
164+
return new Date(this.endYear, this.endMonth, 0).format(this.format)
153165
},
154166
endMinDate () {
155-
return new Date(this.startYear, this.startMonth + 1, 1).format(this.pattern)
167+
return new Date(this.startYear, this.startMonth + 1, 1).format(this.format)
156168
}
157169
},
158170
methods: {
@@ -218,9 +230,13 @@ export default {
218230
this.startMonthsShow = show
219231
},
220232
startSelectMonth (month) {
221-
this.startMonthsShow = false
233+
this.startMonthsShow = this.isMonthRange
222234
this.startMonth = month
223235
this.startDay = ''
236+
if (this.isMonthRange) {
237+
this.start = new Date(this.startYear, this.startMonth).format(this.format)
238+
this.updateDate()
239+
}
224240
},
225241
selectDay (dateObj) {
226242
this.start = dateObj.start
@@ -237,9 +253,13 @@ export default {
237253
this.endMonthsShow = show
238254
},
239255
endSelectMonth (month) {
240-
this.endMonthsShow = false
256+
this.endMonthsShow = this.isMonthRange
241257
this.endMonth = month
242258
this.endDay = ''
259+
if (this.isMonthRange) {
260+
this.end = new Date(this.endYear, this.endMonth).format(this.format)
261+
this.updateDate()
262+
}
243263
},
244264
cancel () {
245265
[this.start, this.end] = this.value

src/components/datepicker/index.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,35 @@ layout: component
144144

145145
```
146146

147+
## 设置月份的范围
148+
149+
```html
150+
<template>
151+
<c-datepicker
152+
v-model="monthrange"
153+
:type="'monthrange'"
154+
:placeholder="'请选择日期'"
155+
size="sm"
156+
@change="monthChange"
157+
></c-datepicker>
158+
</template>
159+
160+
<script>
161+
export default {
162+
data () {
163+
return {
164+
monthrange: ['2018-01', '2018-02']
165+
}
166+
},
167+
methods: {
168+
monthChange (date) {
169+
this.daterange = date
170+
}
171+
}
172+
}
173+
</script>
174+
```
175+
147176
## API
148177

149178
> 注意:当type为daterange时,v-model需要传递起始时间和终止时间的数组

src/components/datepicker/index.vue

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
.c-datepicker__icon(:clas="className")
1212
c-icon(name="calendar")
1313
c-input(
14-
v-if="type == 'daterange'"
14+
v-if="type == 'daterange' || type == 'monthrange'"
1515
v-model="daterange"
1616
:placeholder="placeholder"
1717
:disabled="disabled"
@@ -31,12 +31,13 @@
3131
@change="dateChange"
3232
@focusin.native="open"
3333
@focusout.native="onBlur"
34+
@keydown.native="onKeyDown"
3435
)
3536

3637
.c-datepicker__panel
3738
c-calendar(
3839
ref="calendar"
39-
v-if="type !== 'daterange'"
40+
v-if="type == 'date' || type == 'month'"
4041
:type="type"
4142
:pattern="datePattern"
4243
:value="date"
@@ -55,6 +56,16 @@
5556
:show="isOpen"
5657
@change="setDateRange"
5758
)
59+
.c-datepicker__body(
60+
v-if="type == 'monthrange'"
61+
)
62+
c-daterange(
63+
:value="date"
64+
:size="size"
65+
:show="isOpen"
66+
type="month"
67+
@change="setDateRange"
68+
)
5869
</template>
5970

6071
<script>
@@ -95,7 +106,7 @@ export default {
95106
return this.size ? `is-size-${this.size}` : ''
96107
},
97108
daterange () {
98-
if (this.type === 'date') return []
109+
if (this.type.indexOf('range') === -1) return []
99110
const [start, end] = this.date
100111
return !start && !end ? '' : `${start}${end}`
101112
},
@@ -124,11 +135,9 @@ export default {
124135
this.resize()
125136
window.addEventListener('mousedown', this.onMouseDown, true)
126137
window.addEventListener('mouseup', this.onMouseUp, true)
127-
window.addEventListener('keydown', this.onKeyDown, false)
128138
} else {
129139
window.removeEventListener('mousedown', this.onMouseDown, true)
130140
window.removeEventListener('mouseup', this.onMouseUp, true)
131-
window.removeEventListener('keydown', this.onKeyDown, false)
132141
}
133142
},
134143
value (newVal) {
@@ -187,19 +196,23 @@ export default {
187196
}
188197
const { keyCode } = e
189198
if (keyCode === keys.ESC) this.close()
199+
const { calendar } = this.$refs
200+
const date = new Date(calendar.year, calendar.month, calendar.day).format(this.datePattern)
190201
if (keyCode === keys.ENTER && this.type === 'date') {
191-
const { calendar } = this.$refs
192-
const date = new Date(calendar.year, calendar.month, calendar.day).format(this.datePattern)
193202
this.setDate(date)
194203
}
195204
if (keyCode === keys.UP) {
196-
this.$refs.calendar.updateDay(7, 'sub')
205+
this.type === 'date' && this.$refs.calendar.updateDay(7, 'sub')
206+
this.type === 'month' && this.$refs.calendar.updateMonthBykeydown(3, 'sub')
197207
} else if (keyCode === keys.DOWN) {
198-
this.$refs.calendar.updateDay(7, 'plus')
208+
this.type === 'date' && this.$refs.calendar.updateDay(7, 'plus')
209+
this.type === 'month' && this.$refs.calendar.updateMonthBykeydown(3, 'plus')
199210
} else if (keyCode === keys.LEFT) {
200-
this.$refs.calendar.updateDay(1, 'sub')
211+
this.type === 'date' && this.$refs.calendar.updateDay(1, 'sub')
212+
this.type === 'month' && this.$refs.calendar.updateMonthBykeydown(1, 'sub')
201213
} else if (keyCode === keys.RIGHT) {
202-
this.$refs.calendar.updateDay(1, 'plus')
214+
this.type === 'date' && this.$refs.calendar.updateDay(1, 'plus')
215+
this.type === 'month' && this.$refs.calendar.updateMonthBykeydown(1, 'plus')
203216
}
204217
},
205218
resetDate (e) {

0 commit comments

Comments
 (0)