DataStatisticsDialog.vue 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <script setup lang="ts" name="modal">
  2. import { ref } from 'vue';
  3. import type { FormInstance } from 'element-plus';
  4. // 定义props
  5. interface Props {
  6. isVisible: boolean,
  7. }
  8. type Emit = {
  9. (e: 'update:isVisible', isVisible: boolean): any
  10. (e: 'confirmed', data: string): any,
  11. (e: 'cancel'): any,
  12. }
  13. interface FormRule {
  14. month?: number;
  15. }
  16. const emit = defineEmits<Emit>();
  17. const props = defineProps<Props>();
  18. const formData = reactive<FormRule>({});
  19. const formRef = ref<FormInstance>();
  20. const rules = {
  21. month: [
  22. { required: true, message: '请选择日期', trigger: 'blur' },
  23. ]
  24. };
  25. // 点击确定
  26. const onConfirm = async() => {
  27. if (!formRef) return;
  28. await formRef.value?.validate();
  29. try{
  30. emit('confirmed', formData.month);
  31. onCloseDialog()
  32. } finally {
  33. // TODO
  34. }
  35. }
  36. const onCloseDialog = () => {
  37. formRef.value?.resetFields();
  38. formRef.value?.clearValidate();
  39. emit('update:isVisible', false);
  40. emit('cancel')
  41. };
  42. const disabledFutureMonths = (date: Date) => {
  43. const today = new Date();
  44. const currentYear = today.getFullYear();
  45. const currentMonth = today.getMonth();
  46. const selectYear = date.getFullYear();
  47. const selectMonth = date.getMonth();
  48. return selectYear > currentYear ||
  49. (selectYear === currentYear && selectMonth > currentMonth);
  50. };
  51. </script>
  52. <template>
  53. <div class="modal">
  54. <el-dialog
  55. :model-value="props.isVisible"
  56. title="数据统计"
  57. width="30%"
  58. center
  59. :close-on-click-modal="false"
  60. @close="onCloseDialog()"
  61. >
  62. <el-form
  63. ref="formRef"
  64. :rules="rules"
  65. :model="formData"
  66. size="large"
  67. label-width="100px"
  68. >
  69. <el-form-item label="数据周期" prop="month">
  70. <el-date-picker
  71. v-model="formData.month"
  72. type="month"
  73. placeholder="请选择月份"
  74. value-format="YYYY-MM"
  75. :disabled-date="disabledFutureMonths"
  76. />
  77. </el-form-item>
  78. </el-form>
  79. <template #footer>
  80. <span class="dialog-footer">
  81. <el-button @click="onConfirm()">确认</el-button>
  82. <el-button @click="onCloseDialog()">取消</el-button>
  83. </span>
  84. </template>
  85. </el-dialog>
  86. </div>
  87. </template>
  88. <style scoped lang="scss">
  89. </style>