|
1 | 1 | import type { ComponentOptionsMixin, ComputedOptions, EmitsOptions, MethodOptions } from 'vue'; |
2 | | -import { computed, defineComponent, inject, nextTick, onBeforeUpdate, onMounted, ref, shallowRef, watch } from 'vue'; |
3 | | -import { useCurrentRoute, useHasLink, useHasRouter } from '../../mixins/CommonApi'; |
4 | | -import type { IListItem, IListViewProvider, TBsListTile, TListTileOptionProps, TRecord } from '../../types'; |
| 2 | +import { |
| 3 | + computed, |
| 4 | + defineComponent, |
| 5 | + inject, |
| 6 | + nextTick, |
| 7 | + onBeforeUpdate, |
| 8 | + onMounted, |
| 9 | + ref, |
| 10 | + shallowRef, |
| 11 | + watch, |
| 12 | +} from 'vue'; |
| 13 | +import { useCurrentRoute, useHasLink, useHasRouter, useIsRouteMatch } from '../../mixins/CommonApi'; |
| 14 | +import type { |
| 15 | + IListItem, |
| 16 | + IListViewProvider, |
| 17 | + TBsListTile, |
| 18 | + TListTileOptionProps, |
| 19 | + TRecord, |
| 20 | +} from '../../types'; |
5 | 21 | import { useListTileClassNames, useRenderListTile } from './mixins/listTileApi'; |
6 | 22 | import { listTileProps } from './mixins/listViewProps'; |
7 | 23 |
|
8 | | -export default defineComponent<TBsListTile, TRecord, TRecord, ComputedOptions, MethodOptions, ComponentOptionsMixin, ComponentOptionsMixin, EmitsOptions>({ |
| 24 | +export default defineComponent< |
| 25 | + TBsListTile, |
| 26 | + TRecord, |
| 27 | + TRecord, |
| 28 | + ComputedOptions, |
| 29 | + MethodOptions, |
| 30 | + ComponentOptionsMixin, |
| 31 | + ComponentOptionsMixin, |
| 32 | + EmitsOptions |
| 33 | +>({ |
9 | 34 | name: 'BsListTile', |
10 | 35 | props: listTileProps, |
11 | 36 | emits: [ |
12 | 37 | 'click', |
13 | 38 | /** |
14 | 39 | * Fired when this component's state is updated. |
15 | 40 | */ |
16 | | - 'update:active' |
| 41 | + 'update:active', |
17 | 42 | ], |
18 | | - setup(props, {emit, expose, slots}) { |
| 43 | + setup(props, { emit, expose, slots }) { |
19 | 44 | const thisProps = props as Readonly<TListTileOptionProps>; |
20 | 45 | const vm = shallowRef<IListItem>(); |
21 | 46 | const isActive = ref<boolean | undefined>(thisProps.active); |
22 | 47 | const hasRouter = ref<boolean>(useHasRouter(thisProps)); |
23 | 48 | const hasLink = ref<boolean>(useHasLink(thisProps)); |
24 | 49 |
|
25 | | - expose({isActive}); |
| 50 | + expose({ isActive }); |
26 | 51 |
|
27 | 52 | const provider = inject<IListViewProvider>('ListView'); |
28 | | - const tagName = computed<string>( |
29 | | - () => hasRouter.value || hasLink.value ? 'a' : 'div' |
30 | | - ); |
31 | | - const tileClasses = computed<TRecord>( |
32 | | - () => useListTileClassNames(tagName.value, thisProps, isActive, hasLink, provider) |
| 53 | + const tagName = computed<string>(() => (hasRouter.value || hasLink.value ? 'a' : 'div')); |
| 54 | + const tileClasses = computed<TRecord>(() => |
| 55 | + useListTileClassNames(tagName.value, thisProps, isActive, hasLink, provider) |
33 | 56 | ); |
34 | 57 |
|
35 | 58 | watch( |
36 | 59 | () => thisProps.active, |
37 | 60 | (value) => { |
38 | 61 | if (provider && provider.config.individualState === true) { |
39 | | - isActive.value = value |
| 62 | + isActive.value = value; |
40 | 63 | } |
41 | 64 | } |
42 | 65 | ); |
43 | | - onBeforeUpdate( |
44 | | - () => { |
45 | | - if (hasRouter.value) { |
46 | | - const route = useCurrentRoute(); |
47 | | - if (route && route.value.path === thisProps.path) { |
48 | | - isActive.value = true; |
49 | | - nextTick().then(() => { |
50 | | - emit('update:active', true); |
51 | | - }); |
52 | | - } |
| 66 | + onBeforeUpdate(() => { |
| 67 | + if (hasRouter.value) { |
| 68 | + const route = useCurrentRoute(); |
| 69 | + if (route && useIsRouteMatch(route, thisProps)) { |
| 70 | + isActive.value = true; |
| 71 | + nextTick().then(() => { |
| 72 | + emit('update:active', true); |
| 73 | + }); |
53 | 74 | } |
54 | 75 | } |
55 | | - ); |
| 76 | + }); |
56 | 77 | onMounted(() => { |
57 | 78 | hasRouter.value = useHasRouter(thisProps); |
58 | 79 | hasLink.value = useHasLink(thisProps); |
59 | 80 | }); |
60 | 81 |
|
61 | 82 | return () => |
62 | | - useRenderListTile(tagName.value, slots, emit, thisProps, tileClasses, vm, provider) |
63 | | - } |
| 83 | + useRenderListTile(tagName.value, slots, emit, thisProps, tileClasses, vm, provider); |
| 84 | + }, |
64 | 85 | }); |
0 commit comments