Skip to content

Commit ceae5fb

Browse files
xiaolongyuanairyland
authored andcommitted
inline-calendar: support multi select (Close #1446) (#1467)
1 parent 04d9746 commit ceae5fb

File tree

3 files changed

+86
-17
lines changed

3 files changed

+86
-17
lines changed

src/components/inline-calendar/index.vue

Lines changed: 64 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
v-for="(child,k2) in day"
3535
:data-date="formatDate(year, month, child)"
3636
:data-current="currentValue"
37-
:class="buildClass(k2, child, formatDate(year, month, child) === currentValue && !child.isLastMonth && !child.isNextMonth)"
37+
:class="buildClass(k2, child)"
3838
@click="select(k1,k2,child)">
3939
<slot
4040
:year="year"
@@ -68,19 +68,28 @@ export default {
6868
props: props(),
6969
data () {
7070
return {
71+
multi: false,
7172
year: 0,
7273
month: 0,
7374
days: [],
7475
today: format(new Date(), 'YYYY-MM-DD'),
75-
months: ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'],
76+
months: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'],
7677
currentValue: ''
7778
}
7879
},
7980
created () {
8081
this.currentValue = this.value
82+
this.multi = Object.prototype.toString.call(this.currentValue) === '[object Array]'
8183
},
8284
mounted () {
83-
this.currentValue = this.convertDate(this.currentValue)
85+
if (this.multi) {
86+
for (let i = 0; i < this.currentValue.length; i++) {
87+
this.$set(this.currentValue, i, this.convertDate(this.currentValue[i]))
88+
}
89+
} else {
90+
this.currentValue = this.convertDate(this.currentValue)
91+
}
92+
8493
this.render(this.renderMonth[0], this.renderMonth[1] - 1)
8594
},
8695
computed: {
@@ -101,17 +110,17 @@ export default {
101110
},
102111
watch: {
103112
value (val) {
104-
this.currentValue = val
113+
this.currentValue = this.multi ? val : this.convertDate(val)
105114
},
106115
currentValue (val) {
107-
this.currentValue = this.convertDate(val)
116+
const value = this.multi ? this.currentValue[this.currentValue.length - 1] : this.currentValue
108117
if (this.renderOnValueChange) {
109-
this.render(null, null, val)
118+
this.render(null, null, value)
110119
} else {
111-
this.render(this.year, this.month, this.currentValue)
120+
this.render(this.year, this.month, value)
112121
}
113-
this.$emit('input', val)
114-
this.$emit('on-change', val)
122+
this.$emit('input', this.currentValue)
123+
this.$emit('on-change', this.currentValue)
115124
},
116125
renderFunction () {
117126
this.render(this.year, this.month, this.currentValue)
@@ -149,7 +158,15 @@ export default {
149158
convertDate (date) {
150159
return date === 'TODAY' ? this.today : date
151160
},
152-
buildClass (index, child, isCurrent) {
161+
buildClass (index, child) {
162+
let isCurrent = false
163+
if (!child.isLastMonth && !child.isNextMonth) {
164+
if (this.multi && this.currentValue.length > 0) {
165+
isCurrent = this.currentValue.indexOf(this.formatDate(this.year, this.month, child)) > -1
166+
} else {
167+
isCurrent = this.currentValue === this.formatDate(this.year, this.month, child)
168+
}
169+
}
153170
const className = {
154171
current: child.current || isCurrent,
155172
'is-disabled': child.disabled,
@@ -159,10 +176,12 @@ export default {
159176
return className
160177
},
161178
render (year, month) {
162-
let data = getDays({
179+
let data = null
180+
const value = this.multi ? this.currentValue[this.currentValue.length - 1] : this.currentValue
181+
data = getDays({
163182
year: year,
164183
month: month,
165-
value: this.currentValue,
184+
value,
166185
rangeBegin: this.convertDate(this.startDate),
167186
rangeEnd: this.convertDate(this.endDate),
168187
returnSixRows: this.returnSixRows,
@@ -207,12 +226,42 @@ export default {
207226
if (!data.isBetween) {
208227
return
209228
}
229+
let _currentValue = null
210230
if (!data.isLastMonth && !data.isNextMonth) {
211231
this.days[k1][k2].current = true
212-
this.currentValue = [this.year, zero(this.month + 1), zero(this.days[k1][k2].day)].join('-')
232+
_currentValue = [this.year, zero(this.month + 1), zero(this.days[k1][k2].day)].join('-')
233+
} else {
234+
_currentValue = [data.year, zero(data.month + 1), zero(data.day)].join('-')
235+
}
236+
if (this.multi) {
237+
let index = this.currentValue.indexOf(_currentValue)
238+
if (index > -1) {
239+
this.currentValue.splice(index, 1)
240+
} else {
241+
this.currentValue.push(_currentValue)
242+
}
243+
} else {
244+
this.currentValue = this.currentValue === _currentValue ? '' : _currentValue
245+
}
246+
247+
this.currentValueChange()
248+
},
249+
currentValueChange () {
250+
if (this.multi) {
251+
for (let i = 0; i < this.currentValue.length; i++) {
252+
this.$set(this.currentValue, i, this.convertDate(this.currentValue[i]))
253+
}
213254
} else {
214-
this.currentValue = [data.year, zero(data.month + 1), zero(data.day)].join('-')
255+
this.currentValue = this.convertDate(this.currentValue)
215256
}
257+
const value = this.multi ? this.currentValue[this.currentValue.length - 1] : this.currentValue
258+
if (this.renderOnValueChange) {
259+
this.render(null, null, value)
260+
} else {
261+
this.render(this.year, this.month, value)
262+
}
263+
this.$emit('input', this.currentValue)
264+
this.$emit('on-change', this.currentValue)
216265
},
217266
showChild (year, month, child) {
218267
if (this.replaceText(child.day, this.formatDate(year, month, child))) {
@@ -224,7 +273,7 @@ export default {
224273
}
225274
}
226275
</script>
227-
276+
228277
<style lang="less">
229278
@import '../../styles/variable.less';
230279

src/components/inline-calendar/metas.yml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ tags:
88
- 表单
99
props:
1010
value:
11-
type: String
11+
type: String, Array
1212
default: ''
1313
en: calendar value, use `v-modle` for binding
1414
zh-CN: 当前选中日期,使用`v-model`绑定,默认为空,即选中当天日期
@@ -97,6 +97,26 @@ events:
9797
en: emits when value is changed
9898
zh-CN: 值变化时触发
9999
changes:
100+
v2.4.0:
101+
en:
102+
- '[enhance] re-render when render-month is changed'
103+
zh-CN:
104+
- '[enhance] 当 render-month 变化时重新渲染日历'
105+
v2.3.8:
106+
en:
107+
- '[enhance] prevent from clicking invisiable date #1564'
108+
zh-CN:
109+
- '[enhance] 不可见日期不可点击 #1564'
110+
v2.3.6:
111+
en:
112+
- '[change] render-function params day => date #1361'
113+
zh-CN:
114+
- '[change] render-function 参数 day => date(在 3.0 版本前不会影响目前使用)#1361'
115+
v2.3.5:
116+
en:
117+
- '[feature] support multi select #1446 #1467'
118+
zh-CN:
119+
- '[feature] 支持多选 #1446 #1467'
100120
v2.4.0:
101121
en:
102122
- '[enhance] re-render when render-month is changed'

src/components/inline-calendar/props.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export default () => ({
22
value: {
3-
type: String,
3+
type: [String, Array],
44
default: ''
55
},
66
renderMonth: {

0 commit comments

Comments
 (0)