app-date-range-picker.vue 5.0 KB
Newer Older
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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
<template>
    <div class="app-date-range-picker">
        <el-time-picker
            v-if="Object.is(type, 'timerange')"
            v-model="curValue"
            is-range
            :size="size"
            popper-class="app-date-range-picker-popper"
            :format="format"
            :value-format="valueFormat"
            :disabled="disabled"
            :readonly="readonly"
            :range-separator="$t('components.appdaterangepicker.separator')"
            :start-placeholder="startPlaceholder || $t('components.appdaterangepicker.start')"
            :end-placeholder="endPlaceholder || $t('components.appdaterangepicker.end')"
            @focus="handleFocus"
            @blur="handleBlur"
        >
        </el-time-picker>
        <el-date-picker
            v-else
            v-model="curValue"
            :type="type"
            :size="size"
            popper-class="app-date-range-picker-popper"
            :format="format"
            :value-format="valueFormat"
            :disabled="disabled"
            :readonly="readonly"
            :unlink-panels="unlinkPanels"
            :range-separator="$t('components.appdaterangepicker.separator')"
            :start-placeholder="startPlaceholder || $t('components.appdaterangepicker.start')"
            :end-placeholder="endPlaceholder || $t('components.appdaterangepicker.end')"
            @focus="handleFocus"
            @blur="handleBlur"
        >
        </el-date-picker>
    </div>
</template>

<script lang="ts">
import { Component, Vue, Prop, Model } from 'vue-property-decorator';

@Component({})
export default class AppDateRangePicker extends Vue {
    /**
     * 双向绑定表单项值
     *
     * @type {*}
     * @memberof AppDateRangePicker
     */

    @Model('change') public value: any;

    /**
     * 编辑项名称
     *
     * @type {string}
     * @memberof AppRangDate
     */
    @Prop() public name!: string;

    /**
     * 表单数据
     *
     * @type {*}
     * @memberof AppRangDate
     */
    @Prop() public data: any;

    /**
     * 是否禁用
     *
     * @type {boolean}
     * @memberof AppRangDate
     */
    @Prop({ default: false }) public disabled!: boolean;

    /**
     * 只读
     *
     * @type {boolean}
     * @memberof AppRangDate
     */
    @Prop({ default: false }) public readonly?: boolean;

    /**
     * 类型
     *
     * @type {string}
     * @memberof AppRangDate
     */
    @Prop({ default: 'daterange' }) public type!: string;

    /**
     * 显示格式化
     *
     * @type {string}
     * @memberof AppRangDate
     */
    @Prop() public format?: string;

    /**
     * 值格式化
     *
     * @type {string}
     * @memberof AppRangDate
     */
    @Prop() public valueFormat?: string;

    /**
     * 开始占位提示
     *
     * @type {string}
     * @memberof AppRangDate
     */
    @Prop() public startPlaceholder?: string;

    /**
     * 结束占位提示
     *
     * @type {string}
     * @memberof AppRangDate
     */
    @Prop() public endPlaceholder?: string;

    /**
     * 大小
     *
     * @type {string}
     * @memberof AppRangDate
     */
    @Prop({default: 'small'}) public size?: 'large' | 'small' | 'mini';

    /**
     * 关系表单项集合
     *
     * @type {string}
     * @memberof AppRangDate
     */
    @Prop() public valueItemNames?: string;

    /**
     * 取消范围联动
     *
     * @type {boolean}
     * @memberof AppRangDate
     */
    @Prop({ default: false }) public unlinkPanels!: boolean;

    /**
     * 值分割符
     *
     * @type {boolean}
     * @memberof AppRangDate
     */
    @Prop({ default: ',' }) public valueSeparator!: string;

    /**
     * 输入框失焦
     * @param e
     */
    public handleBlur(e: any): void {
        this.$emit('blur', this.value);
    }

    /**
     * 输入框聚焦
     * @param e
     */
    public handleFocus(e: any): void {
        this.$emit('focus', this.value);
    }

    get curValue() {
        let value: any[] = [];
        if (this.valueItemNames) {
            this.valueItemNames.split(',').forEach((name: any) => {
                if (this.data[name]) {
                    value.push(this.data[name]);
                }
            });
        } else if (this.value) {
            value = this.value.split(this.valueSeparator);
        }
        return value;
    }

    set curValue(dates: string[]) {
        if (dates) {
            this.$emit('change', dates.join(this.valueSeparator));
            if (this.valueItemNames) {
                const valueItemNames: string[] = this.valueItemNames.split(',');
                dates.forEach((value: any, index: number) => {
                    this.$emit('itemChange', { name: valueItemNames[index], value: value });
                });
            }
        } else {
            this.$emit('change', null);
            if (this.valueItemNames) {
                this.valueItemNames.split(',').forEach((name: any) => {
                    this.$emit('itemChange', { name: name, value: null });
                });
            }
        }
    }
}
</script>