|
39 | 39 | }, |
40 | 40 | data() { |
41 | 41 | return { |
| 42 | + dateData: [], |
42 | 43 | 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 |
83 | 50 | } |
84 | 51 | }, |
85 | 52 | 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() |
89 | 58 | } |
90 | 59 | }, |
91 | 60 | methods: { |
92 | 61 | show() { |
93 | 62 | 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 |
94 | 70 | }, |
95 | 71 | hide() { |
96 | 72 | this.$refs.picker.hide() |
97 | 73 | }, |
98 | 74 | change(i, newIndex) { |
99 | 75 | if (newIndex !== this.tempIndex[i]) { |
100 | | - this.tempIndex.splice(i, 1, newIndex) |
| 76 | + this.tempIndex[i] = newIndex |
101 | 77 | } |
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 |
104 | 81 | } |
105 | 82 | }, |
106 | 83 | select(selectedVal, selectedIndex, selectedText) { |
107 | 84 | this.$emit(EVENT_SELECT, selectedVal, selectedIndex, selectedText) |
108 | 85 | }, |
109 | 86 | cancel() { |
110 | 87 | 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 | + }) |
111 | 144 | } |
112 | 145 | } |
113 | 146 | } |
|
0 commit comments