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

Merge pull request #6 from Arkitecht/master

Add additional props
This commit is contained in:
MXie
2017-08-12 05:27:04 -05:00
committed by GitHub
4 changed files with 93 additions and 16 deletions
+11 -7
View File
@@ -39,12 +39,16 @@ export default {
``` ```
## Attributes ## Attributes
| Prop | Type | Default | Description | | Prop | Type | Default | Description |
|-----------------|---------------|-------------|---------------------------------------| |-----------------|---------------|-------------|---------------------------------------------------|
| range | Boolean | false | if true, the type is daterange | | range | Boolean | false | if true, the type is daterange |
| format | String | yyyy-MM-dd | Date formatting string | | format | String | yyyy-MM-dd | Date formatting string |
| lang | String | zh | Translation (en/zh/es) | | lang | String | zh | Translation (en/zh/es) |
| placeholder | String | | input placeholder text | | placeholder | String | | input placeholder text |
| width | String/Number | 210 | input size | | width | String/Number | 210 | input size |
| disabledDays | Array | [] | Days in YYYY-MM-DD format to disable |
| showYearNav | Boolean | true | Show the year nav in the calendar |
| notBefore | String | '' | Disable all dates before date in YYY-MM-DD format |
| notAfter | String | '' | Disable all dates after date in YYY-MM-DD format |
+40 -4
View File
@@ -1,9 +1,9 @@
<template> <template>
<div class="calendar"> <div class="calendar">
<div class="calendar-header"> <div class="calendar-header">
<a class="calendar__prev-icon" @click="changeYear(-1)">&laquo;</a> <a v-show="showYearNav" class="calendar__prev-icon" @click="changeYear(-1)">&laquo;</a>
<a v-show="currentPanel === 'date'" class="calendar__prev-icon" @click="changeMonth(-1)">&lsaquo;</a> <a v-show="currentPanel === 'date'" class="calendar__prev-icon" @click="changeMonth(-1)">&lsaquo;</a>
<a class="calendar__next-icon" @click="changeYear(1)">&raquo;</a> <a v-show="showYearNav" class="calendar__next-icon" @click="changeYear(1)">&raquo;</a>
<a v-show="currentPanel === 'date'" class="calendar__next-icon" @click="changeMonth(1)" >&rsaquo;</a> <a v-show="currentPanel === 'date'" class="calendar__next-icon" @click="changeMonth(1)" >&rsaquo;</a>
<a @click="showMonths">{{months[currentMonth]}}</a> <a @click="showMonths">{{months[currentMonth]}}</a>
<a @click="showYears">{{currentYear}}</a> <a @click="showYears">{{currentYear}}</a>
@@ -40,7 +40,23 @@ export default {
startAt: null, startAt: null,
endAt: null, endAt: null,
value: null, value: null,
show: Boolean show: Boolean,
disabledDays: {
type: Array,
default: function () { return [] }
},
showYearNav: {
type: Boolean,
default: true
},
notBefore: {
type: String,
default: ''
},
notAfter: {
type: String,
default: ''
}
}, },
data () { data () {
const translation = this.$parent.translation const translation = this.$parent.translation
@@ -88,15 +104,17 @@ export default {
function getCalendar (time, firstday, length, classes) { function getCalendar (time, firstday, length, classes) {
return Array.apply(null, { length }).map((v, i) => { // eslint-disable-line return Array.apply(null, { length }).map((v, i) => { // eslint-disable-line
let day = firstday + i let day = firstday + i
const date = new Date(time.getFullYear(), time.getMonth(), day) const date = new Date(time.getFullYear(), time.getMonth(), day, 0, 0, 0)
return { return {
title: date.toLocaleDateString(), title: date.toLocaleDateString(),
iso: cal.isoDate(date),
date, date,
day, day,
classes classes
} }
}) })
} }
var cal = this;
const time = new Date(this.now) const time = new Date(this.now)
time.setDate(0) // 把时间切换到上个月最后一天 time.setDate(0) // 把时间切换到上个月最后一天
const lastMonthLength = time.getDay() + 1 // time.getDay() 0是星期天, 1是星期一 ... const lastMonthLength = time.getDay() + 1 // time.getDay() 0是星期天, 1是星期一 ...
@@ -121,6 +139,15 @@ export default {
} }
this.dates = result this.dates = result
}, },
isoDate(date) {
function doubleDigits(num) {
if ( parseInt(num) < 10 ) {
return '0'+num;
}
return num;
}
return date.getFullYear()+'-'+doubleDigits((date.getMonth()+1))+'-'+doubleDigits(date.getDate());
},
getClasses (cell) { getClasses (cell) {
const classes = [] const classes = []
const cellTime = cell.date.getTime() const cellTime = cell.date.getTime()
@@ -131,6 +158,15 @@ export default {
classes.push(cell.classes) classes.push(cell.classes)
if ( typeof this.disabledDays.find(function(disabledDate) { return disabledDate === cell.iso } ) !== 'undefined' ) {
classes.push('disabled');
} else if (
(this.notBefore !== '' && cell.date.getTime() < (new Date(this.notBefore)).getTime()) ||
(this.notAfter !== '' && cell.date.getTime() > (new Date(this.notAfter+' 00:00:00')).getTime())
) {
classes.push('disabled');
}
if (cellTime === today) { if (cellTime === today) {
classes.push('today') classes.push('today')
} }
+41 -4
View File
@@ -20,14 +20,35 @@
ref="calendar" ref="calendar"
v-show="showPopup"> v-show="showPopup">
<template v-if="!range"> <template v-if="!range">
<calendar-panel @select="showPopup = false" v-model="currentValue" :show="showPopup"></calendar-panel> <calendar-panel @select="showPopup = false"
v-model="currentValue"
:show="showPopup"
:disabledDays="disabledDays"
:showYearNav="showYearNav"
:notBefore="notBefore"
:notAfter="notAfter"></calendar-panel>
</template> </template>
<template v-else> <template v-else>
<div class="datepicker-top"> <div class="datepicker-top">
<span v-for="range in ranges" @click="selectRange(range)">{{range.text}}</span> <span v-for="range in ranges" @click="selectRange(range)">{{range.text}}</span>
</div> </div>
<calendar-panel style="width:50%;box-shadow:1px 0 rgba(0, 0, 0, .1)" v-model="currentValue[0]" :end-at="currentValue[1]" :show="showPopup"></calendar-panel> <calendar-panel style="width:50%;box-shadow:1px 0 rgba(0, 0, 0, .1)"
<calendar-panel style="width:50%;" v-model="currentValue[1]" :start-at="currentValue[0]" :show="showPopup"></calendar-panel> v-model="currentValue[0]"
:end-at="currentValue[1]"
:show="showPopup"
:disabledDays="disabledDays"
:showYearNav="showYearNav"
:notBefore="notBefore"
:notAfter="notAfter"
></calendar-panel>
<calendar-panel style="width:50%;"
v-model="currentValue[1]"
:start-at="currentValue[0]"
:show="showPopup"
:disabledDays="disabledDays"
:showYearNav="showYearNav"
:notBefore="notBefore"
:notAfter="notAfter"></calendar-panel>
</template> </template>
</div> </div>
</div> </div>
@@ -57,7 +78,23 @@ export default {
type: String, type: String,
default: 'zh' default: 'zh'
}, },
value: null value: null,
disabledDays: {
type: Array,
default: function () { return [] }
},
showYearNav: {
type: Boolean,
default: true
},
notBefore: {
type: String,
default: ''
},
notAfter: {
type: String,
default: ''
}
}, },
data () { data () {
return { return {
+1 -1
View File
@@ -2,7 +2,7 @@
"name": "vue2-datepicker", "name": "vue2-datepicker",
"description": "A Datepicker Component For Vue2", "description": "A Datepicker Component For Vue2",
"main": "datepicker/index.vue", "main": "datepicker/index.vue",
"version": "1.2.1", "version": "1.2.3",
"scripts": { "scripts": {
"dev": "cross-env NODE_ENV=development webpack-dev-server --open --hot", "dev": "cross-env NODE_ENV=development webpack-dev-server --open --hot",
"build": "cross-env NODE_ENV=production webpack --progress --hide-modules" "build": "cross-env NODE_ENV=production webpack --progress --hide-modules"