|
12 | 12 | </div>
|
13 | 13 | </template>
|
14 | 14 |
|
15 |
| -<script lang="ts"> |
16 |
| -import {computed, defineComponent, onBeforeUnmount, PropType, ref, watch} from 'vue' |
| 15 | +<script setup lang="ts"> |
| 16 | +// import type {BAlertEmits, BAlertProps} from '@/types/components' |
| 17 | +import type {ColorVariant} from '@/types' |
| 18 | +import {computed, onBeforeUnmount, ref, watch} from 'vue' |
17 | 19 | import Alert from 'bootstrap/js/dist/alert'
|
18 |
| -import {ColorVariant} from '../types' |
19 |
| -import {toInteger} from '../utils/number' |
20 |
| -
|
21 |
| -export default defineComponent({ |
22 |
| - name: 'BAlert', |
23 |
| - props: { |
24 |
| - dismissLabel: {type: String, default: 'Close'}, |
25 |
| - dismissible: {type: Boolean, default: false}, |
26 |
| - fade: {type: Boolean, default: false}, |
27 |
| - modelValue: {type: [Boolean, Number], default: false}, |
28 |
| - show: {type: Boolean, default: false}, |
29 |
| - variant: {type: String as PropType<ColorVariant>, default: 'info'}, |
30 |
| - }, |
31 |
| - emits: ['dismissed', 'dismiss-count-down', 'update:modelValue'], |
32 |
| - setup(props, {emit}) { |
33 |
| - const element = ref<HTMLElement>() |
34 |
| - const instance = ref<Alert>() |
35 |
| - const classes = computed(() => ({ |
36 |
| - [`alert-${props.variant}`]: props.variant, |
37 |
| - 'show': props.modelValue, |
38 |
| - 'alert-dismissible': props.dismissible, |
39 |
| - 'fade': props.modelValue, |
40 |
| - })) |
41 |
| -
|
42 |
| - let _countDownTimeout: number | undefined = 0 |
43 |
| -
|
44 |
| - const parseCountDown = (value: boolean | number) => { |
45 |
| - if (typeof value === 'boolean') { |
46 |
| - return 0 |
47 |
| - } |
48 |
| -
|
49 |
| - const numberValue = toInteger(value, 0) |
50 |
| - return numberValue > 0 ? numberValue : 0 |
51 |
| - } |
52 |
| -
|
53 |
| - const clearCountDownInterval = () => { |
54 |
| - if (_countDownTimeout === undefined) return |
55 |
| - clearTimeout(_countDownTimeout) |
56 |
| - _countDownTimeout = undefined |
57 |
| - } |
58 |
| -
|
59 |
| - const countDown = ref(parseCountDown(props.modelValue)) |
60 |
| - const isAlertVisible = computed(() => props.modelValue || props.show) |
61 |
| -
|
62 |
| - onBeforeUnmount(() => { |
63 |
| - clearCountDownInterval() |
64 |
| - instance.value?.dispose() |
65 |
| - instance.value = undefined |
66 |
| - }) |
67 |
| -
|
68 |
| - const parsedModelValue = computed(() => { |
69 |
| - if (props.modelValue === true) { |
70 |
| - return true |
71 |
| - } |
72 |
| - if (props.modelValue === false) return false |
73 |
| -
|
74 |
| - if (toInteger(props.modelValue, 0) < 1) { |
75 |
| - // Boolean will always return false for the above comparison |
76 |
| - return false |
77 |
| - } |
78 |
| - return !!props.modelValue |
79 |
| - }) |
80 |
| -
|
81 |
| - const handleShowAndModelChanged = () => { |
82 |
| - countDown.value = parseCountDown(props.modelValue) |
83 |
| - if ((parsedModelValue.value || props.show) && !instance.value) |
84 |
| - instance.value = new Alert(element.value as HTMLElement) |
85 |
| - } |
86 |
| -
|
87 |
| - const dismissClicked = () => { |
88 |
| - if (typeof props.modelValue === 'boolean') { |
89 |
| - emit('update:modelValue', false) |
90 |
| - } else { |
91 |
| - emit('update:modelValue', 0) |
92 |
| - } |
93 |
| - emit('dismissed') |
94 |
| - } |
95 |
| -
|
96 |
| - watch(() => props.modelValue, handleShowAndModelChanged) |
97 |
| - watch(() => props.show, handleShowAndModelChanged) |
98 |
| -
|
99 |
| - watch(countDown, (newValue) => { |
100 |
| - clearCountDownInterval() |
101 |
| - if (typeof props.modelValue === 'boolean') return |
102 |
| - emit('dismiss-count-down', newValue) |
103 |
| -
|
104 |
| - if (newValue === 0 && props.modelValue > 0) emit('dismissed') |
105 |
| - if (props.modelValue !== newValue) emit('update:modelValue', newValue) |
106 |
| - if (newValue > 0) { |
107 |
| - _countDownTimeout = setTimeout(() => { |
108 |
| - countDown.value-- |
109 |
| - }, 1000) |
110 |
| - } |
111 |
| - }) |
112 |
| - return { |
113 |
| - dismissClicked, |
114 |
| - isAlertVisible, |
115 |
| - element, |
116 |
| - classes, |
117 |
| - } |
118 |
| - }, |
| 20 | +import {toInteger} from '@/utils/number' |
| 21 | +
|
| 22 | +interface BAlertProps { |
| 23 | + dismissLabel?: string |
| 24 | + dismissible?: boolean |
| 25 | + fade?: boolean |
| 26 | + modelValue?: boolean | number |
| 27 | + show?: boolean |
| 28 | + variant?: ColorVariant |
| 29 | +} |
| 30 | +
|
| 31 | +const props = withDefaults(defineProps<BAlertProps>(), { |
| 32 | + dismissLabel: 'Close', |
| 33 | + dismissible: false, |
| 34 | + fade: false, |
| 35 | + modelValue: false, |
| 36 | + show: false, |
| 37 | + variant: 'info', |
| 38 | +}) |
| 39 | +
|
| 40 | +interface BAlertEmits { |
| 41 | + (e: 'dismissed'): void |
| 42 | + (e: 'dismiss-count-down', value: number): void |
| 43 | + (e: 'update:modelValue', value: boolean | number): void |
| 44 | +} |
| 45 | +
|
| 46 | +const emit = defineEmits<BAlertEmits>() |
| 47 | +
|
| 48 | +const element = ref<HTMLElement>() |
| 49 | +const instance = ref<Alert>() |
| 50 | +const classes = computed(() => ({ |
| 51 | + [`alert-${props.variant}`]: props.variant, |
| 52 | + 'show': props.modelValue, |
| 53 | + 'alert-dismissible': props.dismissible, |
| 54 | + 'fade': props.modelValue, |
| 55 | +})) |
| 56 | +
|
| 57 | +let _countDownTimeout: undefined | ReturnType<typeof setTimeout> |
| 58 | +
|
| 59 | +const parseCountDown = (value: boolean | number): number => { |
| 60 | + if (typeof value === 'boolean') { |
| 61 | + return 0 |
| 62 | + } |
| 63 | +
|
| 64 | + const numberValue = toInteger(value, 0) |
| 65 | + return numberValue > 0 ? numberValue : 0 |
| 66 | +} |
| 67 | +
|
| 68 | +const clearCountDownInterval = (): void => { |
| 69 | + if (_countDownTimeout === undefined) return |
| 70 | + clearTimeout(_countDownTimeout) |
| 71 | + _countDownTimeout = undefined |
| 72 | +} |
| 73 | +
|
| 74 | +const countDown = ref<number>(parseCountDown(props.modelValue)) |
| 75 | +const isAlertVisible = computed<boolean>(() => !!props.modelValue || props.show) |
| 76 | +
|
| 77 | +onBeforeUnmount((): void => { |
| 78 | + clearCountDownInterval() |
| 79 | + instance.value?.dispose() |
| 80 | + instance.value = undefined |
| 81 | +}) |
| 82 | +
|
| 83 | +const parsedModelValue = computed<boolean>(() => { |
| 84 | + if (props.modelValue === true) { |
| 85 | + return true |
| 86 | + } |
| 87 | + if (props.modelValue === false) return false |
| 88 | +
|
| 89 | + if (toInteger(props.modelValue, 0) < 1) { |
| 90 | + // Boolean will always return false for the above comparison |
| 91 | + return false |
| 92 | + } |
| 93 | + return !!props.modelValue |
| 94 | +}) |
| 95 | +
|
| 96 | +const handleShowAndModelChanged = (): void => { |
| 97 | + countDown.value = parseCountDown(props.modelValue) |
| 98 | + if ((parsedModelValue.value || props.show) && !instance.value) |
| 99 | + instance.value = new Alert(element.value as HTMLElement) |
| 100 | +} |
| 101 | +
|
| 102 | +const dismissClicked = (): void => { |
| 103 | + if (typeof props.modelValue === 'boolean') { |
| 104 | + emit('update:modelValue', false) |
| 105 | + } else { |
| 106 | + emit('update:modelValue', 0) |
| 107 | + } |
| 108 | + emit('dismissed') |
| 109 | +} |
| 110 | +
|
| 111 | +watch(() => props.modelValue, handleShowAndModelChanged) |
| 112 | +watch(() => props.show, handleShowAndModelChanged) |
| 113 | +
|
| 114 | +watch(countDown, (newValue) => { |
| 115 | + clearCountDownInterval() |
| 116 | + if (typeof props.modelValue === 'boolean') return |
| 117 | + emit('dismiss-count-down', newValue) |
| 118 | +
|
| 119 | + if (newValue === 0 && props.modelValue > 0) emit('dismissed') |
| 120 | + if (props.modelValue !== newValue) emit('update:modelValue', newValue) |
| 121 | + if (newValue > 0) { |
| 122 | + _countDownTimeout = setTimeout(() => { |
| 123 | + countDown.value-- |
| 124 | + }, 1000) |
| 125 | + } |
119 | 126 | })
|
120 | 127 | </script>
|
0 commit comments