1
0
Fork 0
mirror of https://github.com/twbs/bootstrap.git synced 2022-11-09 12:25:43 -05:00

Fix popover arrow & tooltip template after the setContent addition (#35441)

This commit is contained in:
GeoSot 2021-12-01 15:10:10 +02:00 committed by GitHub
parent 44a6cd724c
commit cab62af2e6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 11 deletions

View file

@ -38,7 +38,6 @@ const CLASS_NAME_SHOW = 'show'
const HOVER_STATE_SHOW = 'show' const HOVER_STATE_SHOW = 'show'
const HOVER_STATE_OUT = 'out' const HOVER_STATE_OUT = 'out'
const SELECTOR_TOOLTIP_ARROW = '.tooltip-arrow'
const SELECTOR_TOOLTIP_INNER = '.tooltip-inner' const SELECTOR_TOOLTIP_INNER = '.tooltip-inner'
const SELECTOR_MODAL = `.${CLASS_NAME_MODAL}` const SELECTOR_MODAL = `.${CLASS_NAME_MODAL}`
@ -333,15 +332,23 @@ class Tooltip extends BaseComponent {
} }
getTipElement() { getTipElement() {
if (this.tip) { if (!this.tip) {
return this.tip this.tip = this._createTipElement(this._getContentForTemplate())
} }
const templateFactory = this._getTemplateFactory(this._getContentForTemplate()) return this.tip
}
_createTipElement(content) {
const tip = this._getTemplateFactory(content).toHtml()
// todo: remove this check on v6
if (!tip) {
return null
}
const tip = templateFactory.toHtml()
tip.classList.remove(CLASS_NAME_FADE, CLASS_NAME_SHOW) tip.classList.remove(CLASS_NAME_FADE, CLASS_NAME_SHOW)
// todo on v6 the following can be done on css only // todo: on v6 the following can be achieved with CSS only
tip.classList.add(`bs-${this.constructor.NAME}-auto`) tip.classList.add(`bs-${this.constructor.NAME}-auto`)
const tipId = getUID(this.constructor.NAME).toString() const tipId = getUID(this.constructor.NAME).toString()
@ -352,8 +359,7 @@ class Tooltip extends BaseComponent {
tip.classList.add(CLASS_NAME_FADE) tip.classList.add(CLASS_NAME_FADE)
} }
this.tip = tip return tip
return this.tip
} }
setContent(content) { setContent(content) {
@ -361,11 +367,11 @@ class Tooltip extends BaseComponent {
if (this.tip) { if (this.tip) {
isShown = this.tip.classList.contains(CLASS_NAME_SHOW) isShown = this.tip.classList.contains(CLASS_NAME_SHOW)
this.tip.remove() this.tip.remove()
this.tip = null
} }
this._disposePopper() this._disposePopper()
this.tip = this._createTipElement(content)
this.tip = this._getTemplateFactory(content).toHtml()
if (isShown) { if (isShown) {
this.show() this.show()
@ -446,7 +452,7 @@ class Tooltip extends BaseComponent {
{ {
name: 'arrow', name: 'arrow',
options: { options: {
element: SELECTOR_TOOLTIP_ARROW element: `.${this.constructor.NAME}-arrow`
} }
} }
] ]

View file

@ -155,6 +155,22 @@ describe('Popover', () => {
popover.show() popover.show()
}) })
it('"setContent" should keep the initial template', () => {
fixtureEl.innerHTML = '<a href="#" title="Popover" data-bs-content="https://twitter.com/getbootstrap" data-bs-custom-class="custom-class">BS twitter</a>'
const popoverEl = fixtureEl.querySelector('a')
const popover = new Popover(popoverEl)
popover.setContent({ '.tooltip-inner': 'foo' })
const tip = popover.getTipElement()
expect(tip).toHaveClass('popover')
expect(tip).toHaveClass('bs-popover-auto')
expect(tip.querySelector('.popover-arrow')).not.toBeNull()
expect(tip.querySelector('.popover-header')).not.toBeNull()
expect(tip.querySelector('.popover-body')).not.toBeNull()
})
it('should call setContent once', done => { it('should call setContent once', done => {
fixtureEl.innerHTML = '<a href="#">BS twitter</a>' fixtureEl.innerHTML = '<a href="#">BS twitter</a>'

View file

@ -1081,6 +1081,21 @@ describe('Tooltip', () => {
expect(tip()).not.toHaveClass('show') expect(tip()).not.toHaveClass('show')
expect(tip().querySelector('.tooltip-inner').textContent).toEqual('foo') expect(tip().querySelector('.tooltip-inner').textContent).toEqual('foo')
}) })
it('"setContent" should keep the initial template', () => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl)
tooltip.setContent({ '.tooltip-inner': 'foo' })
const tip = tooltip.getTipElement()
expect(tip).toHaveClass('tooltip')
expect(tip).toHaveClass('bs-tooltip-auto')
expect(tip.querySelector('.tooltip-arrow')).not.toBeNull()
expect(tip.querySelector('.tooltip-inner')).not.toBeNull()
})
}) })
describe('setContent', () => { describe('setContent', () => {