DateTimePickerField.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <template>
  2. <Popup
  3. :show="popupShow"
  4. @close="onCancel"
  5. :closeIcon="false"
  6. position="bottom"
  7. closeable
  8. >
  9. <ActionSheetTitle
  10. v-bind="titleProps"
  11. :title="title"
  12. @cancel="onCancel"
  13. @confirm="onConfirm"
  14. />
  15. <DateTimePicker
  16. v-bind="props"
  17. v-model="tempValue"
  18. @selectTextChange="onSelectTextChange"
  19. />
  20. </Popup>
  21. <Text
  22. v-if="showSelectText"
  23. :size="30"
  24. :color="selectText ? 'text.content' : 'text.second'"
  25. :text="selectText || placeholder"
  26. :maxWidth="300"
  27. v-bind="textProps"
  28. />
  29. </template>
  30. <script setup lang="ts">
  31. import { ref, toRef } from 'vue';
  32. import { useFieldChildValueInjector } from './FormContext';
  33. import type { DateTimePickerProps } from './DateTimePicker.vue';
  34. import Popup from '../dialog/Popup.vue';
  35. import ActionSheetTitle, { type ActionSheetTitleProps } from '../dialog/ActionSheetTitle.vue';
  36. import DateTimePicker from './DateTimePicker.vue';
  37. import { usePickerFieldTempStorageData } from './PickerUtils';
  38. import Text, { type TextProps } from '../basic/Text.vue';
  39. export interface DateTimePickerFieldProps extends Omit<DateTimePickerProps, 'modelValue'> {
  40. modelValue?: Date;
  41. /**
  42. * 标题
  43. * @default '请选择时间'
  44. */
  45. title?: string,
  46. /**
  47. * 标题属性
  48. */
  49. titleProps?: Omit<ActionSheetTitleProps, 'title'>,
  50. /**
  51. * 是否显示选择的文本
  52. * @default true
  53. */
  54. showSelectText?: boolean,
  55. /**
  56. * 占位符
  57. * @default '请选择时间'
  58. */
  59. placeholder?: string,
  60. /**
  61. * 初始值
  62. */
  63. initalValue?: Date,
  64. /**
  65. * 是否立即更新绑定值
  66. * @default false
  67. */
  68. shouldUpdateValueImmediately?: boolean,
  69. /**
  70. * 显示的文本属性
  71. */
  72. textProps?: TextProps,
  73. }
  74. const emit = defineEmits([ 'update:modelValue', 'cancel', 'confirm', 'selectTextChange', 'tempValueChange' ]);
  75. const props = withDefaults(defineProps<DateTimePickerFieldProps>(), {
  76. title: '请选择时间',
  77. placeholder: '请选择时间',
  78. titleProps: () => ({
  79. cancelText: '取消',
  80. confirmText: '确定',
  81. }),
  82. showSelectText: true,
  83. showYears: true,
  84. showMonths: true,
  85. showDays: true,
  86. showHours: true,
  87. showMinute: true,
  88. showSecond: true,
  89. });
  90. const popupShow = ref(false);
  91. const {
  92. value,
  93. updateValue,
  94. } = useFieldChildValueInjector(
  95. toRef(props, 'modelValue'),
  96. (v) => emit('update:modelValue', v),
  97. undefined,
  98. () => {
  99. popupShow.value = true;
  100. },
  101. props.initalValue,
  102. );
  103. const {
  104. onSelectTextChange,
  105. onCancel,
  106. onConfirm,
  107. selectText,
  108. tempValue,
  109. } = usePickerFieldTempStorageData(
  110. value,
  111. updateValue,
  112. () => popupShow.value = false,
  113. emit as any,
  114. new Date(),
  115. props.shouldUpdateValueImmediately,
  116. undefined,
  117. popupShow,
  118. );
  119. defineOptions({
  120. options: {
  121. styleIsolation: "shared",
  122. virtualHost: true,
  123. }
  124. })
  125. </script>