Skip to content

Commit a0b1e5c

Browse files
committed
update date-picker
1 parent ff77a22 commit a0b1e5c

File tree

1 file changed

+79
-46
lines changed

1 file changed

+79
-46
lines changed

example/components/date-picker.vue

Lines changed: 79 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -39,75 +39,108 @@
3939
},
4040
data() {
4141
return {
42+
dateData: [],
4243
tempIndex: [0, 0, 0],
43-
values: [0, 0, 0]
44-
}
45-
},
46-
mounted() {
47-
this.values = [this.years[this.selectedIndex[0]], this.month[this.selectedIndex[1]], this.day[this.selectedIndex[2]]]
48-
this.tempIndex = this.selectedIndex
49-
},
50-
computed: {
51-
years() {
52-
return range(this.min[0], this.max[0], false, '')
53-
},
54-
months() {
55-
let minMonth = !this.tempIndex[0] ? this.min[1] : 1
56-
let maxMonth = this.tempIndex[0] === this.years.length - 1 ? this.max[1] : 12
57-
58-
return range(minMonth, maxMonth, false, '')
59-
},
60-
days() {
61-
const currentYear = this.years[this.tempIndex[0]].value
62-
const currentMonth = this.months[this.tempIndex[1]].value
63-
64-
let day = 30
65-
if ([1, 3, 5, 7, 8, 10, 12].includes(currentMonth)) {
66-
day = 31
67-
} else {
68-
if (currentMonth === 2) {
69-
day = !(currentYear % 400) || (!(currentYear % 4) && currentYear % 100) ? 29 : 28
70-
}
71-
}
72-
73-
let minDay = !this.tempIndex[0] && !this.tempIndex[1] ? this.min[2] : 1
74-
let maxDay = this.tempIndex[0] === this.years.length - 1 && this.tempIndex[1] === this.months.length - 1 ? this.max[2] : day
75-
let days = range(minDay, maxDay, false, '')
76-
77-
this.$refs.picker.setData([this.years, this.months, this.days], this.tempIndex)
78-
79-
return days
80-
},
81-
dateData() {
82-
return [this.years, this.months, this.days]
44+
years: [],
45+
months: [],
46+
days: [],
47+
year: 0,
48+
month: 0,
49+
day: 0
8350
}
8451
},
8552
watch: {
86-
days() {
87-
this.$refs.picker.setData(this.dateData, [this.yearIndex, this.monthIndex, this.dayIndex])
88-
this.$refs.picker.refresh()
53+
year() {
54+
this.updateMonths()
55+
},
56+
month() {
57+
this.updateDays()
8958
}
9059
},
9160
methods: {
9261
show() {
9362
this.$refs.picker.show()
63+
this.years = range(this.min[0], this.max[0], false, '')
64+
this.year = this.years[this.selectedIndex[0]].value
65+
this.updateMonths()
66+
this.month = this.months[this.selectedIndex[1]].value
67+
this.updateDays()
68+
this.day = this.days[this.selectedIndex[2]].value
69+
this.tempIndex = this.selectedIndex
9470
},
9571
hide() {
9672
this.$refs.picker.hide()
9773
},
9874
change(i, newIndex) {
9975
if (newIndex !== this.tempIndex[i]) {
100-
this.tempIndex.splice(i, 1, newIndex)
76+
this.tempIndex[i] = newIndex
10177
}
102-
if (this.dateData[i][newIndex] !== this.value[i]) {
103-
this.values.splice(i, 1, this.dateData[i][newIndex])
78+
const keyMap = ['year', 'month', 'day']
79+
if (this.dateData[i][newIndex] !== this[keyMap[i]]) {
80+
this[keyMap[i]] = this.dateData[i][newIndex].value
10481
}
10582
},
10683
select(selectedVal, selectedIndex, selectedText) {
10784
this.$emit(EVENT_SELECT, selectedVal, selectedIndex, selectedText)
10885
},
10986
cancel() {
11087
this.$emit(EVENT_CANCEL)
88+
},
89+
updateMonths() {
90+
let minMonth = this.year === this.min[0] ? this.min[1] : 1
91+
let maxMonth = this.year === this.max[0] ? this.max[1] : 12
92+
93+
this.months = range(minMonth, maxMonth, false, '')
94+
95+
/* Try to keep the same month */
96+
const findIndex = this.months.findIndex((item) => {
97+
return item.value === this.month
98+
})
99+
if (findIndex !== -1) {
100+
this.tempIndex.splice(1, 1, findIndex)
101+
this.updateDays()
102+
} else {
103+
if (this.month < this.months[0].value) {
104+
this.month = this.months[0].value
105+
this.tempIndex[1] = 0
106+
} else {
107+
this.month = this.months[this.months.length - 1].value
108+
this.tempIndex[1] = this.months.length - 1
109+
}
110+
}
111+
},
112+
updateDays() {
113+
let day = 30
114+
if ([1, 3, 5, 7, 8, 10, 12].includes(this.month)) {
115+
day = 31
116+
} else {
117+
if (this.month === 2) {
118+
day = !(this.year % 400) || (!(this.year % 4) && this.year % 100) ? 29 : 28
119+
}
120+
}
121+
122+
let minDay = this.year === this.min[0] && this.month === this.min[1] ? this.min[2] : 1
123+
let maxDay = this.year === this.max[0] && this.month === this.max[1] ? this.max[2] : day
124+
125+
this.days = range(minDay, maxDay, false, '')
126+
127+
/* Try to keep the same day */
128+
const self = this
129+
const findIndex = this.days.findIndex((item) => {
130+
return item.value === self.day
131+
})
132+
if (findIndex !== -1) {
133+
this.tempIndex[2] = findIndex
134+
} else {
135+
this.day = this.day < this.days[0].value ? this.days[0].value : this.days[this.days.length - 1].value
136+
}
137+
138+
this.dateData = [this.years, this.months, this.days]
139+
// make sure picker render before call refillColumn
140+
this.$nextTick(() => {
141+
this.$refs.picker.scrollTo(1, this.tempIndex[1])
142+
this.$refs.picker.scrollTo(2, this.tempIndex[2])
143+
})
111144
}
112145
}
113146
}

0 commit comments

Comments
 (0)