🌐 AI搜索 & 代理 主页
Skip to content

Commit 31eeb0a

Browse files
Hiwsjacobmllr95
andauthored
fix(b-tooltip, b-popover): fix title not being reset on hide (#5793)
* fix bug * add test * update tooltip.spec.js * Update bv-tooltip.js * Update bv-tooltip.js Co-authored-by: Jacob Müller <jacob.mueller.elz@gmail.com>
1 parent 107cd14 commit 31eeb0a

File tree

2 files changed

+99
-6
lines changed

2 files changed

+99
-6
lines changed

src/components/tooltip/helpers/bv-tooltip.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -621,19 +621,31 @@ export const BVTooltip = /*#__PURE__*/ Vue.extend({
621621
}
622622
},
623623
fixTitle() {
624-
// If the target has a `title` attribute, remove it and store it on a data attribute
624+
// If the target has a `title` attribute,
625+
// remove it and store it on a data attribute
625626
const target = this.getTarget()
626-
if (target && hasAttr(target, 'title')) {
627-
setAttr(target, DATA_TITLE_ATTR, getAttr(target, 'title') || '')
627+
if (hasAttr(target, 'title')) {
628+
// Get `title` attribute value and remove it from target
629+
const title = getAttr(target, 'title')
628630
setAttr(target, 'title', '')
631+
// Only set the data attribute when the value is truthy
632+
if (title) {
633+
setAttr(target, DATA_TITLE_ATTR, title)
634+
}
629635
}
630636
},
631637
restoreTitle() {
632-
// If the target had a title, restore it and remove the data attribute
638+
// If the target had a `title` attribute,
639+
// restore it and remove the data attribute
633640
const target = this.getTarget()
634-
if (target && hasAttr(target, DATA_TITLE_ATTR)) {
635-
setAttr(target, 'title', getAttr(target, DATA_TITLE_ATTR) || '')
641+
if (hasAttr(target, DATA_TITLE_ATTR)) {
642+
// Get data attribute value and remove it from target
643+
const title = getAttr(target, DATA_TITLE_ATTR)
636644
removeAttr(target, DATA_TITLE_ATTR)
645+
// Only restore the `title` attribute when the value is truthy
646+
if (title) {
647+
setAttr(target, 'title', title)
648+
}
637649
}
638650
},
639651
// --- BvEvent helpers ---

src/components/tooltip/tooltip.spec.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,4 +1445,85 @@ describe('b-tooltip', () => {
14451445

14461446
wrapper.destroy()
14471447
})
1448+
1449+
it('saves title in data attribute on open and adds to back on hide', async () => {
1450+
jest.useFakeTimers()
1451+
const wrapper = mount(App, {
1452+
attachTo: createContainer(),
1453+
propsData: {
1454+
triggers: 'click',
1455+
show: false,
1456+
titleAttr: 'bar'
1457+
},
1458+
slots: {
1459+
default: 'title'
1460+
}
1461+
})
1462+
1463+
expect(wrapper.vm).toBeDefined()
1464+
await waitNT(wrapper.vm)
1465+
await waitRAF()
1466+
await waitNT(wrapper.vm)
1467+
await waitRAF()
1468+
jest.runOnlyPendingTimers()
1469+
1470+
expect(wrapper.element.tagName).toBe('ARTICLE')
1471+
expect(wrapper.attributes('id')).toBeDefined()
1472+
expect(wrapper.attributes('id')).toEqual('wrapper')
1473+
1474+
// The trigger button
1475+
const $button = wrapper.find('button')
1476+
expect($button.exists()).toBe(true)
1477+
expect($button.attributes('id')).toBeDefined()
1478+
expect($button.attributes('id')).toEqual('foo')
1479+
expect($button.attributes('title')).toBeDefined()
1480+
expect($button.attributes('title')).toEqual('bar')
1481+
expect($button.attributes('data-original-title')).not.toBeDefined()
1482+
expect($button.attributes('aria-describedby')).not.toBeDefined()
1483+
1484+
// Show tooltip
1485+
await wrapper.setProps({ show: true })
1486+
1487+
expect($button.attributes('title')).toBeDefined()
1488+
expect($button.attributes('title')).toEqual('')
1489+
expect($button.attributes('data-original-title')).toBeDefined()
1490+
expect($button.attributes('data-original-title')).toEqual('bar')
1491+
expect($button.attributes('aria-describedby')).toBeDefined()
1492+
// ID of the tooltip that will be in the body
1493+
const adb = $button.attributes('aria-describedby')
1494+
1495+
// <b-tooltip> wrapper
1496+
const $tipHolder = wrapper.findComponent(BTooltip)
1497+
expect($tipHolder.exists()).toBe(true)
1498+
expect($tipHolder.element.nodeType).toEqual(Node.COMMENT_NODE)
1499+
1500+
// Find the tooltip element in the document
1501+
const tip = document.getElementById(adb)
1502+
expect(tip).not.toBe(null)
1503+
expect(tip).toBeInstanceOf(HTMLElement)
1504+
expect(tip.tagName).toEqual('DIV')
1505+
expect(tip.classList.contains('tooltip')).toBe(true)
1506+
expect(tip.classList.contains('b-tooltip')).toBe(true)
1507+
expect(tip.classList.contains('interactive')).toBe(false)
1508+
expect(tip.textContent).toEqual('title')
1509+
1510+
// Hide the tooltip
1511+
await wrapper.setProps({ show: false })
1512+
await waitRAF()
1513+
await waitRAF()
1514+
jest.runOnlyPendingTimers()
1515+
await waitNT(wrapper.vm)
1516+
await waitRAF()
1517+
1518+
expect($button.attributes('title')).toBeDefined()
1519+
expect($button.attributes('title')).toEqual('bar')
1520+
expect($button.attributes('data-original-title')).not.toBeDefined()
1521+
expect($button.attributes('aria-describedby')).not.toBeDefined()
1522+
1523+
// Tooltip element should not be in the document
1524+
expect(document.body.contains(tip)).toBe(false)
1525+
expect(document.querySelector(adb)).toBe(null)
1526+
1527+
wrapper.destroy()
1528+
})
14481529
})

0 commit comments

Comments
 (0)