|
11 | 11 | </component>
|
12 | 12 | </template>
|
13 | 13 |
|
14 |
| -<script lang="ts"> |
15 |
| -import {computed, defineComponent, onMounted, ref, watch} from 'vue' |
| 14 | +<script setup lang="ts"> |
| 15 | +// import type {BCollapseEmits, BCollapseProps} from '@/types/components' |
| 16 | +import {computed, onMounted, ref, watch} from 'vue' |
16 | 17 | import Collapse from 'bootstrap/js/dist/collapse'
|
17 |
| -import useEventListener from '../composables/useEventListener' |
18 |
| -import getID from '../utils/getID' |
| 18 | +import useEventListener from '@/composables/useEventListener' |
| 19 | +import getID from '@/utils/getID' |
19 | 20 |
|
20 |
| -export default defineComponent({ |
21 |
| - name: 'BCollapse', |
22 |
| - props: { |
23 |
| - accordion: {type: String, required: false}, |
24 |
| - // appear: {type: Boolean, default: false}, |
25 |
| - id: {type: String, default: getID()}, |
26 |
| - // isNav: {type: Boolean, default: false}, |
27 |
| - modelValue: {type: Boolean, default: false}, |
28 |
| - tag: {type: String, default: 'div'}, |
29 |
| - toggle: {type: Boolean, default: false}, |
30 |
| - visible: {type: Boolean, default: false}, |
31 |
| - }, |
32 |
| - emits: ['update:modelValue', 'show', 'shown', 'hide', 'hidden'], |
33 |
| - setup(props, {emit}) { |
34 |
| - const element = ref<HTMLElement>() |
35 |
| - const instance = ref<Collapse>() |
36 |
| - const classes = computed(() => ({ |
37 |
| - show: props.modelValue, |
38 |
| - })) |
| 21 | +interface BCollapseProps { |
| 22 | + accordion?: string |
| 23 | + // appear?: Boolean |
| 24 | + id?: string |
| 25 | + // isNav?: Boolean |
| 26 | + modelValue?: boolean |
| 27 | + tag?: string |
| 28 | + toggle?: boolean |
| 29 | + visible?: boolean |
| 30 | +} |
39 | 31 |
|
40 |
| - const close = () => emit('update:modelValue', false) |
| 32 | +const props = withDefaults(defineProps<BCollapseProps>(), { |
| 33 | + id: getID(), |
| 34 | + modelValue: false, |
| 35 | + tag: 'div', |
| 36 | + toggle: false, |
| 37 | + visible: false, |
| 38 | +}) |
41 | 39 |
|
42 |
| - useEventListener(element, 'show.bs.collapse', () => { |
43 |
| - emit('show') |
44 |
| - emit('update:modelValue', true) |
45 |
| - }) |
| 40 | +interface BCollapseEmits { |
| 41 | + (e: 'update:modelValue', value: boolean): void |
| 42 | + (e: 'show'): void |
| 43 | + (e: 'shown'): void |
| 44 | + (e: 'hide'): void |
| 45 | + (e: 'hidden'): void |
| 46 | +} |
46 | 47 |
|
47 |
| - useEventListener(element, 'hide.bs.collapse', () => { |
48 |
| - emit('hide') |
49 |
| - emit('update:modelValue', false) |
50 |
| - }) |
| 48 | +const emit = defineEmits<BCollapseEmits>() |
51 | 49 |
|
52 |
| - useEventListener(element, 'shown.bs.collapse', () => emit('shown')) |
53 |
| - useEventListener(element, 'hidden.bs.collapse', () => emit('hidden')) |
| 50 | +const element = ref<HTMLElement>() |
| 51 | +const instance = ref<Collapse>() |
| 52 | +const classes = computed(() => ({ |
| 53 | + show: props.modelValue, |
| 54 | +})) |
54 | 55 |
|
55 |
| - onMounted(() => { |
56 |
| - instance.value = new Collapse(element.value as HTMLElement, { |
57 |
| - parent: props.accordion ? `#${props.accordion}` : undefined, |
58 |
| - toggle: props.toggle, |
59 |
| - }) |
60 |
| - if (props.visible || props.modelValue) { |
61 |
| - emit('update:modelValue', true) |
62 |
| - instance.value?.show() |
63 |
| - } |
64 |
| - }) |
| 56 | +const close = () => emit('update:modelValue', false) |
65 | 57 |
|
66 |
| - watch( |
67 |
| - () => props.modelValue, |
68 |
| - (value) => { |
69 |
| - if (value) { |
70 |
| - instance.value?.show() |
71 |
| - } else { |
72 |
| - instance.value?.hide() |
73 |
| - } |
74 |
| - } |
75 |
| - ) |
76 |
| - watch( |
77 |
| - () => props.visible, |
78 |
| - (value) => { |
79 |
| - if (value) { |
80 |
| - emit('update:modelValue', !!value) |
81 |
| - instance.value?.show() |
82 |
| - } else { |
83 |
| - emit('update:modelValue', !!value) |
84 |
| - instance.value?.hide() |
85 |
| - } |
86 |
| - } |
87 |
| - ) |
| 58 | +useEventListener(element, 'show.bs.collapse', () => { |
| 59 | + emit('show') |
| 60 | + emit('update:modelValue', true) |
| 61 | +}) |
88 | 62 |
|
89 |
| - return { |
90 |
| - element, |
91 |
| - classes, |
92 |
| - close, |
93 |
| - } |
94 |
| - }, |
| 63 | +useEventListener(element, 'hide.bs.collapse', () => { |
| 64 | + emit('hide') |
| 65 | + emit('update:modelValue', false) |
95 | 66 | })
|
| 67 | +
|
| 68 | +useEventListener(element, 'shown.bs.collapse', () => emit('shown')) |
| 69 | +useEventListener(element, 'hidden.bs.collapse', () => emit('hidden')) |
| 70 | +
|
| 71 | +onMounted(() => { |
| 72 | + instance.value = new Collapse(element.value as HTMLElement, { |
| 73 | + parent: props.accordion ? `#${props.accordion}` : undefined, |
| 74 | + toggle: props.toggle, |
| 75 | + }) |
| 76 | + if (props.visible || props.modelValue) { |
| 77 | + emit('update:modelValue', true) |
| 78 | + instance.value?.show() |
| 79 | + } |
| 80 | +}) |
| 81 | +
|
| 82 | +watch( |
| 83 | + () => props.modelValue, |
| 84 | + (value) => { |
| 85 | + if (value) { |
| 86 | + instance.value?.show() |
| 87 | + } else { |
| 88 | + instance.value?.hide() |
| 89 | + } |
| 90 | + } |
| 91 | +) |
| 92 | +watch( |
| 93 | + () => props.visible, |
| 94 | + (value) => { |
| 95 | + if (value) { |
| 96 | + emit('update:modelValue', !!value) |
| 97 | + instance.value?.show() |
| 98 | + } else { |
| 99 | + emit('update:modelValue', !!value) |
| 100 | + instance.value?.hide() |
| 101 | + } |
| 102 | + } |
| 103 | +) |
96 | 104 | </script>
|
0 commit comments