cuiqian2004
2025-03-14 00814401bba75825126baa6675e542ea3c82a5bb
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
137
<template>
    <uni-datetime-picker type="date" v-model="displayValue" :start="start" :end="end" :returnType="returnType"
        :placeholder="placeholder" :border="border" :disabled="disabled" :clearIcon="clearIcon" @change="onChange" />
</template>
 
<script>
    import dayjs from "dayjs";
    export default {
        name: 'OIDatePicker',
        emits: ['update:modelValue', 'clear'],
        props: {
            value: {
                type: [String, Number, Date],
                default: ''
            },
            modelValue: {
                type: [String, Number, Date],
                default: ''
            },
 
            start: {
                type: [Number, String],
                default: ''
            },
            end: {
                type: [Number, String],
                default: ''
            },
            returnType: {
                type: String,
                default: 'string'
            },
            placeholder: {
                type: String,
                default: ''
            },
            border: {
                type: [Boolean],
                default: true
            },
            disabled: {
                type: [Boolean],
                default: false
            },
            clearIcon: {
                type: [Boolean],
                default: true
            },
            hideSecond: {
                type: [Boolean],
                default: false
            },
            format: {
                type: String,
                default: ''
            },
            defaultToday: {
                type: Boolean,
                default: false
            }
        },
        data() {
            return {
                displayValue: "",
            }
        },
        methods: {
            onChange(date) {
 
                let format = "YYYY-MM-DD";
                if (this.format)
                    format = this.format
                    .replace(/y/g, "Y")
                    .replace(/m/g, "M")
                    .replace(/d/g, "D");
                let date2 = dayjs(date).format(format);
                console.log(date, this.displayValue, date2)
 
                this.$emit("update:modelValue", date2)
                this.$emit('input', date2)
                this.$emit("change", date2)
            },
            checkDefaultDate() {
                let format = "YYYY-MM-DD";
                if (this.format)
                    format = this.format
                    .replace(/y/g, "Y")
                    .replace(/m/g, "M")
                    .replace(/d/g, "D");
                if (this.defaultToday && !this.value) {
                    this.displayValue = dayjs().format("YYYY-MM-DD");
                    let date = dayjs(this.displayValue).format(format);
                    this.$emit("update:modelValue", date)
                    this.$emit('input', date)
                    this.$emit("change", date)
                }
            },
        },
        mounted() {
            this.checkDefaultDate();
        },
        watch: {
            // #ifndef VUE3
 
            value: {
                immediate: true,
                handler(newVal) {
                    if (newVal) {
                        this.displayValue = dayjs(newVal).format("YYYY-MM-DD");
                    } else {
                        this.displayValue = ''
                    }
                },
            },
            // #endif
            // #ifdef VUE3
            modelValue: {
                immediate: true,
                handler(newVal) {
                    console.log(newVal)
                    if (newVal) {
                        this.displayValue = dayjs(newVal).format("YYYY-MM-DD");
                    } else {
                        this.displayValue = ''
                    }
                },
 
            },
 
            // #endif
 
        },
    }
</script>
 
<style>
</style>