DateTimePickerField.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. import { usePickerFieldInstance, type PickerFieldInstance } from './Picker';
  40. export interface DateTimePickerFieldProps extends Omit<DateTimePickerProps, 'modelValue'> {
  41. modelValue?: Date;
  42. /**
  43. * 标题
  44. * @default '请选择时间'
  45. */
  46. title?: string,
  47. /**
  48. * 标题属性
  49. */
  50. titleProps?: Omit<ActionSheetTitleProps, 'title'>,
  51. /**
  52. * 是否显示选择的文本
  53. * @default true
  54. */
  55. showSelectText?: boolean,
  56. /**
  57. * 占位符
  58. * @default '请选择时间'
  59. */
  60. placeholder?: string,
  61. /**
  62. * 初始值
  63. */
  64. initalValue?: Date,
  65. /**
  66. * 是否立即更新绑定值
  67. * @default false
  68. */
  69. shouldUpdateValueImmediately?: boolean,
  70. /**
  71. * 显示的文本属性
  72. */
  73. textProps?: TextProps,
  74. }
  75. const emit = defineEmits([ 'update:modelValue', 'cancel', 'confirm', 'selectTextChange', 'tempValueChange' ]);
  76. const props = withDefaults(defineProps<DateTimePickerFieldProps>(), {
  77. title: '请选择时间',
  78. placeholder: '请选择时间',
  79. titleProps: () => ({
  80. cancelText: '取消',
  81. confirmText: '确定',
  82. }),
  83. showSelectText: true,
  84. showYears: true,
  85. showMonths: true,
  86. showDays: true,
  87. showHours: true,
  88. showMinute: true,
  89. showSecond: true,
  90. });
  91. const popupShow = ref(false);
  92. const {
  93. value,
  94. updateValue,
  95. } = useFieldChildValueInjector(
  96. toRef(props, 'modelValue'),
  97. (v) => emit('update:modelValue', v),
  98. undefined,
  99. () => {
  100. popupShow.value = true;
  101. },
  102. props.initalValue,
  103. );
  104. const {
  105. onSelectTextChange,
  106. onCancel,
  107. onConfirm,
  108. selectText,
  109. tempValue,
  110. } = usePickerFieldTempStorageData(
  111. value,
  112. updateValue,
  113. () => popupShow.value = false,
  114. emit as any,
  115. new Date(),
  116. props.shouldUpdateValueImmediately,
  117. undefined,
  118. popupShow,
  119. );
  120. defineExpose<PickerFieldInstance>(usePickerFieldInstance(popupShow));
  121. defineOptions({
  122. options: {
  123. styleIsolation: "shared",
  124. virtualHost: true,
  125. }
  126. })
  127. </script>