2020-01-20 基于Vue Composition API

2020-01-20  本文已影响0人  rub1cky
import { DatePicker } from 'ant-design-vue'
import moment from 'moment'
import { reactive, toRefs, watch } from '@vue/composition-api'

export default {
  name: 'OleDatePicker',
  model: {
    prop: 'value',
    event: 'change'
  },
  props: {
    value: String
  },
  setup (props, { attrs, emit }) {
    const state = reactive({ dateValue: null })

    let { showTime } = attrs

    watch(() => props.value, v => {
      state.dateValue = v ? moment(v) : null
    })

    watch(() => state.dateValue, (v) => {
      const format = showTime ? `YYYY-MM-DD HH:mm:ss` : `YYYY-MM-DD`
      let dateEmit = v ? moment(v).format(format) : null
      emit('change', dateEmit)
    })

    const handleChange = (date) => {
      state.dateValue = date
    }

    return {
      ...toRefs(state),
      handleChange
    }
  },
  render () {
    const props = {
      attrs: this.$attrs,
      props: {
        ...this.$attrs,
        value: this.dateValue
      },
      on: {
        ...this.$listeners,
        change: this.handleChange
      }
    }
    return <DatePicker {...props}></DatePicker>
  }
}
上一篇下一篇

猜你喜欢

热点阅读