Blame view

components/ncc/ncc-date-range/index.vue 1.6 KB
290144e9   易尊强   第一次
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
  <template>
  	<view class="ncc-date-range">
  		<u-input type="select" :select-open="selectShow" v-model="innerValue" :placeholder="placeholder"
  			@click="openSelect"></u-input>
  		<u-calendar v-model="selectShow" ref="calendar" @change="change" mode="range" max-date="2051-01-01"
  			:defaultTime="defaultTime"></u-calendar>
  	</view>
  </template>
  
  <script>
  	export default {
  		name: 'ncc-date-range',
  		model: {
  			prop: 'value',
  			event: 'input'
  		},
  		props: {
  			value: {
  				type: Array,
  				default: () => []
  			},
  			placeholder: {
  				type: String,
  				default: '请选择日期范围'
  			},
  			disabled: {
  				type: Boolean,
  				default: false
  			}
  		},
  		data() {
  			return {
  				defaultTime: [],
  				selectShow: false,
  				innerValue: '',
  			}
  		},
  		watch: {
  			value(val) {
  				this.innerValue = ''
  				this.setDefault()
  			}
  		},
  		created() {
  			this.setDefault()
  		},
  		methods: {
  			setDefault() {
  				if (this.value.length === 2) {
  					this.defaultTime = this.value
  					this.innerValue = this.$u.timeFormat(this.value[0], 'yyyy-mm-dd') + '至' +
  						this.$u.timeFormat(this.value[1], 'yyyy-mm-dd')
  				} else {
  					this.innerValue = ''
  					this.defaultTime = []
  				}
  			},
  			openSelect() {
  				if (this.disabled) return
  				this.selectShow = true
  			},
  			change(e) {
  				const startDate = Date.parse(e.startDate + ' 00:00:00')
  				const endDate = Date.parse(e.endDate + ' 00:00:00')
  				// #ifdef MP-WEIXIN
  				this.innerValue = e.startDate + '至' + e.endDate
  				// #endif
  				this.$emit('input', [startDate, endDate])
  			}
  		}
  	}
  </script>
  <style lang="scss" scoped>
  	.ncc-date-range {
  		width: 100%;
  	}
  </style>