ag-element-ui/packages/agDatePicker/src/index.vue

297 lines
7.6 KiB
Vue
Raw Normal View History

2024-07-05 15:42:19 +08:00
<template>
<el-date-picker
2024-07-08 13:52:52 +08:00
:style="{ width }"
2024-07-08 17:19:16 +08:00
:prefix-icon="iconClass"
2024-07-09 10:36:44 +08:00
clear-icon="ag-icon-clear"
2024-09-09 15:13:37 +08:00
ref="datepickerref"
2024-07-05 15:42:19 +08:00
v-model="dateArr"
v-bind="attrs"
2024-07-13 18:10:18 +08:00
v-on="Listeners"
2024-09-06 18:16:54 +08:00
:value="value"
2024-08-23 17:06:01 +08:00
:type="datetype"
2024-07-08 17:19:16 +08:00
@mouseenter.native="mousetrue = true"
@mouseleave.native="mousetrue = false"
2024-07-05 15:42:19 +08:00
/>
</template>
<script>
/**
* agDatePicker 时间选择器
*/
import isEmpty from "../../../src/utils/isEmpty";
export default {
name: "agDatePicker",
props: {
2024-08-23 17:06:01 +08:00
range:false,
showTime:false,
type: {
type: String,
default: "date",
},
2024-07-05 15:42:19 +08:00
value: {
2024-09-06 18:16:54 +08:00
default:()=>{
return null||[]
},
2024-07-05 15:42:19 +08:00
},
},
data() {
return {
2024-07-08 13:52:52 +08:00
width: "160px",
2024-08-23 17:06:01 +08:00
dateArr: null||[],
2024-07-08 17:19:16 +08:00
date_picker: false,
2024-08-23 17:20:23 +08:00
mousetrue: false,
2024-07-05 15:42:19 +08:00
};
},
computed: {
2024-08-23 17:06:01 +08:00
datetype(){
if(this.range){
if(this.showTime){
return "datetimerange"
}else{
return "daterange";
}
}else{
if(this.showTime){
return "datetime";
}else{
return "date";
}
}
},
2024-07-05 15:42:19 +08:00
attrs() {
2024-07-13 18:10:18 +08:00
let config = {};
2024-08-23 17:06:01 +08:00
if (this.range) {
2024-07-13 18:10:18 +08:00
config = {
2024-09-09 14:27:02 +08:00
"start-placeholder":this.$attrs.startPlaceholder||"开始日期",
"end-placeholder":this.$attrs.endPlaceholder|| "结束日期",
"range-separator":this.$attrs.rangeSeparator||"-",
2024-07-13 18:10:18 +08:00
"picker-options": {
disabledDate(time) {
return (
time.getTime() >
new Date(new Date().toLocaleDateString()).getTime() +
2024-07-05 15:42:19 +08:00
24 * 60 * 60 * 1000 -
2024-07-13 18:10:18 +08:00
1
);
2024-07-05 15:42:19 +08:00
},
2024-07-13 18:10:18 +08:00
shortcuts: [
{
text: "本月",
onClick(picker) {
const end =
new Date(new Date().toLocaleDateString()).getTime() +
24 * 60 * 60 * 1000 -
1;
const time = new Date();
time.setDate(1);
time.setHours(0);
time.setSeconds(0);
time.setMinutes(0);
var start = time.getTime();
picker.$emit("pick", [start, end]);
},
2024-07-05 15:42:19 +08:00
},
2024-07-13 18:10:18 +08:00
{
text: "近一个月",
onClick(picker) {
const end = new Date();
const start = new Date();
start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
picker.$emit("pick", [start, end]);
},
2024-07-05 15:42:19 +08:00
},
2024-07-13 18:10:18 +08:00
{
text: "近三个月",
onClick(picker) {
const end = new Date();
const start = new Date();
start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
picker.$emit("pick", [start, end]);
},
2024-07-05 15:42:19 +08:00
},
2024-07-13 18:10:18 +08:00
{
text: "近六个月",
onClick(picker) {
const end = new Date();
const start = new Date();
start.setTime(start.getTime() - 3600 * 1000 * 24 * 180);
picker.$emit("pick", [start, end]);
},
},
{
text: "最近一年",
onClick(picker) {
const end = new Date();
const start = new Date();
start.setTime(start.getTime() - 3600 * 1000 * 24 * 360);
picker.$emit("pick", [start, end]);
},
2024-07-05 15:42:19 +08:00
},
2024-07-13 18:10:18 +08:00
],
}, //多选日期
};
} else {
config = {
align: "right",
2024-09-06 18:16:54 +08:00
placeholder: this.$attrs.placeholder||"选择日期",
2024-07-13 18:10:18 +08:00
"picker-options": {
disabledDate(time) {
return time.getTime() > Date.now();
2024-07-05 15:42:19 +08:00
},
2024-07-13 18:10:18 +08:00
shortcuts: [
{
2024-08-23 17:06:01 +08:00
text: "今天",
2024-07-13 18:10:18 +08:00
onClick(picker) {
const date = new Date();
2024-08-23 17:06:01 +08:00
date.setTime(date.getTime());
2024-07-13 18:10:18 +08:00
picker.$emit("pick", date);
},
},
{
2024-08-23 17:06:01 +08:00
text: "昨天",
2024-07-13 18:10:18 +08:00
onClick(picker) {
const date = new Date();
2024-08-23 17:06:01 +08:00
date.setTime(date.getTime() - 1* 24 * 60 * 60 * 1000);
2024-07-13 18:10:18 +08:00
picker.$emit("pick", date);
},
},
{
2024-08-23 17:06:01 +08:00
text: "一周前",
2024-07-13 18:10:18 +08:00
onClick(picker) {
const date = new Date();
2024-08-23 17:06:01 +08:00
date.setTime(date.getTime() - 7 * 24 * 60 * 60 * 1000);
2024-07-13 18:10:18 +08:00
picker.$emit("pick", date);
},
2024-08-23 17:06:01 +08:00
},
2024-07-13 18:10:18 +08:00
],
},
};
}
return {
size: "small",
type: "daterange",
2024-08-23 17:06:01 +08:00
format: this.showTime?"yyyy-MM-dd hh:mm:ss":"yyyy-MM-dd",
"value-format":this.showTime?"yyyy-MM-dd hh:mm:ss": "yyyy-MM-dd",
2024-07-13 18:10:18 +08:00
...config,
2024-07-05 15:42:19 +08:00
...this.$attrs,
};
},
// 所有父级事件
2024-09-06 18:16:54 +08:00
Listeners() {
2024-07-05 15:42:19 +08:00
return Object.assign({}, this.$listeners, {
input: (value) => {
2024-08-23 17:06:01 +08:00
if (this.range) {
2024-09-06 18:16:54 +08:00
if (!isEmpty(value) && value.length === 2 && value[0] && value[1]) {
if(this.showTime){
this.$emit("change", value);
}else{
this.$emit("change", [
`${value[0]} 00:00:00`,
2024-07-13 18:10:18 +08:00
`${value[1]} 23:59:59`,
]);
2024-09-06 18:16:54 +08:00
}
2024-07-13 18:10:18 +08:00
} else {
2024-08-23 17:06:01 +08:00
this.$emit("change", []);
2024-07-13 18:10:18 +08:00
}
} else {
2024-08-23 17:06:01 +08:00
this.$emit("change", value);
2024-07-05 15:42:19 +08:00
}
},
});
},
2024-09-06 18:16:54 +08:00
iconClass() {
2024-08-23 17:20:23 +08:00
return this.mousetrue
2024-07-08 17:19:16 +08:00
? "ag-icon-prefix-hide"
: "ag-icon-prefix-show";
},
2024-07-05 15:42:19 +08:00
},
2024-09-09 15:13:37 +08:00
mounted() {
if (Array.isArray(this.value)&&this.range){
2024-09-06 18:16:54 +08:00
this.dateArr = [this.value[0] || "", this.value[1] || ""];
} else {
this.dateArr = this.value;
}
2024-09-09 15:13:37 +08:00
for(const key in this.$refs.datepickerref){
if(!this[key]&&key!='value'){
this[key]=this.$refs.datepickerref[key];
}
}
2024-09-06 18:16:54 +08:00
},
2024-08-23 17:06:01 +08:00
watch: {
2024-07-05 15:42:19 +08:00
value: {
handler(newVal) {
2024-09-06 18:16:54 +08:00
console.log(newVal,'datevalue');
if (Array.isArray(newVal)&&this.range){
this.dateArr = [newVal[0] || "", newVal[1] || ""];
2024-07-13 18:10:18 +08:00
} else {
this.dateArr = newVal;
2024-07-05 15:42:19 +08:00
}
},
immediate: true,
},
2024-07-08 17:19:16 +08:00
dateArr: {
handler(newVal) {
2024-08-23 17:06:01 +08:00
if(!newVal){
return
2024-07-08 17:19:16 +08:00
}
2024-08-23 17:06:01 +08:00
let defaultWidth = this.range ?this.showTime?"330px":"170px" : this.showTime?"190px":"140px";
this.width = defaultWidth;
2024-07-08 17:19:16 +08:00
},
immediate: true,
deep: true,
},
},
2024-07-09 10:36:44 +08:00
methods: {},
2024-07-05 15:42:19 +08:00
};
</script>
2024-07-08 13:52:52 +08:00
<style lang="scss" scoped>
::v-deep {
2024-07-08 17:19:16 +08:00
[class*="ag-icon-"] {
2024-07-08 13:52:52 +08:00
font-family: element-icons !important;
speak: none;
font-style: normal;
font-weight: 400;
font-variant: normal;
text-transform: none;
line-height: 1;
vertical-align: baseline;
display: inline-block;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
2024-07-13 18:10:18 +08:00
.el-input__icon,
.ag-icon-prefix-show,
.ag-iconfont,
.ag-icon-clear {
2024-07-09 10:36:44 +08:00
position: absolute;
2024-07-13 18:10:18 +08:00
width: 16px !important;
2024-07-09 10:36:44 +08:00
font-size: 16px;
margin-left: -5px;
2024-07-13 18:10:18 +08:00
line-height: 1 !important;
right: 6px;
top: 1px;
2024-07-09 10:36:44 +08:00
z-index: 1;
}
2024-07-13 18:10:18 +08:00
.el-input__prefix {
position: absolute;
left: 98%;
}
2024-08-23 17:20:23 +08:00
.ag-icon-clear {
2024-07-09 10:36:44 +08:00
&:before {
content: "\e6db";
2024-07-08 13:52:52 +08:00
}
}
2024-07-08 17:19:16 +08:00
.ag-icon-prefix-show {
2024-07-08 13:52:52 +08:00
&:before {
content: "\e78e";
}
}
2024-07-08 17:19:16 +08:00
.ag-icon-prefix-hide {
display: none;
}
2024-07-08 13:52:52 +08:00
}
</style>