datetime-picker.vue 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <template>
  2. <view class="bg-white py-20 px-30 text-26 ">
  3. <view class="h-50 p-10 flex items-center gap-x-10 border border-[#999999] border-solid rounded-6"
  4. @click="handlePicker">
  5. <uv-icon name="calendar" size="20" color="#999999" />
  6. <text class="flex-1 text-center" :class="[startDate ? 'text-fore-title' : 'text-[#999999]']">
  7. {{ startDate || '开始日期' }}
  8. </text>
  9. <text class="text-[#999999]">至</text>
  10. <text class="flex-1 text-center" :class="[endDate ? 'text-fore-title' : 'text-[#999999]']">
  11. {{ endDate || '结束日期' }}
  12. </text>
  13. <view class="w-44">
  14. <uv-icon name="close-circle" size="20" color="#999999" v-if="startDate || endDate" @click.stop="handleClear" />
  15. </view>
  16. </view>
  17. </view>
  18. <ie-popup ref="popupRef" title="选择日期" @confirm="handleConfirm">
  19. <uni-calendar ref="calendarRef" :insert="true" :lunar="false" :range="true" :showMonth="false"
  20. :showExtraInfo="false" start-date="" end-date="" @change="handleChange"></uni-calendar>
  21. </ie-popup>
  22. </template>
  23. <script lang="ts" setup>
  24. const emit = defineEmits<{
  25. (e: 'change', value: { startDate: string, endDate: string }): void
  26. }>();
  27. const startDate = defineModel<string>('startDate', { required: true });
  28. const endDate = defineModel<string>('endDate', { required: true });
  29. const currentStartDate = ref('');
  30. const currentEndDate = ref('');
  31. const handleChange = (e: any) => {
  32. const { before, after } = e.range;
  33. if (before && after) {
  34. currentStartDate.value = before;
  35. currentEndDate.value = after;
  36. }
  37. }
  38. const popupRef = ref();
  39. const handlePicker = () => {
  40. popupRef.value.open()
  41. }
  42. const handleClear = () => {
  43. currentStartDate.value = ''
  44. currentEndDate.value = ''
  45. updateDate();
  46. }
  47. const updateDate = () => {
  48. startDate.value = currentStartDate.value;
  49. endDate.value = currentEndDate.value;
  50. setTimeout(() => {
  51. emit('change', { startDate: startDate.value, endDate: endDate.value });
  52. }, 0);
  53. }
  54. const handleConfirm = () => {
  55. updateDate();
  56. popupRef.value.close();
  57. }
  58. </script>
  59. <style lang="scss" scoped></style>