DatePickerField.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. <DatePicker
  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 { DatePickerProps } from './DatePicker.vue';
  34. import Popup from '../dialog/Popup.vue';
  35. import ActionSheetTitle, { type ActionSheetTitleProps } from '../dialog/ActionSheetTitle.vue';
  36. import DatePicker from './DatePicker.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 DatePickerFieldProps extends Omit<DatePickerProps, '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<DatePickerFieldProps>(), {
  77. title: '请选择时间',
  78. placeholder: '请选择时间',
  79. titleProps: () => ({
  80. cancelText: '取消',
  81. confirmText: '确定',
  82. }),
  83. showSelectText: true,
  84. showYears: true,
  85. showMonths: true,
  86. showDays: true,
  87. });
  88. const popupShow = ref(false);
  89. const {
  90. value,
  91. updateValue,
  92. } = useFieldChildValueInjector(
  93. toRef(props, 'modelValue'),
  94. (v) => emit('update:modelValue', v),
  95. undefined,
  96. () => {
  97. popupShow.value = true;
  98. },
  99. props.initalValue,
  100. );
  101. const {
  102. onSelectTextChange,
  103. onCancel,
  104. onConfirm,
  105. selectText,
  106. tempValue,
  107. } = usePickerFieldTempStorageData(
  108. value,
  109. updateValue,
  110. () => popupShow.value = false,
  111. emit as any,
  112. new Date(),
  113. props.shouldUpdateValueImmediately,
  114. undefined,
  115. popupShow,
  116. );
  117. defineExpose<PickerFieldInstance>(usePickerFieldInstance(popupShow));
  118. defineOptions({
  119. options: {
  120. styleIsolation: "shared",
  121. virtualHost: true,
  122. }
  123. })
  124. </script>