Skip to content

Commit 2e92a66

Browse files
authored
Merge pull request #735 from VividLemon/master
fix(BTransition): allowTransProps to work
2 parents c5d69f0 + 06819db commit 2e92a66

File tree

8 files changed

+114
-72
lines changed

8 files changed

+114
-72
lines changed

packages/bootstrap-vue-3/src/components/BPlaceholder/BPlaceholder.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<script setup lang="ts">
66
import {computed, StyleValue} from 'vue'
7-
import {ColorVariant, PlaceholderAnimation, PlaceholderSize} from '../../types'
7+
import type {ColorVariant, PlaceholderAnimation, PlaceholderSize} from '../../types'
88
99
interface Props {
1010
tag?: string

packages/bootstrap-vue-3/src/components/BPlaceholder/BPlaceholderCard.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import BCard from '../BCard/BCard.vue'
3232
import BCardImg from '../BCard/BCardImg.vue'
3333
import BPlaceholder from './BPlaceholder.vue'
3434
import BPlaceholderButton from './BPlaceholderButton.vue'
35-
import {Booleanish, ColorVariant, PlaceholderAnimation, PlaceholderSize} from '../../types'
35+
import type {Booleanish, ColorVariant, PlaceholderAnimation, PlaceholderSize} from '../../types'
3636
import {computed, toRef} from 'vue'
3737
import {useBooleanish} from '../../composables'
3838
Lines changed: 55 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,64 @@
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'
3-
import {isPlainObject} from '../../utils'
4-
import {defineComponent, h, PropType, ref, toRef, Transition} from 'vue'
9+
import {computed, toRef} from 'vue'
510
import {useBooleanish} from '../../composables'
611
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',
12+
interface Props {
13+
appear?: Booleanish
14+
mode?: TransitionMode
15+
noFade?: Booleanish
16+
transProps?: Record<string, unknown>
2117
}
2218
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'))
19+
const props = withDefaults(defineProps<Props>(), {
20+
appear: false,
21+
noFade: false,
22+
})
3323
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-
}
24+
const appearBoolean = useBooleanish(toRef(props, 'appear'))
25+
const noFadeBoolean = useBooleanish(toRef(props, 'noFade'))
5426
55-
return () =>
56-
h(
57-
Transition,
58-
{...transProperties.value},
59-
{
60-
default: () => (slots.default ? slots.default() : []),
61-
}
62-
)
63-
},
27+
const fadeProperties = computed(() => {
28+
const NO_FADE_PROPS = {
29+
name: '',
30+
enterActiveClass: '',
31+
enterToClass: '',
32+
leaveActiveClass: '',
33+
leaveToClass: 'showing',
34+
enterFromClass: 'showing',
35+
leaveFromClass: '',
36+
}
37+
const FADE_PROPS = {
38+
...NO_FADE_PROPS,
39+
enterActiveClass: 'fade showing',
40+
leaveActiveClass: 'fade showing',
41+
}
42+
return noFadeBoolean.value ? NO_FADE_PROPS : FADE_PROPS
6443
})
44+
45+
const baseProperties = computed(() => ({mode: props.mode, css: true, ...fadeProperties.value}))
46+
47+
const computedAttrs = computed(() =>
48+
props.transProps !== undefined
49+
? {
50+
// Order matters here since the props.transProps would get overwritten if it came first
51+
// But the goal of props.transProps is to overwrite base properties
52+
...baseProperties.value,
53+
...props.transProps,
54+
}
55+
: appearBoolean.value
56+
? {
57+
...baseProperties.value,
58+
appear: true,
59+
appearActiveClass: fadeProperties.value.enterActiveClass,
60+
appearToClass: fadeProperties.value.enterToClass,
61+
}
62+
: baseProperties.value
63+
)
6564
</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
})

packages/bootstrap-vue-3/src/utils/cssEscape.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ const escapeChar = (value: string) => `\\${value}`
55
/**
66
* The `cssEscape()` util is based on this `CSS.escape()` polyfill: https://github.com/mathiasbynens/CSS.escape
77
*
8-
* @param {any} value
8+
* @param {unknown} value
99
*/
10-
export default (value: any): string => {
11-
value = toString(value)
10+
export default (value: unknown): string => {
11+
const val = toString(value)
1212

13-
const {length} = value
14-
const firstCharCode = value.charCodeAt(0)
13+
const {length} = val
14+
const firstCharCode = val.charCodeAt(0)
1515

16-
return value.split('').reduce((result: string, char: string, index: number) => {
17-
const charCode = value.charCodeAt(index)
16+
return val.split('').reduce((result: string, char: string, index: number) => {
17+
const charCode = val.charCodeAt(index)
1818

1919
// If the character is NULL (U+0000), use (U+FFFD) as replacement
2020
if (charCode === 0x0000) {

packages/bootstrap-vue-3/src/utils/inspect.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
* @param obj
33
* @returns
44
*/
5-
export const isObject = (obj: unknown): boolean => obj !== null && typeof obj === 'object'
5+
export const isObject = (obj: unknown): obj is Record<PropertyKey, unknown> =>
6+
obj !== null && typeof obj === 'object'
67

78
/**
89
* @param value
@@ -16,5 +17,5 @@ export const isNumeric = (value: unknown): boolean => /^[0-9]*\.?[0-9]+$/.test(S
1617
* @param obj
1718
* @returns
1819
*/
19-
export const isPlainObject = (obj: unknown): boolean =>
20+
export const isPlainObject = (obj: unknown): obj is Record<PropertyKey, unknown> =>
2021
Object.prototype.toString.call(obj) === '[object Object]'

packages/bootstrap-vue-3/src/utils/props.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Suffix can be a falsey value so nothing is appended to string
22
// (helps when looping over props & some shouldn't change)
33

4-
import {upperFirst} from '.'
4+
import {upperFirst} from './stringUtils'
55

66
/**
77
* Use data last parameters to allow for currying

packages/bootstrap-vue-3/src/utils/stringUtils.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ export const toString = (val: unknown, spaces = 2): string =>
1818
? val
1919
: val === undefined || val === null
2020
? ''
21-
: Array.isArray(val) ||
22-
(isPlainObject(val) &&
23-
(val as Record<string, unknown>).toString === Object.prototype.toString)
21+
: Array.isArray(val) || (isPlainObject(val) && val.toString === Object.prototype.toString)
2422
? JSON.stringify(val, null, spaces)
2523
: String(val)
2624

0 commit comments

Comments
 (0)