ut-action-sheet.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <template>
  2. <template v-if="mode === 'picker'">
  3. <picker :range="options" range-key="name" @change="selectChange" :disabled="false">
  4. <slot></slot>
  5. </picker>
  6. </template>
  7. <template v-else-if="mode === 'custom'">
  8. <view @click="showModel = true" class="flex1">
  9. <slot></slot>
  10. </view>
  11. <up-popup v-model:show="showModel" mode="center" round="30rpx" closeable @close="showModel = false">
  12. <view class="w-700">
  13. <view class="pd-24">
  14. <view class="f-s-32 c-#333 f-w-500">{{ title }}</view>
  15. </view>
  16. <scroll-view scroll-y style="max-height: 70vh">
  17. <view class="pd3-10-24-24">
  18. <ut-row gap="16rpx">
  19. <template v-for="(item, index) in options" :key="index">
  20. <ut-col :span="item.span">
  21. <view class="">{{ item?.name }}</view>
  22. </ut-col>
  23. </template>
  24. </ut-row>
  25. </view>
  26. </scroll-view>
  27. </view>
  28. </up-popup>
  29. </template>
  30. </template>
  31. <script setup lang="ts">
  32. import { ref, watch, computed } from 'vue';
  33. const props = defineProps({
  34. modelValue: {
  35. type: Array,
  36. default: () => [],
  37. },
  38. title: {
  39. type: String,
  40. default: '系统提示',
  41. },
  42. showTitle: {
  43. type: Boolean,
  44. default: true,
  45. },
  46. tabs: {
  47. type: Array,
  48. default: () => [],
  49. },
  50. mode: {
  51. type: String,
  52. default: 'picker', // 原生 picker 模式 或自定义弹窗模式 custom
  53. },
  54. // 是否多选
  55. multiple: {
  56. type: Boolean,
  57. default: false, // 只有自定义弹窗模式下生效
  58. },
  59. });
  60. const options = computed(() => {
  61. console.log(props.tabs);
  62. return props.tabs.map((item: any) => {
  63. return {
  64. name: item.label,
  65. value: item.value,
  66. span: item.elTagClass || 10,
  67. };
  68. });
  69. });
  70. const showModel = ref(false);
  71. const emit = defineEmits(['close', 'confirm', 'open', 'update:show', 'update:modelValue', 'change']);
  72. const checkeds = ref({});
  73. const close = () => {
  74. showModel.value = false;
  75. // 如果有初始值,恢复初始值
  76. checkeds.value = { ...initialCheckeds.value };
  77. emit('update:show', false);
  78. emit('close');
  79. };
  80. // 未选择之前的
  81. const initialCheckeds = ref({});
  82. const selectChange = (item: any) => {
  83. const selectedValues = options.value[item.detail.value].value;
  84. emit('update:modelValue', selectedValues);
  85. emit('change', selectedValues);
  86. close();
  87. };
  88. watch(
  89. () => props.modelValue,
  90. (newVal) => {},
  91. { immediate: true }
  92. );
  93. onMounted(() => {
  94. // 初始化已选择项
  95. console.log(props.tabs);
  96. });
  97. </script>
  98. <style lang="scss" scoped></style>