Skip to content

Commit dd17957

Browse files
committed
fix(BTransition): allowTransProps to work
fix: transProps working in BTransition cascades fixes for components that rely on it, ex: Overlay noFade perf(BTransition): replace ref with cached computed properties refactor(BTransition): use script setup syntax and templates test: transition.spec.ts not done
1 parent db032f8 commit dd17957

File tree

2 files changed

+100
-56
lines changed

2 files changed

+100
-56
lines changed
Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,65 @@
1-
<script lang="ts">
1+
<template>
2+
<Transition v-bind="computedAttrs">
3+
<slot />
4+
</Transition>
5+
</template>
6+
7+
<script setup lang="ts">
28
import type {Booleanish, TransitionMode} from '../../types'
39
import {isPlainObject} from '../../utils'
4-
import {defineComponent, h, PropType, ref, toRef, Transition} from 'vue'
10+
import {computed, toRef} from 'vue'
511
import {useBooleanish} from '../../composables'
612
7-
const NO_FADE_PROPS = {
8-
name: '',
9-
enterActiveClass: '',
10-
enterToClass: '',
11-
leaveActiveClass: '',
12-
leaveToClass: 'showing',
13-
enterFromClass: 'showing',
14-
leaveFromClass: '',
15-
}
16-
17-
const FADE_PROPS = {
18-
...NO_FADE_PROPS,
19-
enterActiveClass: 'fade showing',
20-
leaveActiveClass: 'fade showing',
13+
interface Props {
14+
appear?: Booleanish
15+
mode?: TransitionMode
16+
noFade?: Booleanish
17+
transProps?: Record<string, unknown>
2118
}
2219
23-
export default defineComponent({
24-
props: {
25-
appear: {type: [Boolean, String] as PropType<Booleanish>, default: false},
26-
mode: {type: String as PropType<TransitionMode>, required: false},
27-
noFade: {type: [Boolean, String] as PropType<Booleanish>, default: false},
28-
transProps: {type: Object, required: false},
29-
},
30-
setup(props, {slots}) {
31-
const appearBoolean = useBooleanish(toRef(props, 'appear'))
32-
const noFadeBoolean = useBooleanish(toRef(props, 'noFade'))
20+
const props = withDefaults(defineProps<Props>(), {
21+
appear: false,
22+
noFade: false,
23+
})
3324
34-
const transProperties = ref(props.transProps)
35-
if (!isPlainObject(transProperties.value)) {
36-
transProperties.value = noFadeBoolean.value ? NO_FADE_PROPS : FADE_PROPS
37-
if (appearBoolean.value) {
38-
// Default the appear classes to equal the enter classes
39-
transProperties.value = {
40-
...transProperties.value,
41-
appear: true,
42-
appearClass: transProperties.value.enterClass,
43-
appearActiveClass: transProperties.value.enterActiveClass,
44-
appearToClass: transProperties.value.enterToClass,
45-
}
46-
}
47-
}
48-
transProperties.value = {
49-
mode: props.mode,
50-
...transProperties.value,
51-
// We always need `css` true
52-
css: true,
53-
}
25+
const appearBoolean = useBooleanish(toRef(props, 'appear'))
26+
const noFadeBoolean = useBooleanish(toRef(props, 'noFade'))
5427
55-
return () =>
56-
h(
57-
Transition,
58-
{...transProperties.value},
59-
{
60-
default: () => (slots.default ? slots.default() : []),
61-
}
62-
)
63-
},
28+
const fadeProperties = computed(() => {
29+
const NO_FADE_PROPS = {
30+
name: '',
31+
enterActiveClass: '',
32+
enterToClass: '',
33+
leaveActiveClass: '',
34+
leaveToClass: 'showing',
35+
enterFromClass: 'showing',
36+
leaveFromClass: '',
37+
}
38+
const FADE_PROPS = {
39+
...NO_FADE_PROPS,
40+
enterActiveClass: 'fade showing',
41+
leaveActiveClass: 'fade showing',
42+
}
43+
return noFadeBoolean.value ? NO_FADE_PROPS : FADE_PROPS
6444
})
45+
46+
const baseProperties = computed(() => ({mode: props.mode, css: true, ...fadeProperties.value}))
47+
48+
const computedAttrs = computed(() =>
49+
isPlainObject(props.transProps)
50+
? {
51+
// Order matters here since the props.transProps would get overwritten if it came first
52+
// But the goal of props.transProps is to overwrite base properties
53+
...baseProperties.value,
54+
...props.transProps,
55+
}
56+
: appearBoolean.value
57+
? {
58+
...baseProperties.value,
59+
appear: true,
60+
appearActiveClass: fadeProperties.value.enterActiveClass,
61+
appearToClass: fadeProperties.value.enterToClass,
62+
}
63+
: baseProperties.value
64+
)
6565
</script>

packages/bootstrap-vue-3/src/components/BTransition/transition.spec.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {enableAutoUnmount, mount} from '@vue/test-utils'
22
import {afterEach, describe, expect, it} from 'vitest'
33
import BTransition from './BTransition.vue'
4-
import {Transition} from 'vue'
4+
import {Component, Transition} from 'vue'
55

66
describe('transition', () => {
77
enableAutoUnmount(afterEach)
@@ -23,4 +23,48 @@ describe('transition', () => {
2323
const $transition = wrapper.findComponent(Transition)
2424
expect($transition.exists()).toBe(true)
2525
})
26+
27+
it('Transition has prop css true when prop transProps', () => {
28+
const wrapper = mount(BTransition, {
29+
props: {transProps: {enterActiveClass: 'foo'}},
30+
})
31+
const $transition = wrapper.getComponent(Transition as Component)
32+
expect($transition.props('css')).toBe(true)
33+
})
34+
35+
it('Transition has mode undefined by default when prop transProps', () => {
36+
const wrapper = mount(BTransition, {
37+
props: {transProps: {enterActiveClass: 'foo'}},
38+
})
39+
const $transition = wrapper.getComponent(Transition as Component)
40+
expect($transition.props('mode')).toBeUndefined()
41+
})
42+
43+
it('Transition has mode to be prop mode when prop transProps', () => {
44+
const wrapper = mount(BTransition, {
45+
props: {transProps: {enterActiveClass: 'foo', mode: 'in-out'}},
46+
})
47+
const $transition = wrapper.getComponent(Transition as Component)
48+
expect($transition.props('mode')).toBe('in-out')
49+
})
50+
51+
it('Transition has props from transProps', () => {
52+
const wrapper = mount(BTransition, {
53+
props: {transProps: {enterActiveClass: 'foo'}},
54+
})
55+
const $transition = wrapper.getComponent(Transition as Component)
56+
expect($transition.props('enterActiveClass')).toBe('foo')
57+
})
58+
59+
// Test for when appear is true but trans props is also true
60+
61+
// Test for when appear is false but trans props is also true
62+
63+
// Test for when appear is true
64+
65+
// Test for when appear is false
66+
67+
// Test for when appear is true but fade is also true
68+
69+
// Test for when appear is true but fade is false
2670
})

0 commit comments

Comments
 (0)