emis-sapp/components/tpx-date-input/tpx-date-input.vue

186 lines
4.1 KiB
Vue
Raw Normal View History

2024-01-22 01:55:30 +00:00
<template>
<view @click="handleSelectDate" class="pos-rlt">
<u-input :placeholder="placeholder" :fontSize="fontSize" :modelValue="inputValue"
2024-01-29 10:48:55 +00:00
:custom-style="`border-radius: 10rpx;padding: 11rpx 20rpx;${customStyle};`"
2024-01-22 01:55:30 +00:00
readonly="true"
>
<template v-slot:suffix>
<u-icon name="calendar" :size="fontSize*1.5"></u-icon>
</template>
</u-input>
2024-01-27 09:02:38 +00:00
<view class="pos-abt-all">
2024-06-20 11:11:43 +00:00
<view v-if="showRangeTools" @click.stop="">
2024-05-31 10:42:15 +00:00
<tpx-select :list="toolsRangesList" v-model:value="rangeVal" mode="drop" @onChange="handleToolsRangChange">
2024-01-27 09:56:52 +00:00
<view :style="`height: 64rpx;`" ></view>
</tpx-select>
</view>
2024-01-27 09:02:38 +00:00
</view>
2024-01-22 01:55:30 +00:00
</view>
<!-- 日历 -->
2024-05-31 10:42:15 +00:00
<view v-if="showCladar">
2024-06-20 11:11:43 +00:00
<uni-datetime-picker ref="unDateTimePckRef" @maskClick="handleClaClose"
:start="minDate" :end="maxDate" v-model="proxyValue"
:type="mode" @change="handleClaValChange"
:placeholder="placeholder"
>
<!-- 使用自定义显示 -->
<view></view>
</uni-datetime-picker>
2024-05-31 10:42:15 +00:00
</view>
2024-01-22 01:55:30 +00:00
</template>
<script>
2024-06-20 11:11:43 +00:00
// uni-datetime-picker 插件地址: https://ext.dcloud.net.cn/plugin?id=3962
2024-01-27 09:02:38 +00:00
import dayjs from "dayjs"
2024-05-31 10:42:15 +00:00
const formatStr = 'YYYY-MM-DD'
2024-01-27 09:02:38 +00:00
2024-01-22 01:55:30 +00:00
export default {
computed: {
2024-06-20 11:11:43 +00:00
proxyValue: {
get() {
return this.value
}
},
showRangeTools(){
return this.mode?.indexOf("range") > -1 && this.showTools
},
2024-01-22 01:55:30 +00:00
inputValue() {
2024-05-31 10:42:15 +00:00
let val = this.value
if(Array.isArray(val)) {
val = [].concat(val)
if(val.length > 1) {
val[0] = dayjs(val[0]).format(formatStr)
}
if(val.length > 1) {
val[1] = dayjs(val[1]).format(formatStr)
}
}
2024-06-20 11:11:43 +00:00
// 范围是数组,单个是字符串
if(this.mode?.indexOf("range") > -1) {
val = val && (val.length > 1 ? val.join("~") : val[0])
}
return val
2024-05-31 10:42:15 +00:00
},
2024-01-22 01:55:30 +00:00
},
props: {
value: {
default () {
return ''
}
},
placeholder:{
default () {
return '请选择日期'
}
},
fontSize: {
default () {
2024-06-20 11:11:43 +00:00
return 28
2024-01-22 01:55:30 +00:00
}
},
2024-05-31 10:42:15 +00:00
minDate: {
default() {
return dayjs().subtract(12, 'months').format(`${formatStr}`)
}
},
2024-06-20 11:11:43 +00:00
maxDate: {
2024-05-31 10:42:15 +00:00
default() {
2024-06-20 11:11:43 +00:00
return dayjs().add(12, 'months').format(`${formatStr}`)
2024-05-31 10:42:15 +00:00
}
},
mode: {
default() {
2024-06-20 11:11:43 +00:00
return 'daterange' // datetime | date | datetimerange | daterange
2024-05-31 10:42:15 +00:00
}
},
2024-01-22 01:55:30 +00:00
customStyle:{
default () {
return ''
}
},
2024-01-27 09:02:38 +00:00
showTools:{
default () {
return true
}
},
toolsRangesList: {
default () {
2024-05-31 10:42:15 +00:00
let today = dayjs().format(`${formatStr}`),
yesterday = dayjs().subtract(1, 'days').format(`${formatStr}`),
lastWeek = dayjs().subtract(7, 'days').format(`${formatStr}`)
2024-01-27 09:02:38 +00:00
return [
2024-05-31 10:42:15 +00:00
{ title: '今日', val: '1', range: [ today, today ] },
{ title: '昨日', val: '2', range: [yesterday, yesterday] },
{ title: '最近一周', val: '3', range: [lastWeek, today] },
{ title: '自定义', val: '-1', range: [] },
2024-01-27 09:02:38 +00:00
]
}
},
2024-01-22 01:55:30 +00:00
placeholderStyle:{
default () {
return ''
}
},
},
data() {
return {
2024-05-31 10:42:15 +00:00
showCladar: false,
rangeVal: ''
2024-01-22 01:55:30 +00:00
}
},
created() {
},
onHide() {
},
2024-01-30 03:09:49 +00:00
unmounted() {},
2024-01-22 01:55:30 +00:00
methods: {
handleSelectDate() {
2024-08-10 15:38:29 +00:00
this.handleClaShow()
2024-01-22 01:55:30 +00:00
},
handleChange(val){
2024-06-20 11:11:43 +00:00
if(Array.isArray(val)) {
// 补全 时间戳
if(val[0].indexOf(":") == -1) {
val[0] = dayjs(val[0]).format(`${formatStr} 00:00:00`)
}
if(val[1].indexOf(":") == -1) {
val[1] = dayjs(val[1]).format(`${formatStr} 23:59:59`)
}
}
2024-01-22 01:55:30 +00:00
this.$emit('change', val)
this.$emit('update:value', val)
},
2024-06-20 11:11:43 +00:00
handleClaValChange(val) {
2024-01-22 01:55:30 +00:00
this.handleChange(val)
2024-08-10 15:38:29 +00:00
this.handleClaClose()
2024-06-20 11:11:43 +00:00
},
handleClaClose(res){
2024-08-10 15:38:29 +00:00
this.showCladar = false
2024-01-22 01:55:30 +00:00
},
2024-08-10 15:38:29 +00:00
handleClaShow() {
this.showCladar = true
2024-06-20 11:11:43 +00:00
// 手动唤起日期事件
setTimeout(()=> {
2024-08-10 15:38:29 +00:00
this.$refs.unDateTimePckRef.show()
}, 1000)
2024-01-27 09:02:38 +00:00
},
handleToolsRangChange(val) {
2024-07-30 03:10:52 +00:00
this.rangeVal = val
2024-01-27 09:02:38 +00:00
if(val == -1) {
2024-08-10 15:38:29 +00:00
this.handleClaShow()
2024-01-27 09:02:38 +00:00
} else {
2024-05-31 10:42:15 +00:00
let sltRang = this.toolsRangesList.filter(t=> t.val == val)[0].range
2024-06-20 11:11:43 +00:00
this.handleChange(sltRang, )
2024-01-27 09:02:38 +00:00
}
2024-01-22 01:55:30 +00:00
}
}
}
</script>
<style scoped lang="scss">
</style>