DatePicker: disabled year or month, fixed #758

This commit is contained in:
qingwei.li 2016-11-04 18:14:50 +08:00
parent 9f945ad782
commit a2f7316b69
6 changed files with 78 additions and 29 deletions

View File

@ -2,44 +2,44 @@
<table @click="handleMonthTableClick" class="el-month-table"> <table @click="handleMonthTableClick" class="el-month-table">
<tbody> <tbody>
<tr> <tr>
<td :class="{ current: month === 0 }"> <td :class="getCellStyle(0)">
<a class="cell">{{ $t('el.datepicker.months.jan') }}</a> <a class="cell">{{ $t('el.datepicker.months.jan') }}</a>
</td> </td>
<td :class="{ current: month === 1 }"> <td :class="getCellStyle(1)">
<a class="cell">{{ $t('el.datepicker.months.feb') }}</a> <a class="cell">{{ $t('el.datepicker.months.feb') }}</a>
</td> </td>
<td :class="{ current: month === 2 }"> <td :class="getCellStyle(2)">
<a class="cell">{{ $t('el.datepicker.months.mar') }}</a> <a class="cell">{{ $t('el.datepicker.months.mar') }}</a>
</td> </td>
<td :class="{ current: month === 3 }"> <td :class="getCellStyle(3)">
<a class="cell">{{ $t('el.datepicker.months.apr') }}</a> <a class="cell">{{ $t('el.datepicker.months.apr') }}</a>
</td> </td>
</tr> </tr>
<tr> <tr>
<td :class="{ current: month === 4 }"> <td :class="getCellStyle(4)">
<a class="cell">{{ $t('el.datepicker.months.may') }}</a> <a class="cell">{{ $t('el.datepicker.months.may') }}</a>
</td> </td>
<td :class="{ current: month === 5 }"> <td :class="getCellStyle(5)">
<a class="cell">{{ $t('el.datepicker.months.jun') }}</a> <a class="cell">{{ $t('el.datepicker.months.jun') }}</a>
</td> </td>
<td :class="{ current: month === 6 }"> <td :class="getCellStyle(6)">
<a class="cell">{{ $t('el.datepicker.months.jul') }}</a> <a class="cell">{{ $t('el.datepicker.months.jul') }}</a>
</td> </td>
<td :class="{ current: month === 7 }"> <td :class="getCellStyle(7)">
<a class="cell">{{ $t('el.datepicker.months.aug') }}</a> <a class="cell">{{ $t('el.datepicker.months.aug') }}</a>
</td> </td>
</tr> </tr>
<tr> <tr>
<td :class="{ current: month === 8 }"> <td :class="getCellStyle(8)">
<a class="cell">{{ $t('el.datepicker.months.sep') }}</a> <a class="cell">{{ $t('el.datepicker.months.sep') }}</a>
</td> </td>
<td :class="{ current: month === 9 }"> <td :class="getCellStyle(9)">
<a class="cell">{{ $t('el.datepicker.months.oct') }}</a> <a class="cell">{{ $t('el.datepicker.months.oct') }}</a>
</td> </td>
<td :class="{ current: month === 10 }"> <td :class="getCellStyle(10)">
<a class="cell">{{ $t('el.datepicker.months.nov') }}</a> <a class="cell">{{ $t('el.datepicker.months.nov') }}</a>
</td> </td>
<td :class="{ current: month === 11 }"> <td :class="getCellStyle(11)">
<a class="cell">{{ $t('el.datepicker.months.dec') }}</a> <a class="cell">{{ $t('el.datepicker.months.dec') }}</a>
</td> </td>
</tr> </tr>
@ -49,18 +49,34 @@
<script type="text/babel"> <script type="text/babel">
import Locale from 'element-ui/src/mixins/locale'; import Locale from 'element-ui/src/mixins/locale';
import { hasClass } from 'wind-dom/src/class';
export default { export default {
props: { props: {
disabledDate: {},
date: {},
month: { month: {
type: Number type: Number
} }
}, },
mixins: [Locale], mixins: [Locale],
methods: { methods: {
getCellStyle(month) {
const style = {};
const date = new Date(this.date);
date.setMonth(month);
style.disabled = typeof this.disabledDate === 'function' &&
this.disabledDate(date);
style.current = this.month === month;
return style;
},
handleMonthTableClick(event) { handleMonthTableClick(event) {
const target = event.target; const target = event.target;
if (target.tagName !== 'A') return; if (target.tagName !== 'A') return;
if (hasClass(target.parentNode, 'disabled')) return;
const column = target.parentNode.cellIndex; const column = target.parentNode.cellIndex;
const row = target.parentNode.parentNode.rowIndex; const row = target.parentNode.parentNode.rowIndex;
const month = row * 4 + column; const month = row * 4 + column;

View File

@ -2,38 +2,38 @@
<table @click="handleYearTableClick" class="el-year-table"> <table @click="handleYearTableClick" class="el-year-table">
<tbody> <tbody>
<tr> <tr>
<td class="available" :class="{ current: year === startYear }"> <td class="available" :class="getCellStyle(startYear + 0)">
<a class="cell">{{ startYear }}</a> <a class="cell">{{ startYear }}</a>
</td> </td>
<td class="available" :class="{ current: year === startYear + 1 }"> <td class="available" :class="getCellStyle(startYear + 1)">
<a class="cell">{{ startYear + 1 }}</a> <a class="cell">{{ startYear + 1 }}</a>
</td> </td>
<td class="available" :class="{ current: year === startYear + 2 }"> <td class="available" :class="getCellStyle(startYear + 2)">
<a class="cell">{{ startYear + 2 }}</a> <a class="cell">{{ startYear + 2 }}</a>
</td> </td>
<td class="available" :class="{ current: year === startYear + 3 }"> <td class="available" :class="getCellStyle(startYear + 3)">
<a class="cell">{{ startYear + 3 }}</a> <a class="cell">{{ startYear + 3 }}</a>
</td> </td>
</tr> </tr>
<tr> <tr>
<td class="available" :class="{ current: year === startYear + 4 }"> <td class="available" :class="getCellStyle(startYear + 4)">
<a class="cell">{{ startYear + 4 }}</a> <a class="cell">{{ startYear + 4 }}</a>
</td> </td>
<td class="available" :class="{ current: year === startYear + 5 }"> <td class="available" :class="getCellStyle(startYear + 5)">
<a class="cell">{{ startYear + 5 }}</a> <a class="cell">{{ startYear + 5 }}</a>
</td> </td>
<td class="available" :class="{ current: year === startYear + 6 }"> <td class="available" :class="getCellStyle(startYear + 6)">
<a class="cell">{{ startYear + 6 }}</a> <a class="cell">{{ startYear + 6 }}</a>
</td> </td>
<td class="available" :class="{ current: year === startYear + 7 }"> <td class="available" :class="getCellStyle(startYear + 7)">
<a class="cell">{{ startYear + 7 }}</a> <a class="cell">{{ startYear + 7 }}</a>
</td> </td>
</tr> </tr>
<tr> <tr>
<td class="available" :class="{ current: year === startYear + 8 }"> <td class="available" :class="getCellStyle(startYear + 8)">
<a class="cell">{{ startYear + 8 }}</a> <a class="cell">{{ startYear + 8 }}</a>
</td> </td>
<td class="available" :class="{ current: year === startYear + 9 }"> <td class="available" :class="getCellStyle(startYear + 9)">
<a class="cell">{{ startYear + 9 }}</a> <a class="cell">{{ startYear + 9 }}</a>
</td> </td>
<td></td> <td></td>
@ -43,9 +43,13 @@
</table> </table>
</template> </template>
<script type="text/ecmascript-6"> <script type="text/babel">
import { hasClass } from 'wind-dom/src/class';
export default { export default {
props: { props: {
disabledDate: {},
date: {},
year: { year: {
type: Number type: Number
} }
@ -58,6 +62,18 @@
}, },
methods: { methods: {
getCellStyle(year) {
const style = {};
const date = new Date(this.date);
date.setFullYear(year);
style.disabled = typeof this.disabledDate === 'function' &&
this.disabledDate(date);
style.current = this.year === year;
return style;
},
nextTenYear() { nextTenYear() {
this.$emit('pick', this.year + 10, false); this.$emit('pick', this.year + 10, false);
}, },
@ -69,6 +85,7 @@
handleYearTableClick(event) { handleYearTableClick(event) {
const target = event.target; const target = event.target;
if (target.tagName === 'A') { if (target.tagName === 'A') {
if (hasClass(target.parentNode, 'disabled')) return;
const year = parseInt(target.textContent || target.innerText, 10); const year = parseInt(target.textContent || target.innerText, 10);
this.$emit('pick', year); this.$emit('pick', year);
} }

View File

@ -93,13 +93,17 @@
<year-table <year-table
ref="yearTable" ref="yearTable"
:year="year" :year="year"
:date="date"
v-show="currentView === 'year'" v-show="currentView === 'year'"
@pick="handleYearPick"> @pick="handleYearPick"
:disabled-date="disabledDate">
</year-table> </year-table>
<month-table <month-table
:month="month" :month="month"
:date="date"
v-show="currentView === 'month'" v-show="currentView === 'month'"
@pick="handleMonthPick"> @pick="handleMonthPick"
:disabled-date="disabledDate">
</month-table> </month-table>
</div> </div>
</div> </div>

View File

@ -57,7 +57,6 @@
opacity: 1; opacity: 1;
cursor: not-allowed; cursor: not-allowed;
color: #ccc; color: #ccc;
text-decoration: line-through;
} }
&.week { &.week {

View File

@ -4,12 +4,19 @@
@b month-table { @b month-table {
font-size: 12px; font-size: 12px;
margin: -1px; margin: -1px;
border-collapse: collapse;
td { td {
text-align: center; text-align: center;
padding: 20px 3px; padding: 20px 3px;
cursor: pointer; cursor: pointer;
&.disabled .cell {
background-color: #f4f4f4;
cursor: not-allowed;
color: #ccc;
}
.cell { .cell {
width: 48px; width: 48px;
height: 32px; height: 32px;

View File

@ -14,16 +14,22 @@
padding: 20px 3px; padding: 20px 3px;
cursor: pointer; cursor: pointer;
&.disabled .cell {
background-color: #f4f4f4;
cursor: not-allowed;
color: #ccc;
}
.cell { .cell {
width: 48px; width: 48px;
height: 32px; height: 32px;
display: block; display: block;
line-height: 32px; line-height: 32px;
color: var(--datepicker-color); color: var(--datepicker-color);
}
&.available .cell:hover { &:hover {
background-color: var(--datepicker-cell-hover-color); background-color: var(--datepicker-cell-hover-color);
}
} }
&.current .cell { &.current .cell {