index.vue 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <template>
  2. <div class="app-datepicker">
  3. <div @click="show = true">
  4. <slot>
  5. <span class="app-datepicker__placeholder">{{ modelValue || placeholder }}</span>
  6. </slot>
  7. </div>
  8. <Popup v-model:show="show" position="bottom" round>
  9. <DatePicker v-model="currentDate" title="选择日期" @cancel="onCancel" @confirm="onConfirm" />
  10. </Popup>
  11. </div>
  12. </template>
  13. <script lang="ts" setup>
  14. import { ref, onMounted } from 'vue'
  15. import { Popup, DatePicker } from 'vant'
  16. import moment from 'moment'
  17. const props = defineProps({
  18. modelValue: {
  19. type: String,
  20. required: true
  21. },
  22. // 分隔符
  23. separator: {
  24. type: String,
  25. default: '-'
  26. },
  27. // 占位符文本
  28. placeholder: {
  29. type: String,
  30. default: '日期选择'
  31. }
  32. })
  33. const emit = defineEmits(['update:modelValue'])
  34. const show = ref(false)
  35. const currentDate = ref<string[]>([])
  36. const onCancel = () => {
  37. show.value = false
  38. }
  39. const onConfirm = () => {
  40. emit('update:modelValue', currentDate.value.join(props.separator))
  41. onCancel()
  42. }
  43. onMounted(() => {
  44. if (props.modelValue) {
  45. currentDate.value = props.modelValue.split(props.separator)
  46. } else {
  47. const now = moment()
  48. const dateArray = [now.format('YYYY'), now.format('MM'), now.format('DD')]
  49. currentDate.value = dateArray
  50. }
  51. })
  52. </script>
  53. <style lang="less">
  54. @import './index.less';
  55. </style>