|
7 | 7 | :aria-disabled="disabled ? true : null"
|
8 | 8 | :target="link ? target : null"
|
9 | 9 | :href="!button ? href : null"
|
10 |
| - v-bind="attrs" |
| 10 | + v-bind="computedAttrs" |
11 | 11 | >
|
12 | 12 | <slot />
|
13 | 13 | </component>
|
14 | 14 | </template>
|
15 | 15 |
|
16 |
| -<script lang="ts"> |
17 |
| -import {computed, defineComponent, inject, PropType} from 'vue' |
18 |
| -import {ColorVariant} from '../../types' |
| 16 | +<script setup lang="ts"> |
| 17 | +// import type {BListGroupItemProps} from '@/types/components' |
| 18 | +import {computed, inject, useAttrs} from 'vue' |
| 19 | +import type {ColorVariant, LinkTarget} from '@/types' |
19 | 20 | import {injectionKey} from './BListGroup.vue'
|
20 | 21 |
|
21 |
| -const ACTION_TAGS = ['a', 'router-link', 'button', 'b-link'] |
| 22 | +interface BListGroupItemProps { |
| 23 | + action?: boolean |
| 24 | + active?: boolean |
| 25 | + // activeClass: string |
| 26 | + // append?: boolean |
| 27 | + button?: boolean |
| 28 | + disabled?: boolean |
| 29 | + // exact?: boolean |
| 30 | + // exactActiveClass: string |
| 31 | + href?: string |
| 32 | + // noPrefetch?: Boolean |
| 33 | + // prefetch?: Boolean |
| 34 | + // rel?: String |
| 35 | + // replace?: Boolean |
| 36 | + // routerComponentName?: String |
| 37 | + tag?: string |
| 38 | + target?: LinkTarget |
| 39 | + //to: string | Record<string, unknown> |
| 40 | + variant?: ColorVariant |
| 41 | +} |
22 | 42 |
|
23 |
| -export default defineComponent({ |
24 |
| - name: 'BListGroupItem', |
25 |
| - props: { |
26 |
| - action: {type: Boolean, default: false}, |
27 |
| - active: {type: Boolean, default: false}, |
28 |
| - // activeClass: {type: String}, |
29 |
| - // append: {type: Boolean, default: false}, |
30 |
| - button: {type: Boolean, default: false}, |
31 |
| - disabled: {type: Boolean, default: false}, |
32 |
| - // exact: {type: Boolean, default: false}, |
33 |
| - // exactActiveClass: {type: String}, |
34 |
| - href: {type: String}, |
35 |
| - // noPrefetch: {type: Boolean, default: false}, |
36 |
| - // prefetch: {type: Boolean, default: null}, |
37 |
| - // rel: {type: String, default: null}, |
38 |
| - // replace: {type: Boolean, default: false}, |
39 |
| - // routerComponentName: {type: String, default: null}, |
40 |
| - tag: {type: String, default: 'div'}, |
41 |
| - target: {type: String, default: '_self'}, |
42 |
| - //to: {type: [String, Object]}, |
43 |
| - variant: {type: String as PropType<ColorVariant>}, |
44 |
| - }, |
45 |
| - setup(props, context) { |
46 |
| - const parentData = inject(injectionKey, null) |
| 43 | +const props = withDefaults(defineProps<BListGroupItemProps>(), { |
| 44 | + action: false, |
| 45 | + active: false, |
| 46 | + button: false, |
| 47 | + disabled: false, |
| 48 | + tag: 'div', |
| 49 | + target: '_self', |
| 50 | +}) |
| 51 | +
|
| 52 | +const attrs = useAttrs() |
47 | 53 |
|
48 |
| - const link = computed(() => !props.button && props.href) |
49 |
| - const tagComputed = computed( |
50 |
| - () => |
51 |
| - parentData?.numbered |
52 |
| - ? 'li' |
53 |
| - : props.button |
54 |
| - ? 'button' |
55 |
| - : !link.value |
56 |
| - ? props.tag |
57 |
| - : 'a' /* BLink */ |
58 |
| - ) |
| 54 | +const parentData = inject(injectionKey, null) |
59 | 55 |
|
60 |
| - const classes = computed(() => { |
61 |
| - const action = props.action || link.value || props.button || ACTION_TAGS.includes(props.tag) |
62 |
| - return { |
63 |
| - [`list-group-item-${props.variant}`]: props.variant, |
64 |
| - 'list-group-item-action': action, |
65 |
| - 'active': props.active, |
66 |
| - 'disabled': props.disabled, |
67 |
| - } |
68 |
| - }) |
| 56 | +const link = computed<boolean>(() => !props.button && !!props.href) |
69 | 57 |
|
70 |
| - const attrs = computed(() => { |
71 |
| - const attrs = {} as {type?: string; disabled?: boolean} |
72 |
| - if (props.button) { |
73 |
| - if (!context.attrs || !context.attrs.type) { |
74 |
| - // Add a type for button is one not provided in passed attributes |
75 |
| - attrs.type = 'button' |
76 |
| - } |
77 |
| - if (props.disabled) { |
78 |
| - // Set disabled attribute if button and disabled |
79 |
| - attrs.disabled = true |
80 |
| - } |
81 |
| - } |
82 |
| - return attrs |
83 |
| - }) |
| 58 | +const tagComputed = computed<string>( |
| 59 | + () => |
| 60 | + parentData?.numbered |
| 61 | + ? 'li' |
| 62 | + : props.button |
| 63 | + ? 'button' |
| 64 | + : !link.value |
| 65 | + ? props.tag |
| 66 | + : 'a' /* BLink */ |
| 67 | +) |
84 | 68 |
|
85 |
| - return { |
86 |
| - tagComputed, |
87 |
| - classes, |
88 |
| - attrs, |
89 |
| - link, |
| 69 | +const classes = computed(() => { |
| 70 | + const action = |
| 71 | + props.action || |
| 72 | + link.value || |
| 73 | + props.button || |
| 74 | + ['a', 'router-link', 'button', 'b-link'].includes(props.tag) |
| 75 | + return { |
| 76 | + [`list-group-item-${props.variant}`]: props.variant, |
| 77 | + 'list-group-item-action': action, |
| 78 | + 'active': props.active, |
| 79 | + 'disabled': props.disabled, |
| 80 | + } |
| 81 | +}) |
| 82 | +
|
| 83 | +const computedAttrs = computed(() => { |
| 84 | + const localAttrs = {} as {type?: string; disabled?: boolean} |
| 85 | + if (props.button) { |
| 86 | + if (!attrs || !attrs.type) { |
| 87 | + // Add a type for button is one not provided in passed attributes |
| 88 | + localAttrs.type = 'button' |
| 89 | + } |
| 90 | + if (props.disabled) { |
| 91 | + // Set disabled attribute if button and disabled |
| 92 | + localAttrs.disabled = true |
90 | 93 | }
|
91 |
| - }, |
| 94 | + } |
| 95 | + return localAttrs |
92 | 96 | })
|
93 | 97 | </script>
|
0 commit comments