date-pick.vue
3.46 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<template>
<CInput ref="label" :label="label">
<slot>
<div class="wrapper">
<div style="width: 67%; display: inline-block;">
<div>
<input ref="start" type="text" readonly="true" class="input-wrapper" :value="value.start_time" style="width: 40%;" placeholder="选择开始时间" @focus="onFocusStartTime" @blur="validate">
<span style="display: inline-block; width: 5%; color: #999999">-</span>
<input ref="end" type="text" readonly="true" class="input-wrapper" :value="value.expire_time" style="width: 40%;" placeholder="选择结束时间" @focus="onFocusEndTime" @blur="validate">
</div>
</div>
<div style="display: inline-block;">
<Radio :label="{text: '无固定期限', value: true}" v-model="noLimitTime" :active="true"></Radio>
</div>
</div>
</slot>
</CInput>
</template>
<script>
import Input from './input';
import Radio from './radio';
import {DatePicker} from 'cube-ui';
export default {
name: 'CDatePick',
props: {
label: {
type: String
},
value: {
type: Object,
default() {
return {start_time: '', expire_time: '', noLimit: false};
}
}
},
data() {
return {
noLimitTime: this.value.noLimit
};
},
methods: {
onFocusStartTime() {
if (!this.startDatePicker) {
this.startDatePicker = this.$createDatePicker({
title: '选择开始时间',
min: new Date(1990, 7, 8),
max: new Date(2050, 9, 20),
value: this.value.start_time ? new Date(...this.value.start_time.split('/')) : new Date(),
onSelect: this.selectStartTimeHandle,
onCancel: null
});
}
this.startDatePicker.show();
},
selectStartTimeHandle(date, selectedVal) {
this.$refs.start.value = selectedVal.join('/');
this.updateTime();
this.validate();
},
onFocusEndTime() {
if (this.noLimitTime) {
this.noLimitTime = false;
}
this.endDatePicker = this.$createDatePicker({
title: '选择结束时间',
min: this.value.start_time ? new Date(...this.value.start_time.split('/')) : new Date(1990, 7, 8),
max: new Date(2050, 9, 20),
value: this.value.expire_time ? new Date(...this.value.expire_time.split('/')) : new Date(),
onSelect: this.selectEndTimeHandle,
onCancel: null
});
this.endDatePicker.show();
},
selectEndTimeHandle(data, selectedVal) {
this.$refs.end.value = selectedVal.join('/');
this.updateTime();
this.validate();
},
updateTime() {
this.$emit('input', {
start_time: this.$refs.start.value,
expire_time: this.$refs.end.value,
noLimit: this.noLimitTime
});
},
validate() {
if (this.$refs.start.value && (this.$refs.end.value || this.noLimitTime)) {
this.$refs.label.setShowError(false);
return true;
} else {
this.$refs.label.setShowError();
return false;
}
}
},
watch: {
noLimitTime(newVal) {
this.$refs.end.value = '';
this.updateTime();
this.validate();
}
},
components: {
CInput: Input,
DatePicker,
Radio
}
};
</script>
<style lang="scss">
.wrapper {
width: 100%;
font-size: 28px;
}
.input-wrapper {
display: inline-block;
line-height: normal;
}
.cube-picker-panel {
/deep/ .cube-picker-confirm {
color: black;
}
}
</style>