element的DateTimePicker時間選擇器確定才變更時間的問題

需求:是點擊確定才變更時間

element的組件是change就會改變雙向綁定的值,

看到源碼有handleClose方法,於是嘗試在handleClose方法動收件,並且套了一層,封裝這個DateTimePicker組件,

缺點:
點擊確定後,派發給父組件的值,在提交前需要處理轉化一下格式,但是好歹算是實現了

父組件:

   <picker
		:disabled="false"
	    v-model="timer"
	    format="yyyy.MM.dd HH:mm"
	    value-format="yyyy.MM.dd HH:mm"
	    type="datetime"
	    placeholder="選擇日期時間"
	    default-time="12:00:00"
    >
    </picker>
...
 components: {
	 'picker': import 'xxx/picker.vue',
  },
data(){
	return{
		timer:""
	}
},



子組件:


<template>
    <el-date-picker
            ref="datePicker"
            v-bind="$attrs"
            v-model="pValue"
            @change="handleChange"></el-date-picker>
</template>

<script>
    module.exports = {
        name: "picker",
        data() {
            return {
                pValue: "",
                clickOutsideFlag: false,
                childHandleClose: null
            };
        },
        props: {
            value: {
                type: [Date, String, Array],
                required: true
            }
        },
        created() {
        	//不用moment的話自己處理一下時間格式
  			const timer = moment(this.value, "YYYYMMDDHHmmss").format(`YYYY.MM.DD HH:mm`);
            this.$emit('input', timer);
        },
        mounted() {
            this.childHandleClose = this.$refs.datePicker.handleClose;
            this.$refs.datePicker.handleClose = this.handleClose;
        },
        watch: {
            value(val) {
                if (val === null || val === undefined) {
                    this.pValue = "";
                } else {
                    this.pValue = val;
                }
                this.updateInput();
            },
        },
        methods: {
            handleChange() {
                if (!this.clickOutsideFlag) {
                    this.updateInput();
                    this.$emit("change", this.pValue);
                } else {
                    this.clickOutsideFlag = false;
                    this.pValue = this.value;
                }
            },
            handleClose() {
                if (this.$refs.datePicker.pickerVisible) {
                    this.clickOutsideFlag = true;
                    this.childHandleClose();
                }
            },
            updateInput() {
                this.$emit('input', this.pValue);
                this.$refs.datePicker.$refs.reference.$refs.input.value = this.value;
            }
        }
    };
</script>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章