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

155 lines
3.4 KiB
Vue
Raw Normal View History

2023-12-30 15:28:01 +00:00
<template>
<view @click="handleSelectDate" class="pos-rlt">
<u-input :placeholder="placeholder" :fontSize="fontSize" :modelValue="inputValue"
2024-01-29 10:05:13 +00:00
:custom-style="`border-radius: 10rpx;padding: 11rpx 20rpx;${customStyle};`"
2023-12-30 15:28:01 +00:00
readonly="true"
>
<template v-slot:suffix>
<u-icon name="calendar" :size="fontSize*1.5"></u-icon>
</template>
</u-input>
2024-01-27 08:55:59 +00:00
<view class="pos-abt-all">
2024-01-27 09:57:30 +00:00
<view v-if="showTools" @click.stop="">
2024-05-29 06:33:02 +00:00
<tpx-select :list="toolsRangesList" v-model:value="rangeVal" mode="drop" @onChange="handleToolsRangChange">
2024-01-27 09:57:30 +00:00
<view :style="`height: 64rpx;`" ></view>
</tpx-select>
</view>
2024-01-27 08:55:59 +00:00
</view>
2023-12-30 15:28:01 +00:00
</view>
<!-- 日历 -->
2024-05-29 07:02:36 +00:00
<view v-if="showCladar">
<u-calendar title="请选择日期范围" :minDate="minDate" :monthNum="monthNum" color="#B12D29" :mode="mode" rowHeight="90" closeOnClickOverlay :show="showCladar" :defaultDate="value" @confirm="handleClaOk" @close="handleSwitchCladar"
></u-calendar>
</view>
2023-12-30 15:28:01 +00:00
</template>
<script>
import { } from "@/common/untool"
2024-01-27 08:55:59 +00:00
import dayjs from "dayjs"
2024-05-29 06:33:02 +00:00
const formatStr = 'YYYY-MM-DD'
2024-01-27 08:55:59 +00:00
2023-12-30 15:28:01 +00:00
export default {
computed: {
inputValue() {
2024-05-29 06:33:02 +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)
}
}
2023-12-30 15:28:01 +00:00
return val && (val.length > 1 ? val.join("~") : val[0])
2024-05-29 06:33:02 +00:00
},
2023-12-30 15:28:01 +00:00
},
props: {
value: {
default () {
return ''
}
},
placeholder:{
default () {
return '请选择日期'
}
},
fontSize: {
default () {
return 26
}
},
2024-05-29 06:54:35 +00:00
minDate: {
default() {
return dayjs().subtract(12, 'months').format(`${formatStr}`)
}
},
monthNum: {
default() {
return 24
}
},
mode: {
default() {
return 'range' // single | multiple | range
}
},
2023-12-30 15:28:01 +00:00
customStyle:{
default () {
return ''
}
},
2024-01-27 08:55:59 +00:00
showTools:{
default () {
return true
}
},
toolsRangesList: {
default () {
2024-05-29 06:33:02 +00:00
let today = dayjs().format(`${formatStr}`),
yesterday = dayjs().subtract(1, 'days').format(`${formatStr}`),
lastWeek = dayjs().subtract(7, 'days').format(`${formatStr}`)
2024-01-27 08:55:59 +00:00
return [
2024-05-29 06:33:02 +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 08:55:59 +00:00
]
}
},
2023-12-30 15:28:01 +00:00
placeholderStyle:{
default () {
return ''
}
},
},
data() {
return {
2024-05-29 06:33:02 +00:00
showCladar: false,
rangeVal: ''
2023-12-30 15:28:01 +00:00
}
},
created() {
},
onHide() {
},
2024-01-30 03:09:14 +00:00
unmounted() {},
2023-12-30 15:28:01 +00:00
methods: {
handleSelectDate() {
2024-01-27 09:57:30 +00:00
this.handleSwitchCladar()
2023-12-30 15:28:01 +00:00
},
handleChange(val){
2024-05-29 06:33:02 +00:00
val[0] = dayjs(val[0]).format(`${formatStr} 00:00:00`)
val[1] = dayjs(val[1]).format(`${formatStr} 23:59:59`)
2023-12-30 15:28:01 +00:00
this.$emit('change', val)
this.$emit('update:value', val)
},
handleClaOk(res){
const ary = Object.values(res)
2023-12-30 16:12:42 +00:00
const val = [ary[0], ary[ary.length-1]]
this.handleChange(val)
2023-12-30 15:28:01 +00:00
this.handleSwitchCladar()
},
handleSwitchCladar() {
this.showCladar = !this.showCladar
2024-01-27 08:55:59 +00:00
},
handleToolsRangChange(val) {
if(val == -1) {
this.handleSwitchCladar()
} else {
2024-05-29 06:33:02 +00:00
let sltRang = this.toolsRangesList.filter(t=> t.val == val)[0].range
this.handleChange(sltRang)
2024-01-27 08:55:59 +00:00
}
2023-12-30 15:28:01 +00:00
}
}
}
</script>
<style scoped lang="scss">
</style>