Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix+example #36585

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion js/src/popover.js
Expand Up @@ -55,7 +55,7 @@ class Popover extends Tooltip {

// Overrides
_isWithContent() {
return this._getTitle() || this._getContent()
return Boolean(this._config.title || this._config.originalTitle || this._config.content)
}

// Private
Expand Down
32 changes: 15 additions & 17 deletions js/src/tooltip.js
Expand Up @@ -115,6 +115,7 @@ class Tooltip extends BaseComponent {
this._activeTrigger = {}
this._popper = null
this._templateFactory = null
this._newContent = null

// Protected
this.tip = null
Expand Down Expand Up @@ -205,6 +206,12 @@ class Tooltip extends BaseComponent {
return
}

// todo v6 remove this OR make it optional
if (this.tip) {
this.tip.remove()
this.tip = null
}

const tip = this._getTipElement()

this._element.setAttribute('aria-describedby', tip.getAttribute('id'))
Expand All @@ -219,7 +226,7 @@ class Tooltip extends BaseComponent {
if (this._popper) {
this._popper.update()
} else {
this._createPopper(tip)
this._popper = this._createPopper(tip)
}

tip.classList.add(CLASS_NAME_SHOW)
Expand Down Expand Up @@ -300,12 +307,12 @@ class Tooltip extends BaseComponent {

// Protected
_isWithContent() {
return Boolean(this._getTitle())
return Boolean(this._config.title || this._config.originalTitle)
}

_getTipElement() {
if (!this.tip) {
this.tip = this._createTipElement(this._getContentForTemplate())
this.tip = this._createTipElement(this._newContent || this._getContentForTemplate())
}

return this.tip
Expand Down Expand Up @@ -335,17 +342,9 @@ class Tooltip extends BaseComponent {
}

setContent(content) {
let isShown = false
if (this.tip) {
isShown = this._isShown()
this.tip.remove()
this.tip = null
}

this._disposePopper()
this.tip = this._createTipElement(content)

if (isShown) {
this._newContent = content
if (this._isShown()) {
this._disposePopper()
this.show()
}
}
Expand Down Expand Up @@ -373,7 +372,7 @@ class Tooltip extends BaseComponent {
}

_getTitle() {
return this._config.title
return this._resolvePossibleFunction(this._config.title) || this._config.originalTitle
}

// Private
Expand All @@ -394,7 +393,7 @@ class Tooltip extends BaseComponent {
this._config.placement.call(this, tip, this._element) :
this._config.placement
const attachment = AttachmentMap[placement.toUpperCase()]
this._popper = Popper.createPopper(this._element, tip, this._getPopperConfig(attachment))
return Popper.createPopper(this._element, tip, this._getPopperConfig(attachment))
}

_getOffset() {
Expand Down Expand Up @@ -592,7 +591,6 @@ class Tooltip extends BaseComponent {
}

config.originalTitle = this._element.getAttribute('title') || ''
config.title = this._resolvePossibleFunction(config.title) || config.originalTitle
if (typeof config.title === 'number') {
config.title = config.title.toString()
}
Expand Down
6 changes: 4 additions & 2 deletions js/tests/unit/tooltip.spec.js
Expand Up @@ -185,7 +185,7 @@ describe('Tooltip', () => {
const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl)

expect(tooltip._config.title).toEqual('Another tooltip')
expect(tooltip._getTitle()).toEqual('Another tooltip')
})
})

Expand Down Expand Up @@ -848,7 +848,7 @@ describe('Tooltip', () => {
}, 100)

setTimeout(() => {
expect(insertedFunc).toHaveBeenCalledTimes(1)
expect(insertedFunc).toHaveBeenCalledTimes(2)
resolve()
}, 200)
}, 0)
Expand Down Expand Up @@ -1166,6 +1166,7 @@ describe('Tooltip', () => {
tooltip.setContent({ '.tooltip-inner': 'foo' })

expect(tip()).not.toHaveClass('show')
tooltip.show()
expect(tip().querySelector('.tooltip-inner').textContent).toEqual('foo')
})

Expand Down Expand Up @@ -1229,6 +1230,7 @@ describe('Tooltip', () => {
})

tooltip.setContent({ '.tooltip': { 0: childContent, jquery: 'jQuery' } })
tooltip.show()

expect(childContent.parentNode).toEqual(tooltip._getTipElement())
})
Expand Down