forked from youzan/vant
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimePicker.js
More file actions
105 lines (89 loc) · 2.29 KB
/
TimePicker.js
File metadata and controls
105 lines (89 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { createNamespace } from '../utils';
import { padZero } from '../utils/format/string';
import { range } from '../utils/format/number';
import { sharedProps, TimePickerMixin } from './shared';
const [createComponent] = createNamespace('time-picker');
export default createComponent({
mixins: [TimePickerMixin],
props: {
...sharedProps,
minHour: {
type: Number,
default: 0
},
maxHour: {
type: Number,
default: 23
},
minMinute: {
type: Number,
default: 0
},
maxMinute: {
type: Number,
default: 59
}
},
computed: {
ranges() {
return [
{
type: 'hour',
range: [this.minHour, this.maxHour]
},
{
type: 'minute',
range: [this.minMinute, this.maxMinute]
}
];
}
},
watch: {
filter: 'updateInnerValue',
minHour: 'updateInnerValue',
maxHour: 'updateInnerValue',
minMinute: 'updateInnerValue',
maxMinute: 'updateInnerValue',
value(val) {
val = this.formatValue(val);
if (val !== this.innerValue) {
this.innerValue = val;
this.updateColumnValue(val);
}
}
},
methods: {
formatValue(value) {
if (!value) {
value = `${padZero(this.minHour)}:${padZero(this.minMinute)}`;
}
let [hour, minute] = value.split(':');
hour = padZero(range(hour, this.minHour, this.maxHour));
minute = padZero(range(minute, this.minMinute, this.maxMinute));
return `${hour}:${minute}`;
},
updateInnerValue() {
const indexes = this.$refs.picker.getIndexes();
const hour = this.originColumns[0].values[indexes[0]];
const minute = this.originColumns[1].values[indexes[1]];
const value = `${hour}:${minute}`;
this.innerValue = this.formatValue(value);
},
onChange(picker) {
this.updateInnerValue();
this.$nextTick(() => {
this.$nextTick(() => {
this.$emit('change', picker);
});
});
},
updateColumnValue() {
const { formatter } = this;
const pair = this.innerValue.split(':');
const values = [formatter('hour', pair[0]), formatter('minute', pair[1])];
this.$nextTick(() => {
this.$refs.picker.setValues(values);
});
}
}
});