2
0
mirror of https://github.com/tenrok/vue2-datepicker.git synced 2026-06-24 01:10:36 +03:00

feat: add type year and month

This commit is contained in:
mxie
2018-07-24 13:59:38 +08:00
7 changed files with 98 additions and 23 deletions
+1 -1
View File
@@ -68,7 +68,7 @@ export default {
| Prop | Type | Default | Description | | Prop | Type | Default | Description |
|---------------------|---------------|-------------|-----------------------------------------------------| |---------------------|---------------|-------------|-----------------------------------------------------|
| type | String | 'date' | select datepicker or datetimepicker(date/datetime) | | type | String | 'date' | select date type (date/datetime/year/month) |
| range | Boolean | false | if true, the type is daterange or datetimerange | | range | Boolean | false | if true, the type is daterange or datetimerange |
| format | String | YYYY-MM-DD | The parsing tokens are similar to the moment.js | | format | String | YYYY-MM-DD | The parsing tokens are similar to the moment.js |
| lang | String/Object | zh | Translation (en/zh/es/pt-br/fr/ru/de/it/cs)(custom) | | lang | String/Object | zh | Translation (en/zh/es/pt-br/fr/ru/de/it/cs)(custom) |
+1 -1
View File
@@ -68,7 +68,7 @@ export default {
| Prop | Type | Default | Description | Prop | Type | Default | Description
|---------------------|---------------|-------------|----------------------------------------------------- |---------------------|---------------|-------------|-----------------------------------------------------
| type | String | 'date' | 选择日期或日期时间(可选:date,datetime) | type | String | 'date' | 选择日期或日期时间(可选:date,datetime,year,month)
| range | Boolean | false | 如果是true, 显示日历范围选择 | range | Boolean | false | 如果是true, 显示日历范围选择
| format | String | YYYY-MM-DD | 格式化显示日期 api类似moment.js | format | String | YYYY-MM-DD | 格式化显示日期 api类似moment.js
| lang | String/Object | zh | 选择语言或自定义 (en/zh/es/pt-br/fr/ru/de/it/cs)(custom) | lang | String/Object | zh | 选择语言或自定义 (en/zh/es/pt-br/fr/ru/de/it/cs)(custom)
+6 -2
View File
@@ -15,7 +15,9 @@ new Vue({ // eslint-disable-line
value6: '', value6: '',
value7: '', value7: '',
value8: '', value8: '',
value9: '' value9: '',
value10: new Date(),
value11: new Date()
} }
}, },
methods: { methods: {
@@ -44,7 +46,9 @@ new Vue({ // eslint-disable-line
render (h) { render (h) {
const example1 = { const example1 = {
'base': '<date-picker v-model="value1" lang="en" :not-before="new Date()"></date-picker>', 'base': '<date-picker v-model="value1" lang="en" :not-before="new Date()"></date-picker>',
'range': '<date-picker v-model="value2" range ></date-picker>' 'range': '<date-picker v-model="value2" range ></date-picker>',
'month': '<date-picker v-model="value10" lang="en" type="month" format="YYYY-MM"></date-picker>',
'year': '<date-picker v-model="value11" lang="en" type="year" format="YYYY"></date-picker>'
} }
const example2 = { const example2 = {
'datetime': ` 'datetime': `
+34 -15
View File
@@ -18,13 +18,16 @@
class="mx-icon-next-month" class="mx-icon-next-month"
@click="handleIconMonth(1)">&rsaquo;</a> @click="handleIconMonth(1)">&rsaquo;</a>
<a <a
v-show="panel !== 'TIME'" v-show="panel === 'DATE'"
class="mx-current-month" class="mx-current-month"
@click="handleBtnMonth">{{months[calendarMonth]}}</a> @click="handleBtnMonth">{{months[calendarMonth]}}</a>
<a <a
v-show="panel !== 'TIME'" v-show="panel === 'DATE' || panel === 'MONTH'"
class="mx-current-year" class="mx-current-year"
@click="handleBtnYear">{{calendarYear}}</a> @click="handleBtnYear">{{calendarYear}}</a>
<a
v-show="panel === 'YEAR'"
class="mx-current-year">{{yearHeader}}</a>
<a <a
v-show="panel === 'TIME'" v-show="panel === 'TIME'"
class="mx-time-header" class="mx-time-header"
@@ -44,11 +47,13 @@
<panel-year <panel-year
v-show="panel === 'YEAR'" v-show="panel === 'YEAR'"
:value="value" :value="value"
:disabled-year="isDisabledYear"
:first-year="firstYear" :first-year="firstYear"
@select="selectYear" /> @select="selectYear" />
<panel-month <panel-month
v-show="panel === 'MONTH'" v-show="panel === 'MONTH'"
:value="value" :value="value"
:disabled-month="isDisabledMonth"
:calendar-year="calendarYear" :calendar-year="calendarYear"
@select="selectMonth" /> @select="selectMonth" />
<panel-time <panel-time
@@ -92,7 +97,7 @@ export default {
// below user set // below user set
type: { type: {
type: String, type: String,
default: 'date' // ['date', 'datetime'] default: 'date' // ['date', 'datetime'] zendy added 'month', 'year'
}, },
firstDayOfWeek: { firstDayOfWeek: {
default: 7, default: 7,
@@ -156,6 +161,9 @@ export default {
timeHeader () { timeHeader () {
return this.value && new Date(this.value).toLocaleDateString() return this.value && new Date(this.value).toLocaleDateString()
}, },
yearHeader () {
return this.firstYear + ' ~ ' + (this.firstYear + 10)
},
months () { months () {
return this.t('months') return this.t('months')
} }
@@ -187,7 +195,13 @@ export default {
} }
}, },
init () { init () {
this.panel = 'DATE' if (this.type.toLowerCase() === 'month') {
this.panel = 'MONTH'
} else if (this.type.toLowerCase() === 'year') {
this.panel = 'YEAR'
} else {
this.panel = 'DATE'
}
this.updateNow(this.value) this.updateNow(this.value)
}, },
// 根据value更新日历 // 根据value更新日历
@@ -218,9 +232,16 @@ export default {
} else if (typeof this.disabledDays === 'function') { } else if (typeof this.disabledDays === 'function') {
disabledDays = this.disabledDays(new Date(date)) disabledDays = this.disabledDays(new Date(date))
} }
return notBefore || notAfter || disabledDays || startAt || endAt return notBefore || notAfter || disabledDays || startAt || endAt
}, },
isDisabledYear (year) {
const date = new Date(year, this.calendarMonth)
return this.isDisabledDate(date)
},
isDisabledMonth (month) {
const date = new Date(this.calendarYear, month)
return this.isDisabledDate(date)
},
selectDate (date) { selectDate (date) {
if (this.type === 'datetime') { if (this.type === 'datetime') {
let time = new Date(date) let time = new Date(date)
@@ -248,10 +269,16 @@ export default {
}, },
selectYear (year) { selectYear (year) {
this.changeCalendarYear(year) this.changeCalendarYear(year)
if (this.type.toLowerCase() === 'year') {
return this.selectDate(new Date(this.now))
}
this.showPanelMonth() this.showPanelMonth()
}, },
selectMonth (month) { selectMonth (month) {
this.changeCalendarMonth(month) this.changeCalendarMonth(month)
if (this.type.toLowerCase() === 'month') {
return this.selectDate(new Date(this.now))
}
this.showPanelDate() this.showPanelDate()
}, },
selectTime (time) { selectTime (time) {
@@ -274,18 +301,10 @@ export default {
} }
}, },
handleBtnYear () { handleBtnYear () {
if (this.panel === 'YEAR') { this.showPanelYear()
this.showPanelDate()
} else {
this.showPanelYear()
}
}, },
handleBtnMonth () { handleBtnMonth () {
if (this.panel === 'MONTH') { this.showPanelMonth()
this.showPanelDate()
} else {
this.showPanelMonth()
}
}, },
changePanelYears (flag) { changePanelYears (flag) {
this.firstYear = this.firstYear + flag * 10 this.firstYear = this.firstYear + flag * 10
+13 -2
View File
@@ -7,10 +7,20 @@ export default {
value: null, value: null,
calendarYear: { calendarYear: {
default: new Date().getFullYear() default: new Date().getFullYear()
} },
disabledMonth: Function
}, },
methods: { methods: {
isDisabled (month) {
if (typeof this.disabledMonth === 'function' && this.disabledMonth(month)) {
return true
}
return false
},
selectMonth (month) { selectMonth (month) {
if (this.isDisabled(month)) {
return
}
this.$emit('select', month) this.$emit('select', month)
} }
}, },
@@ -22,7 +32,8 @@ export default {
return <span return <span
class={{ class={{
'cell': true, 'cell': true,
'actived': currentYear === this.calendarYear && currentMonth === i 'actived': currentYear === this.calendarYear && currentMonth === i,
'disabled': this.isDisabled(i)
}} }}
onClick={this.selectMonth.bind(this, i)}> onClick={this.selectMonth.bind(this, i)}>
{v} {v}
+13 -2
View File
@@ -2,10 +2,20 @@ export default {
name: 'panelYear', name: 'panelYear',
props: { props: {
value: null, value: null,
firstYear: Number firstYear: Number,
disabledYear: Function
}, },
methods: { methods: {
isDisabled (year) {
if (typeof this.disabledYear === 'function' && this.disabledYear(year)) {
return true
}
return false
},
selectYear (year) { selectYear (year) {
if (this.isDisabled(year)) {
return
}
this.$emit('select', year) this.$emit('select', year)
} }
}, },
@@ -18,7 +28,8 @@ export default {
return <span return <span
class={{ class={{
'cell': true, 'cell': true,
'actived': currentYear === year 'actived': currentYear === year,
'disabled': this.isDisabled(year)
}} }}
onClick={this.selectYear.bind(this, year)} onClick={this.selectYear.bind(this, year)}
>{year}</span> >{year}</span>
+30
View File
@@ -409,6 +409,36 @@ describe('calendar-panel', () => {
done() done()
}, 0) }, 0)
}) })
it('prop: type year', () => {
wrapper = mount(CalendarPanel, {
propsData: {
type: 'year',
value: new Date(2018, 1, 1)
}
})
const td = wrapper.find('.mx-panel-year .cell:nth-child(1)')
td.trigger('click')
const expectDate = new Date(2010, 1, 1)
expect(wrapper.emitted()).toEqual({
'select-date': [[expectDate]]
})
})
it('prop: type month', () => {
wrapper = mount(CalendarPanel, {
propsData: {
type: 'month',
value: new Date(2018, 1, 1)
}
})
const td = wrapper.find('.mx-panel-month .cell:nth-child(1)')
td.trigger('click')
const expectDate = new Date(2018, 0, 1)
expect(wrapper.emitted()).toEqual({
'select-date': [[expectDate]]
})
})
}) })
describe('date-panel', () => { describe('date-panel', () => {