app-date-range.vue 3.5 KB
Newer Older
1
<template>
2 3 4 5 6 7 8 9 10 11
  <date-picker
    :class="['app-date-range', 'app-date-range--' + type]"
    :type="rangeType"
    :value="realVal"
    :placeholder="placeholder"
    :disabled="disabled"
    transfer
    @on-change="onChange"
    @on-open-change="onOpenChange"
  ></date-picker>
12 13 14
</template>

<script lang="ts">
15
import { Vue, Component, Prop, Watch } from "vue-property-decorator";
16
@Component({})
17
export default class AppDateRange extends Vue {
18 19 20 21 22 23 24
  /**
   * 是否禁用
   *
   * @type {string}
   * @memberof AppDateRange
   */
  @Prop({ default: false }) public disabled?: boolean;
25

26 27 28 29 30 31 32
  /**
   * placeholder
   *
   * @type {string}
   * @memberof AppDateRange
   */
  @Prop({ default: "请选择时间" }) public placeholder?: string;
33

34 35 36 37 38 39 40
  /**
   * 日期值格式化
   *
   * @type {string}
   * @memberof AppDateRange
   */
  @Prop() public valueFormat?: string;
41

42 43 44 45 46 47 48
  /**
   * 数据类型
   *
   * @type {string}
   * @memberof AppDateRange
   */
  @Prop({ default: "datetime" }) public type?: "datetime" | "date";
49

50 51 52 53 54 55 56
  /**
   * 开始属性名称
   *
   * @type {string}
   * @memberof AppDateRange
   */
  @Prop() public startField?: string;
57

58 59 60 61 62 63 64
  /**
   * 结束属性名称
   *
   * @type {string}
   * @memberof AppDateRange
   */
  @Prop() public endField?: string;
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
  /**
   * 传入表单数据
   *
   * @type {*}
   * @memberof AppDateRange
   */
  @Prop() public data?: any;

  /**
   * 实际组件维护的值
   *
   * @type {*}
   * @memberof AppDateRange
   */
  public realVal: any[] = [];

  /**
   * 是否修改过数据
   * @type {boolean}
   * @memberof AppDateRange
   */
  public modified: boolean = false;

  /**
   * iview组件类型`
   *
   * @type {string}
   * @memberof AppDateRange
   */
  get rangeType() {
    switch (this.type) {
      case "date":
        return "daterange";
      default:
        return "datetimerange";
    }
  }

  /**
   * 生命周期
   *
   * @type {string}
   * @memberof AppDateRange
   */
  created() {
    if (!this.startField) {
      this.$Notice.error({
        title: this.$t("app.commonWords.wrong") as string,
        desc: "没有配置开始属性",
      });
    }
    if (!this.endField) {
      this.$Notice.error({
        title: this.$t("app.commonWords.wrong") as string,
        desc: "没有配置结束属性",
      });
    }
  }

  /**
   * 监控表单属性 data 值
   *
   * @memberof AppDateRange
   */
  @Watch("data", { deep: true, immediate: true })
  onDataChange(newVal: any, oldVal: any) {
    if (newVal && this.startField && this.endField) {
      this.realVal = [newVal[this.startField], newVal[this.endField]];
      this.modified = false;
      console.log(this.realVal);
    }
  }

  /**
   * iview组件值变更事件(只更新realVal,不抛出)
   *
   * @memberof AppDateRange
   */
  onChange(values: any[]) {
    this.realVal = values;
    this.modified = true;
    if(values[0]==='' && values[1] === ''){
      //清空的情况,直接抛值,openChange会先触发,或不触发。
      this.$emit("change", this.startField!, null);
      this.$emit("change", this.endField!, null);
    }
  }

  /**
   * iview组件值关闭事件(如果数据有变更,则抛出change事件)
   *
   * @memberof AppDateRange
   */
  onOpenChange(isOpen: boolean) {
    if (!isOpen && this.modified) {
      const [start, end] = this.realVal;
      this.$emit("change", this.startField!, start);
      this.$emit("change", this.endField!, end);
164
    }
165
  }
166 167 168
}
</script>

169 170 171
<style lang="scss">
@import "./app-date-range.scss";
</style>