mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
[improvement] DatetimePicker: optimize static methods (#2395)
This commit is contained in:
parent
b1044c6739
commit
b81dff92fe
@ -26,6 +26,10 @@ import create from '../utils/create';
|
|||||||
import { raf, cancel } from '../utils/raf';
|
import { raf, cancel } from '../utils/raf';
|
||||||
import { BLUE, WHITE } from '../utils/color';
|
import { BLUE, WHITE } from '../utils/color';
|
||||||
|
|
||||||
|
function format(rate) {
|
||||||
|
return Math.min(Math.max(rate, 0), 100);
|
||||||
|
}
|
||||||
|
|
||||||
export default create({
|
export default create({
|
||||||
name: 'circle',
|
name: 'circle',
|
||||||
|
|
||||||
@ -100,7 +104,7 @@ export default create({
|
|||||||
handler() {
|
handler() {
|
||||||
this.startTime = Date.now();
|
this.startTime = Date.now();
|
||||||
this.startRate = this.value;
|
this.startRate = this.value;
|
||||||
this.endRate = this.format(this.rate);
|
this.endRate = format(this.rate);
|
||||||
this.increase = this.endRate > this.startRate;
|
this.increase = this.endRate > this.startRate;
|
||||||
this.duration = Math.abs((this.startRate - this.endRate) * 1000 / this.speed);
|
this.duration = Math.abs((this.startRate - this.endRate) * 1000 / this.speed);
|
||||||
if (this.speed) {
|
if (this.speed) {
|
||||||
@ -119,14 +123,10 @@ export default create({
|
|||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
const progress = Math.min((now - this.startTime) / this.duration, 1);
|
const progress = Math.min((now - this.startTime) / this.duration, 1);
|
||||||
const rate = progress * (this.endRate - this.startRate) + this.startRate;
|
const rate = progress * (this.endRate - this.startRate) + this.startRate;
|
||||||
this.$emit('input', this.format(parseFloat(rate.toFixed(1))));
|
this.$emit('input', format(parseFloat(rate.toFixed(1))));
|
||||||
if (this.increase ? rate < this.endRate : rate > this.endRate) {
|
if (this.increase ? rate < this.endRate : rate > this.endRate) {
|
||||||
this.rafId = raf(this.animate);
|
this.rafId = raf(this.animate);
|
||||||
}
|
}
|
||||||
},
|
|
||||||
|
|
||||||
format(rate) {
|
|
||||||
return Math.min(Math.max(rate, 0), 100);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -27,6 +27,7 @@ export default create({
|
|||||||
gutter() {
|
gutter() {
|
||||||
return (this.$parent && Number(this.$parent.gutter)) || 0;
|
return (this.$parent && Number(this.$parent.gutter)) || 0;
|
||||||
},
|
},
|
||||||
|
|
||||||
style() {
|
style() {
|
||||||
const padding = `${this.gutter / 2}px`;
|
const padding = `${this.gutter / 2}px`;
|
||||||
return this.gutter ? { paddingLeft: padding, paddingRight: padding } : {};
|
return this.gutter ? { paddingLeft: padding, paddingRight: padding } : {};
|
||||||
|
@ -21,7 +21,36 @@ import { range } from '../utils';
|
|||||||
import PickerMixin from '../mixins/picker';
|
import PickerMixin from '../mixins/picker';
|
||||||
|
|
||||||
const currentYear = new Date().getFullYear();
|
const currentYear = new Date().getFullYear();
|
||||||
const isValidDate = date => Object.prototype.toString.call(date) === '[object Date]' && !isNaN(date.getTime());
|
|
||||||
|
function isValidDate(date) {
|
||||||
|
return Object.prototype.toString.call(date) === '[object Date]' && !isNaN(date.getTime());
|
||||||
|
}
|
||||||
|
|
||||||
|
function padZero(val) {
|
||||||
|
return `00${val}`.slice(-2);
|
||||||
|
}
|
||||||
|
|
||||||
|
function times(n, iteratee) {
|
||||||
|
let index = -1;
|
||||||
|
const result = Array(n);
|
||||||
|
|
||||||
|
while (++index < n) {
|
||||||
|
result[index] = iteratee(index);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getTrueValue(formattedValue) {
|
||||||
|
if (!formattedValue) return;
|
||||||
|
while (isNaN(parseInt(formattedValue, 10))) {
|
||||||
|
formattedValue = formattedValue.slice(1);
|
||||||
|
}
|
||||||
|
return parseInt(formattedValue, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getMonthEndDay(year, month) {
|
||||||
|
return 32 - new Date(year, month - 1, 32).getDate();
|
||||||
|
}
|
||||||
|
|
||||||
export default create({
|
export default create({
|
||||||
name: 'datetime-picker',
|
name: 'datetime-picker',
|
||||||
@ -142,7 +171,7 @@ export default create({
|
|||||||
|
|
||||||
columns() {
|
columns() {
|
||||||
const results = this.ranges.map(({ type, range: rangeArr }) => {
|
const results = this.ranges.map(({ type, range: rangeArr }) => {
|
||||||
const values = this.times(rangeArr[1] - rangeArr[0] + 1, index => {
|
const values = times(rangeArr[1] - rangeArr[0] + 1, index => {
|
||||||
let value = rangeArr[0] + index;
|
let value = rangeArr[0] + index;
|
||||||
value = value < 10 ? `0${value}` : `${value}`;
|
value = value < 10 ? `0${value}` : `${value}`;
|
||||||
return this.formatter(type, value);
|
return this.formatter(type, value);
|
||||||
@ -157,11 +186,11 @@ export default create({
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
methods: {
|
mounted() {
|
||||||
pad(val) {
|
this.updateColumnValue(this.innerValue);
|
||||||
return `00${val}`.slice(-2);
|
},
|
||||||
},
|
|
||||||
|
|
||||||
|
methods: {
|
||||||
correctValue(value) {
|
correctValue(value) {
|
||||||
// validate value
|
// validate value
|
||||||
const isDateType = this.type !== 'time';
|
const isDateType = this.type !== 'time';
|
||||||
@ -175,8 +204,8 @@ export default create({
|
|||||||
// time type
|
// time type
|
||||||
if (!isDateType) {
|
if (!isDateType) {
|
||||||
let [hour, minute] = value.split(':');
|
let [hour, minute] = value.split(':');
|
||||||
hour = this.pad(range(hour, this.minHour, this.maxHour));
|
hour = padZero(range(hour, this.minHour, this.maxHour));
|
||||||
minute = this.pad(range(minute, this.minMinute, this.maxMinute));
|
minute = padZero(range(minute, this.minMinute, this.maxMinute));
|
||||||
|
|
||||||
return `${hour}:${minute}`;
|
return `${hour}:${minute}`;
|
||||||
}
|
}
|
||||||
@ -188,16 +217,6 @@ export default create({
|
|||||||
return new Date(value);
|
return new Date(value);
|
||||||
},
|
},
|
||||||
|
|
||||||
times(n, iteratee) {
|
|
||||||
let index = -1;
|
|
||||||
const result = Array(n);
|
|
||||||
|
|
||||||
while (++index < n) {
|
|
||||||
result[index] = iteratee(index);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
},
|
|
||||||
|
|
||||||
getBoundary(type, value) {
|
getBoundary(type, value) {
|
||||||
const boundary = this[`${type}Date`];
|
const boundary = this[`${type}Date`];
|
||||||
const year = boundary.getFullYear();
|
const year = boundary.getFullYear();
|
||||||
@ -208,7 +227,7 @@ export default create({
|
|||||||
|
|
||||||
if (type === 'max') {
|
if (type === 'max') {
|
||||||
month = 12;
|
month = 12;
|
||||||
date = this.getMonthEndDay(value.getFullYear(), value.getMonth() + 1);
|
date = getMonthEndDay(value.getFullYear(), value.getMonth() + 1);
|
||||||
hour = 23;
|
hour = 23;
|
||||||
minute = 59;
|
minute = 59;
|
||||||
}
|
}
|
||||||
@ -235,18 +254,6 @@ export default create({
|
|||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
getTrueValue(formattedValue) {
|
|
||||||
if (!formattedValue) return;
|
|
||||||
while (isNaN(parseInt(formattedValue, 10))) {
|
|
||||||
formattedValue = formattedValue.slice(1);
|
|
||||||
}
|
|
||||||
return parseInt(formattedValue, 10);
|
|
||||||
},
|
|
||||||
|
|
||||||
getMonthEndDay(year, month) {
|
|
||||||
return 32 - new Date(year, month - 1, 32).getDate();
|
|
||||||
},
|
|
||||||
|
|
||||||
onConfirm() {
|
onConfirm() {
|
||||||
this.$emit('confirm', this.innerValue);
|
this.$emit('confirm', this.innerValue);
|
||||||
},
|
},
|
||||||
@ -259,10 +266,10 @@ export default create({
|
|||||||
value = `${indexes[0] + this.minHour}:${indexes[1] + this.minMinute}`;
|
value = `${indexes[0] + this.minHour}:${indexes[1] + this.minMinute}`;
|
||||||
} else {
|
} else {
|
||||||
const values = picker.getValues();
|
const values = picker.getValues();
|
||||||
const year = this.getTrueValue(values[0]);
|
const year = getTrueValue(values[0]);
|
||||||
const month = this.getTrueValue(values[1]);
|
const month = getTrueValue(values[1]);
|
||||||
const maxDate = this.getMonthEndDay(year, month);
|
const maxDate = getMonthEndDay(year, month);
|
||||||
let date = this.getTrueValue(values[2]);
|
let date = getTrueValue(values[2]);
|
||||||
if (this.type === 'year-month') {
|
if (this.type === 'year-month') {
|
||||||
date = 1;
|
date = 1;
|
||||||
}
|
}
|
||||||
@ -270,8 +277,8 @@ export default create({
|
|||||||
let hour = 0;
|
let hour = 0;
|
||||||
let minute = 0;
|
let minute = 0;
|
||||||
if (this.type === 'datetime') {
|
if (this.type === 'datetime') {
|
||||||
hour = this.getTrueValue(values[3]);
|
hour = getTrueValue(values[3]);
|
||||||
minute = this.getTrueValue(values[4]);
|
minute = getTrueValue(values[4]);
|
||||||
}
|
}
|
||||||
|
|
||||||
value = new Date(year, month - 1, date, hour, minute);
|
value = new Date(year, month - 1, date, hour, minute);
|
||||||
@ -288,7 +295,7 @@ export default create({
|
|||||||
|
|
||||||
updateColumnValue(value) {
|
updateColumnValue(value) {
|
||||||
let values = [];
|
let values = [];
|
||||||
const { formatter, pad } = this;
|
const { formatter } = this;
|
||||||
|
|
||||||
if (this.type === 'time') {
|
if (this.type === 'time') {
|
||||||
const pair = value.split(':');
|
const pair = value.split(':');
|
||||||
@ -299,13 +306,13 @@ export default create({
|
|||||||
} else {
|
} else {
|
||||||
values = [
|
values = [
|
||||||
formatter('year', `${value.getFullYear()}`),
|
formatter('year', `${value.getFullYear()}`),
|
||||||
formatter('month', pad(value.getMonth() + 1)),
|
formatter('month', padZero(value.getMonth() + 1)),
|
||||||
formatter('day', pad(value.getDate()))
|
formatter('day', padZero(value.getDate()))
|
||||||
];
|
];
|
||||||
if (this.type === 'datetime') {
|
if (this.type === 'datetime') {
|
||||||
values.push(
|
values.push(
|
||||||
formatter('hour', pad(value.getHours())),
|
formatter('hour', padZero(value.getHours())),
|
||||||
formatter('minute', pad(value.getMinutes()))
|
formatter('minute', padZero(value.getMinutes()))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (this.type === 'year-month') {
|
if (this.type === 'year-month') {
|
||||||
@ -317,10 +324,6 @@ export default create({
|
|||||||
this.$refs.picker.setValues(values);
|
this.$refs.picker.setValues(values);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
|
||||||
|
|
||||||
mounted() {
|
|
||||||
this.updateColumnValue(this.innerValue);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user