ActionSheetRoot.vue 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <template>
  2. <ActionSheet
  3. v-bind="options"
  4. :show="show"
  5. @close="options?.onClose"
  6. @select="options?.onSelect"
  7. />
  8. </template>
  9. <script setup lang="ts">
  10. import { ref } from 'vue';
  11. import ActionSheet, { type ActionSheetProps } from './ActionSheet.vue';
  12. export interface ActionSheetOptions extends Omit<ActionSheetProps, 'show'> {
  13. onSelect?: (index: number, name: string) => void;
  14. onClose?: () => void;
  15. }
  16. export interface ActionSheetRoot {
  17. show(options: ActionSheetOptions): Promise<number|undefined>;
  18. }
  19. const show = ref(false);
  20. const options = ref<ActionSheetOptions>();
  21. defineExpose<ActionSheetRoot>({
  22. show(_options: ActionSheetOptions) {
  23. show.value = true;
  24. options.value = _options;
  25. const onSelect = _options.onSelect;
  26. const onClose = _options.onClose;
  27. return new Promise<number|undefined>((resolve) => {
  28. _options.onClose = () => {
  29. show.value = false;
  30. onClose?.();
  31. resolve(undefined);
  32. };
  33. _options.onSelect = (i: number, n: string) => {
  34. show.value = false;
  35. onSelect?.(i, n);
  36. resolve(i);
  37. };
  38. options.value = _options;
  39. show.value = true;
  40. });
  41. },
  42. })
  43. </script>