Skip to content

Commit d8d4ca6

Browse files
Kingwlyyx990803
authored andcommitted
fix: transition group should work with dynamic name (#6006) (#6019)
* fix: transition group should work with dynamic name (#6006) * fix: improve remove class
1 parent 8ff77a2 commit d8d4ca6

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

Diff for: src/platforms/web/runtime/class-util.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,20 @@ export function removeClass (el: HTMLElement, cls: ?string) {
4242
} else {
4343
el.classList.remove(cls)
4444
}
45+
if (!el.classList.length) {
46+
el.removeAttribute('class')
47+
}
4548
} else {
4649
let cur = ` ${el.getAttribute('class') || ''} `
4750
const tar = ' ' + cls + ' '
4851
while (cur.indexOf(tar) >= 0) {
4952
cur = cur.replace(tar, ' ')
5053
}
51-
el.setAttribute('class', cur.trim())
54+
cur = cur.trim()
55+
if (cur) {
56+
el.setAttribute('class', cur)
57+
} else {
58+
el.removeAttribute('class')
59+
}
5260
}
5361
}

Diff for: src/platforms/web/runtime/components/transition-group.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ export default {
127127
if (!hasTransition) {
128128
return false
129129
}
130-
if (this._hasMove != null) {
130+
if (this._hasMove) {
131131
return this._hasMove
132132
}
133133
// Detect whether an element with the move class applied has

Diff for: test/unit/features/transition/transition-group.spec.js

+47
Original file line numberDiff line numberDiff line change
@@ -293,5 +293,52 @@ if (!isIE9) {
293293
}).$mount()
294294
expect('<transition-group> children must be keyed: <div>').toHaveBeenWarned()
295295
})
296+
297+
// Github issue #6006
298+
it('should work with dynamic name', done => {
299+
const vm = new Vue({
300+
template: `
301+
<div>
302+
<transition-group :name="name">
303+
<div v-for="item in items" :key="item">{{ item }}</div>
304+
</transition-group>
305+
</div>
306+
`,
307+
data: {
308+
items: ['a', 'b', 'c'],
309+
name: 'group'
310+
}
311+
}).$mount(el)
312+
313+
vm.name = 'invalid-name'
314+
vm.items = ['b', 'c', 'a']
315+
waitForUpdate(() => {
316+
expect(vm.$el.innerHTML.replace(/\s?style=""(\s?)/g, '$1')).toBe(
317+
`<span>` +
318+
`<div>b</div>` +
319+
`<div>c</div>` +
320+
`<div>a</div>` +
321+
`</span>`
322+
)
323+
vm.name = 'group'
324+
vm.items = ['a', 'b', 'c']
325+
}).thenWaitFor(nextFrame).then(() => {
326+
expect(vm.$el.innerHTML.replace(/\s?style=""(\s?)/g, '$1')).toBe(
327+
`<span>` +
328+
`<div class="group-move">a</div>` +
329+
`<div class="group-move">b</div>` +
330+
`<div class="group-move">c</div>` +
331+
`</span>`
332+
)
333+
}).thenWaitFor(duration * 2 + buffer).then(() => {
334+
expect(vm.$el.innerHTML.replace(/\s?style=""(\s?)/g, '$1')).toBe(
335+
`<span>` +
336+
`<div>a</div>` +
337+
`<div>b</div>` +
338+
`<div>c</div>` +
339+
`</span>`
340+
)
341+
}).then(done)
342+
})
296343
})
297344
}

Diff for: test/unit/features/transition/transition.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ if (!isIE9) {
437437
expect(enterSpy).toHaveBeenCalled()
438438
expect(vm.$el.innerHTML).toBe('<div class="nope-enter nope-enter-active">foo</div>')
439439
}).thenWaitFor(nextFrame).then(() => {
440-
expect(vm.$el.innerHTML).toMatch(/<div( class="")?>foo<\/div>/)
440+
expect(vm.$el.innerHTML).toBe('<div>foo</div>')
441441
}).then(done)
442442
})
443443

0 commit comments

Comments
 (0)