mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
[Doc] DatetimePicker: improve documentation (#747)
This commit is contained in:
parent
4810630215
commit
70fbdea35d
@ -1,6 +1,6 @@
|
|||||||
<template>
|
<template>
|
||||||
<demo-section>
|
<demo-section>
|
||||||
<demo-block :title="$t('basicUsage')">
|
<demo-block :title="$t('title1')">
|
||||||
<van-datetime-picker
|
<van-datetime-picker
|
||||||
v-model="currentDate1"
|
v-model="currentDate1"
|
||||||
type="datetime"
|
type="datetime"
|
||||||
@ -15,8 +15,6 @@
|
|||||||
<van-datetime-picker
|
<van-datetime-picker
|
||||||
v-model="currentDate2"
|
v-model="currentDate2"
|
||||||
type="date"
|
type="date"
|
||||||
:min-hour="minHour"
|
|
||||||
:max-hour="maxHour"
|
|
||||||
:min-date="minDate"
|
:min-date="minDate"
|
||||||
/>
|
/>
|
||||||
</demo-block>
|
</demo-block>
|
||||||
@ -24,8 +22,15 @@
|
|||||||
<demo-block :title="$t('title3')">
|
<demo-block :title="$t('title3')">
|
||||||
<van-datetime-picker
|
<van-datetime-picker
|
||||||
v-model="currentDate3"
|
v-model="currentDate3"
|
||||||
|
type="year-month"
|
||||||
|
:min-date="minDate"
|
||||||
|
/>
|
||||||
|
</demo-block>
|
||||||
|
|
||||||
|
<demo-block :title="$t('title4')">
|
||||||
|
<van-datetime-picker
|
||||||
|
v-model="currentDate4"
|
||||||
type="time"
|
type="time"
|
||||||
:visibleItemCount="3"
|
|
||||||
:min-hour="minHour"
|
:min-hour="minHour"
|
||||||
:max-hour="maxHour"
|
:max-hour="maxHour"
|
||||||
:min-date="minDate"
|
:min-date="minDate"
|
||||||
@ -38,12 +43,16 @@
|
|||||||
export default {
|
export default {
|
||||||
i18n: {
|
i18n: {
|
||||||
'zh-CN': {
|
'zh-CN': {
|
||||||
title2: '选择日期',
|
title1: '选择完整时间',
|
||||||
title3: '选择时间'
|
title2: '选择日期(年月日)',
|
||||||
|
title3: '选择日期(年月)',
|
||||||
|
title4: '选择时间'
|
||||||
},
|
},
|
||||||
'en-US': {
|
'en-US': {
|
||||||
title2: 'Date Picker',
|
title1: 'Choose DateTime',
|
||||||
title3: 'Time Picker'
|
title2: 'Choose Date',
|
||||||
|
title3: 'Choose Year-Month',
|
||||||
|
title4: 'Choose Time'
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -55,7 +64,8 @@ export default {
|
|||||||
maxDate: new Date(2019, 10, 1),
|
maxDate: new Date(2019, 10, 1),
|
||||||
currentDate1: new Date(2018, 0, 1),
|
currentDate1: new Date(2018, 0, 1),
|
||||||
currentDate2: null,
|
currentDate2: null,
|
||||||
currentDate3: '12:00'
|
currentDate3: new Date(2018, 0, 1),
|
||||||
|
currentDate4: '12:00'
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -9,7 +9,7 @@ Vue.use(DatetimePicker);
|
|||||||
|
|
||||||
### Usage
|
### Usage
|
||||||
|
|
||||||
#### Basic Usage
|
#### Choose DateTime
|
||||||
|
|
||||||
```html
|
```html
|
||||||
<van-datetime-picker
|
<van-datetime-picker
|
||||||
@ -30,29 +30,57 @@ export default {
|
|||||||
maxHour: 20,
|
maxHour: 20,
|
||||||
minDate: new Date(),
|
minDate: new Date(),
|
||||||
maxDate: new Date(2019, 10, 1),
|
maxDate: new Date(2019, 10, 1),
|
||||||
currentDate: new Date(2018, 0, 1)
|
currentDate: new Date()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Date Picker
|
#### Choose Date
|
||||||
|
|
||||||
```html
|
```html
|
||||||
<van-datetime-picker
|
<van-datetime-picker
|
||||||
v-model="currentDate"
|
v-model="currentDate"
|
||||||
type="date"
|
type="date"
|
||||||
:min-hour="minHour"
|
|
||||||
:max-hour="maxHour"
|
|
||||||
:min-date="minDate"
|
:min-date="minDate"
|
||||||
/>
|
/>
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Time Picker
|
```js
|
||||||
|
export default {
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
currentDate: new Date()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Choose Year-Month
|
||||||
|
|
||||||
```html
|
```html
|
||||||
<van-datetime-picker
|
<van-datetime-picker
|
||||||
v-model="currentDate3"
|
v-model="currentDate"
|
||||||
|
type="year-month"
|
||||||
|
:min-date="minDate"
|
||||||
|
/>
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
export default {
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
currentDate: new Date()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Choose Time
|
||||||
|
|
||||||
|
```html
|
||||||
|
<van-datetime-picker
|
||||||
|
v-model="currentDate"
|
||||||
type="time"
|
type="time"
|
||||||
:min-hour="minHour"
|
:min-hour="minHour"
|
||||||
:max-hour="maxHour"
|
:max-hour="maxHour"
|
||||||
@ -60,11 +88,21 @@ export default {
|
|||||||
/>
|
/>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
export default {
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
currentDate: '12:00'
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### API
|
### API
|
||||||
|
|
||||||
| Attribute | Description | Type | Default | Accepted Values |
|
| Attribute | Description | Type | Default | Accepted Values |
|
||||||
|-----------|-----------|-----------|-------------|-------------|
|
|-----------|-----------|-----------|-------------|-------------|
|
||||||
| type | Picker type | `String` | 'datetime' | 'date', 'time' |
|
| type | Picker type | `String` | `datetime` | `date` `time` `year-month` |
|
||||||
| min-date | Min date | `Date` | Ten years ago on January 1 | - |
|
| min-date | Min date | `Date` | Ten years ago on January 1 | - |
|
||||||
| max-date | Max date | `Date` | Ten years later on December 31 | - |
|
| max-date | Max date | `Date` | Ten years later on December 31 | - |
|
||||||
| min-hour | Min hour | `Number` | `0` | - |
|
| min-hour | Min hour | `Number` | `0` | - |
|
||||||
|
@ -9,7 +9,7 @@ Vue.use(DatetimePicker);
|
|||||||
|
|
||||||
### 代码演示
|
### 代码演示
|
||||||
|
|
||||||
#### 基础用法
|
#### 选择完整时间
|
||||||
|
|
||||||
```html
|
```html
|
||||||
<van-datetime-picker
|
<van-datetime-picker
|
||||||
@ -30,24 +30,52 @@ export default {
|
|||||||
maxHour: 20,
|
maxHour: 20,
|
||||||
minDate: new Date(),
|
minDate: new Date(),
|
||||||
maxDate: new Date(2019, 10, 1),
|
maxDate: new Date(2019, 10, 1),
|
||||||
currentDate: new Date(2018, 0, 1)
|
currentDate: new Date()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 选择日期
|
#### 选择日期(年月日)
|
||||||
|
|
||||||
```html
|
```html
|
||||||
<van-datetime-picker
|
<van-datetime-picker
|
||||||
v-model="currentDate"
|
v-model="currentDate"
|
||||||
type="date"
|
type="date"
|
||||||
:min-hour="minHour"
|
|
||||||
:max-hour="maxHour"
|
|
||||||
:min-date="minDate"
|
:min-date="minDate"
|
||||||
/>
|
/>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
export default {
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
currentDate: new Date()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 选择日期(年月)
|
||||||
|
|
||||||
|
```html
|
||||||
|
<van-datetime-picker
|
||||||
|
v-model="currentDate"
|
||||||
|
type="year-month"
|
||||||
|
:min-date="minDate"
|
||||||
|
/>
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
export default {
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
currentDate: new Date()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
#### 选择时间
|
#### 选择时间
|
||||||
|
|
||||||
```html
|
```html
|
||||||
@ -60,11 +88,21 @@ export default {
|
|||||||
/>
|
/>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
export default {
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
currentDate: '12:00'
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### API
|
### API
|
||||||
|
|
||||||
| 参数 | 说明 | 类型 | 默认值 | 可选值 |
|
| 参数 | 说明 | 类型 | 默认值 | 可选值 |
|
||||||
|-----------|-----------|-----------|-------------|-------------|
|
|-----------|-----------|-----------|-------------|-------------|
|
||||||
| type | 组件类型 | `String` | 'datetime' | 'date', 'time' |
|
| type | 组件类型 | `String` | `datetime` | `date` `time` `year-month` |
|
||||||
| min-date | 可选的最小日期 | `Date` | 十年前的 1 月 1 日 | - |
|
| min-date | 可选的最小日期 | `Date` | 十年前的 1 月 1 日 | - |
|
||||||
| max-date | 可选的最大日期 | `Date` | 十年后的 12 月 31 日 | - |
|
| max-date | 可选的最大日期 | `Date` | 十年后的 12 月 31 日 | - |
|
||||||
| min-hour | 可选的最小小时 | `Number` | `0` | - |
|
| min-hour | 可选的最小小时 | `Number` | `0` | - |
|
||||||
|
@ -11,9 +11,10 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import create from '../utils/create';
|
|
||||||
import Picker from '../picker';
|
import Picker from '../picker';
|
||||||
|
import create from '../utils/create';
|
||||||
|
|
||||||
|
const currentYear = new Date().getFullYear();
|
||||||
const isValidDate = date => Object.prototype.toString.call(date) === '[object Date]' && !isNaN(date.getTime());
|
const isValidDate = date => Object.prototype.toString.call(date) === '[object Date]' && !isNaN(date.getTime());
|
||||||
|
|
||||||
export default create({
|
export default create({
|
||||||
@ -42,16 +43,12 @@ export default create({
|
|||||||
},
|
},
|
||||||
minDate: {
|
minDate: {
|
||||||
type: Date,
|
type: Date,
|
||||||
default() {
|
default: () => new Date(currentYear - 10, 0, 1),
|
||||||
return new Date(new Date().getFullYear() - 10, 0, 1);
|
|
||||||
},
|
|
||||||
validator: isValidDate
|
validator: isValidDate
|
||||||
},
|
},
|
||||||
maxDate: {
|
maxDate: {
|
||||||
type: Date,
|
type: Date,
|
||||||
default() {
|
default: () => new Date(currentYear + 10, 11, 31),
|
||||||
return new Date(new Date().getFullYear() + 10, 11, 31);
|
|
||||||
},
|
|
||||||
validator: isValidDate
|
validator: isValidDate
|
||||||
},
|
},
|
||||||
minHour: {
|
minHour: {
|
||||||
@ -77,6 +74,7 @@ export default create({
|
|||||||
const isEqual = this.type === 'time' ? val === this.innerValue : val.valueOf() === this.innerValue.valueOf();
|
const isEqual = this.type === 'time' ? val === this.innerValue : val.valueOf() === this.innerValue.valueOf();
|
||||||
if (!isEqual) this.innerValue = val;
|
if (!isEqual) this.innerValue = val;
|
||||||
},
|
},
|
||||||
|
|
||||||
innerValue(val) {
|
innerValue(val) {
|
||||||
this.updateColumnValue(val);
|
this.updateColumnValue(val);
|
||||||
this.$emit('input', val);
|
this.$emit('input', val);
|
||||||
@ -104,7 +102,7 @@ export default create({
|
|||||||
];
|
];
|
||||||
|
|
||||||
if (this.type === 'date') result.splice(3, 2);
|
if (this.type === 'date') result.splice(3, 2);
|
||||||
if (this.type === 'date-year-month') result.splice(2, 3);
|
if (this.type === 'year-month') result.splice(2, 3);
|
||||||
return result;
|
return result;
|
||||||
},
|
},
|
||||||
columns() {
|
columns() {
|
||||||
@ -125,7 +123,7 @@ export default create({
|
|||||||
methods: {
|
methods: {
|
||||||
correctValue(value) {
|
correctValue(value) {
|
||||||
// validate value
|
// validate value
|
||||||
const isDateType = this.type.indexOf('date') > -1;
|
const isDateType = this.type !== 'time';
|
||||||
if (isDateType && !isValidDate(value)) {
|
if (isDateType && !isValidDate(value)) {
|
||||||
value = this.minDate;
|
value = this.minDate;
|
||||||
} else if (!value) {
|
} else if (!value) {
|
||||||
@ -241,7 +239,7 @@ export default create({
|
|||||||
const month = this.getTrueValue(values[1]);
|
const month = this.getTrueValue(values[1]);
|
||||||
const maxDate = this.getMonthEndDay(year, month);
|
const maxDate = this.getMonthEndDay(year, month);
|
||||||
let date = this.getTrueValue(values[2]);
|
let date = this.getTrueValue(values[2]);
|
||||||
if (this.type === 'date-year-month') {
|
if (this.type === 'year-month') {
|
||||||
date = 1;
|
date = 1;
|
||||||
}
|
}
|
||||||
date = date > maxDate ? maxDate : date;
|
date = date > maxDate ? maxDate : date;
|
||||||
@ -259,6 +257,7 @@ export default create({
|
|||||||
},
|
},
|
||||||
|
|
||||||
updateColumnValue(value) {
|
updateColumnValue(value) {
|
||||||
|
console.log(value, this.type);
|
||||||
let values = [];
|
let values = [];
|
||||||
if (this.type === 'time') {
|
if (this.type === 'time') {
|
||||||
const currentValue = value.split(':');
|
const currentValue = value.split(':');
|
||||||
@ -278,7 +277,7 @@ export default create({
|
|||||||
`0${value.getMinutes()}`.slice(-2)
|
`0${value.getMinutes()}`.slice(-2)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (this.type === 'date-year-month') {
|
if (this.type === 'year-month') {
|
||||||
values = values.slice(0, 2);
|
values = values.slice(0, 2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,11 +35,11 @@ describe('DatetimePicker', () => {
|
|||||||
expect(wrapper.vm.innerValue.getTime()).to.equal(testDate.getTime());
|
expect(wrapper.vm.innerValue.getTime()).to.equal(testDate.getTime());
|
||||||
});
|
});
|
||||||
|
|
||||||
it('create a date-year-month', () => {
|
it('create a year-month', () => {
|
||||||
wrapper = mount(DatetimePicker, {
|
wrapper = mount(DatetimePicker, {
|
||||||
attachToDocument: true,
|
attachToDocument: true,
|
||||||
propsData: {
|
propsData: {
|
||||||
type: 'date-year-month',
|
type: 'year-month',
|
||||||
value: testDate
|
value: testDate
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -104,11 +104,11 @@ describe('DatetimePicker', () => {
|
|||||||
}, 10);
|
}, 10);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('drag date-year-month picker', (done) => {
|
it('drag year-month picker', (done) => {
|
||||||
wrapper = mount(DatetimePicker, {
|
wrapper = mount(DatetimePicker, {
|
||||||
attachToDocument: true,
|
attachToDocument: true,
|
||||||
propsData: {
|
propsData: {
|
||||||
type: 'date-year-month',
|
type: 'year-month',
|
||||||
value: testDate,
|
value: testDate,
|
||||||
minDate,
|
minDate,
|
||||||
maxDate
|
maxDate
|
||||||
|
Loading…
x
Reference in New Issue
Block a user