CascadePickerField.vue 2.7 KB

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