feat(Calendar): add default-date prop and remove v-model

This commit is contained in:
陈嘉涵 2019-12-26 10:10:29 +08:00 committed by neverland
parent 779dcf4e54
commit f49c21a7ca
4 changed files with 42 additions and 51 deletions

View File

@ -23,7 +23,7 @@ Vue.use(Calendar);
<!-- 点击单元格后唤起日历组件 --> <!-- 点击单元格后唤起日历组件 -->
<van-cell <van-cell
title="选择单个日期" title="选择单个日期"
:value="formattedDate" :value="selectedDate"
@click="showCalendar = true" @click="showCalendar = true"
/> />
@ -35,7 +35,7 @@ Vue.use(Calendar);
position="bottom" position="bottom"
style="height: 80vh;" style="height: 80vh;"
> >
<van-calendar v-model="currentDate" /> <van-calendar @select="onSelect" />
</van-popup> </van-popup>
``` ```
@ -44,18 +44,14 @@ export default {
data() { data() {
return { return {
// 当前选中的日期 // 当前选中的日期
currentDate: null, selectedDate: '',
showCalendar: false showCalendar: false
}; };
}, },
computed: { computed: {
// 将 Date 格式化为 YYYY/MM/DD 的格式 onSelect(date) {
formattedDate() { this.selectedDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
const date = this.currentDate;
if (date) {
return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
}
} }
} }
} }
@ -63,28 +59,23 @@ export default {
### 选择日期区间 ### 选择日期区间
设置`type``range`后可以选择日期区间,此时`v-model`绑定的 currrentDate 为数组结构,数组第一项为开始时间,第二项为结束时间。 设置`type``range`后可以选择日期区间,此时`select`事件返回的 date 为数组结构,数组第一项为开始时间,第二项为结束时间。
```html ```html
<van-calendar <van-calendar type="range" @select="onSelect" />
v-model="currentDate"
type="range"
@select="onSelect"
/>
``` ```
```js ```js
export default { export default {
data() { data() {
return { return {
currrentDate: [] selectedDate: []
}; };
}, },
methods: { methods: {
onSelect(date) { onSelect(date) {
console.log('开始时间: ' + date[0]); this.selectedDate = date;
console.log('结束时间: ' + date[1]);
} }
} }
} }
@ -96,11 +87,11 @@ export default {
| 参数 | 说明 | 类型 | 默认值 | 版本 | | 参数 | 说明 | 类型 | 默认值 | 版本 |
|------|------|------|------|------| |------|------|------|------|------|
| v-model | 当前选中的日期 | *Date* | - | - |
| type | 选择类型,`single`表示选择单个日期,<br>`range`表示选择日期区间 | *string* | `single` | - | | type | 选择类型,`single`表示选择单个日期,<br>`range`表示选择日期区间 | *string* | `single` | - |
| title | 日历标题 | *string* | `日期选择` | - | | title | 日历标题 | *string* | `日期选择` | - |
| min-date | 最小日期 | *Date* | 当前日期 | - | | min-date | 最小日期 | *Date* | 当前日期 | - |
| max-date | 最大日期 | *Date* | 当前日期的六个月后 | - | | max-date | 最大日期 | *Date* | 当前日期的六个月后 | - |
| default-date | 默认选中的日期 | *Date \| Date[]* | 今天 | - |
| row-height | 日期所在行的高度 | *number* | `64` | - | | row-height | 日期所在行的高度 | *number* | `64` | - |
| button-text | 确认按钮的文字 | *string* | `确定` | - | | button-text | 确认按钮的文字 | *string* | `确定` | - |
| button-disabled-text | 确认按钮处于禁用状态时的文字 | *string* | `确定` | - | | button-disabled-text | 确认按钮处于禁用状态时的文字 | *string* | `确定` | - |

View File

@ -13,7 +13,7 @@ export default createComponent({
showMark: Boolean, showMark: Boolean,
showTitle: Boolean, showTitle: Boolean,
rowHeight: Number, rowHeight: Number,
currentValue: [Date, Array] currentDate: [Date, Array]
}, },
data() { data() {
@ -76,18 +76,18 @@ export default createComponent({
methods: { methods: {
getDayType(day) { getDayType(day) {
const { type, minDate, maxDate, currentValue } = this; const { type, minDate, maxDate, currentDate } = this;
if (compareDay(day, minDate) < 0 || compareDay(day, maxDate) > 0) { if (compareDay(day, minDate) < 0 || compareDay(day, maxDate) > 0) {
return 'disabled'; return 'disabled';
} }
if (type === 'single') { if (type === 'single') {
return compareDay(day, currentValue) === 0 ? 'selected' : ''; return compareDay(day, currentDate) === 0 ? 'selected' : '';
} }
if (type === 'range') { if (type === 'range') {
const [startDay, endDay] = this.currentValue; const [startDay, endDay] = this.currentDate;
if (!startDay) { if (!startDay) {
return; return;

View File

@ -15,10 +15,7 @@
position="bottom" position="bottom"
style="height: 80vh;" style="height: 80vh;"
> >
<van-calendar <van-calendar @select="onSelect($event, 'selectSingleDate')" />
v-model="date.selectSingleDate"
@select="show.selectSingleDate = false"
/>
</van-popup> </van-popup>
<van-cell <van-cell
@ -36,9 +33,8 @@
style="height: 80vh;" style="height: 80vh;"
> >
<van-calendar <van-calendar
v-model="date.selectDateRange"
type="range" type="range"
@select="show.selectDateRange = false" @select="onSelect($event, 'selectDateRange')"
/> />
</van-popup> </van-popup>
</demo-block> </demo-block>
@ -93,6 +89,11 @@ export default {
const [start, end] = dateRange; const [start, end] = dateRange;
return `${this.formatDate(start)} - ${this.formatDate(end)}`; return `${this.formatDate(start)} - ${this.formatDate(end)}`;
} }
},
onSelect(date, type) {
this.date[type] = date;
this.show[type] = false;
} }
} }
}; };

View File

@ -18,8 +18,8 @@ import Header from './components/Header';
export default createComponent({ export default createComponent({
props: { props: {
title: String, title: String,
value: [Date, Array],
buttonText: String, buttonText: String,
defaultDate: [Date, Array],
buttonDisabledText: String, buttonDisabledText: String,
type: { type: {
type: String, type: String,
@ -27,16 +27,16 @@ export default createComponent({
}, },
minDate: { minDate: {
type: Date, type: Date,
default: () => new Date(), validator: isDate,
validator: isDate default: () => new Date()
}, },
maxDate: { maxDate: {
type: Date, type: Date,
validator: isDate,
default() { default() {
const now = new Date(); const now = new Date();
return new Date(now.getFullYear(), now.getMonth() + 6, now.getDate()); return new Date(now.getFullYear(), now.getMonth() + 6, now.getDate());
}, }
validator: isDate
}, },
rowHeight: { rowHeight: {
type: Number, type: Number,
@ -55,7 +55,7 @@ export default createComponent({
data() { data() {
return { return {
monthTitle: '', monthTitle: '',
currentValue: this.getDefaultValue() currentDate: this.getInitialDate()
}; };
}, },
@ -76,8 +76,8 @@ export default createComponent({
}, },
watch: { watch: {
value(val) { defaultDate(val) {
this.currentValue = val; this.currentDate = val;
} }
}, },
@ -87,16 +87,16 @@ export default createComponent({
}, },
methods: { methods: {
getDefaultValue() { getInitialDate() {
const { type, value, minDate } = this; const { type, defaultDate, minDate } = this;
if (type === 'single') { if (type === 'single') {
return value || minDate; return defaultDate || minDate;
} }
if (type === 'range') { if (type === 'range') {
const range = value || []; const [startDay, endDay] = defaultDate || [];
return [range[0] || minDate, range[1] || getNextDay(minDate)]; return [startDay || minDate, endDay || getNextDay(minDate)];
} }
}, },
@ -132,32 +132,31 @@ export default createComponent({
const { date } = item; const { date } = item;
if (this.type === 'single') { if (this.type === 'single') {
this.$emit('input', date); this.currentDate = date;
this.$emit('select', date); this.$emit('select', date);
} }
if (this.type === 'range') { if (this.type === 'range') {
const [startDay, endDay] = this.currentValue; const [startDay, endDay] = this.currentDate;
if (startDay && !endDay) { if (startDay && !endDay) {
const compareToStart = compareDay(date, startDay); const compareToStart = compareDay(date, startDay);
if (compareToStart === 1) { if (compareToStart === 1) {
this.$emit('input', [startDay, date]); this.currentDate = [startDay, date];
} }
if (compareToStart === -1) { if (compareToStart === -1) {
this.$emit('input', [date, null]); this.currentDate = [date, null];
} }
} else { } else {
this.$emit('input', [date, null]); this.currentDate = [date, null];
} }
} }
}, },
onConfirmRange() { onConfirmRange() {
this.$emit('input', this.currentValue); this.$emit('select', this.currentDate);
this.$emit('select', this.currentValue);
}, },
genMonth(date, index) { genMonth(date, index) {
@ -172,7 +171,7 @@ export default createComponent({
showMark={this.showMark} showMark={this.showMark}
rowHeight={this.rowHeight} rowHeight={this.rowHeight}
showTitle={index !== 0} showTitle={index !== 0}
currentValue={this.currentValue} currentDate={this.currentDate}
onClick={this.onClickDay} onClick={this.onClickDay}
/> />
); );
@ -186,7 +185,7 @@ export default createComponent({
} }
if (this.type === 'range') { if (this.type === 'range') {
const disabled = !this.currentValue[1]; const disabled = !this.currentDate[1];
const text = disabled ? this.buttonDisabledText : this.buttonText; const text = disabled ? this.buttonDisabledText : this.buttonText;
return ( return (