cuiqian2004
2025-04-01 01fa158740467ca0db83b131249beeeb515a73c2
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
<template>
    <uni-datetime-picker v-if="model" type="date" :value="model[data.fieldId]" :placeholder="data.placeholder" border
        :disabled="data.disabled" clearIcon @change="onChange" />
    <uni-datetime-picker v-else type="date" :value="data.value" :placeholder="data.placeholder" border
        :disabled="data.disabled" clearIcon @change="onChange" />
</template>
 
<script>
    import dayjs from "dayjs";
    export default {
        name: 'OIFormDatePicker',
        props: {
            data: {
                type: Object,
                required: true,
            },
            model: Object,
        },
        data() {
            return {}
        },
        computed: {
        },
        methods: {
            onChange(date) {
                let format = "YYYY-MM-DD";
                if (this.data.setting.format)
                    format = this.data.setting.format
                    .replace(/y/g, "Y")
                    .replace(/m/g, "M")
                    .replace(/d/g, "D");
                let date2 = dayjs(date).format(format);
                if (!this.model) {
                    this.data.value = date2
                } else {
                    this.model[this.data.fieldId] = date2
                }
                this.$emit("change", date2)
            },
            checkDefaultDate() {
                let format = "YYYY-MM-DD";
                if (!this.model) {
                    return
                }
                if (this.data.setting.format)
                    format = this.data.setting.format
                    .replace(/y/g, "Y")
                    .replace(/m/g, "M")
                    .replace(/d/g, "D");
 
                if (this.data.setting.defaultToday && !this.model[this.data.fieldId]) {
                    let date = dayjs().format(format);
                    this.model[this.data.fieldId] = date
                }
 
            },
        },
        mounted() {
            if (!this.model) {
                if (this.data.setting.defaultToday && !this.data.value) {
                    let format = "YYYY-MM-DD";
                    if (this.data.setting.format)
                        format = this.data.setting.format
                        .replace(/y/g, "Y")
                        .replace(/m/g, "M")
                        .replace(/d/g, "D");
                    let date = dayjs().format(format);
                    this.data.value = date
                }
            }
            this.checkDefaultDate();
        },
        watch: {
            data() {
                this.checkDefaultDate();
            },
        },
    }
</script>
 
<style>
</style>