mirror of
https://github.com/twbs/bootstrap.git
synced 2022-11-09 12:25:43 -05:00
Restore offset
option for tooltip/popover components
This commit is contained in:
parent
881f43a3b9
commit
b1bd54955e
2 changed files with 57 additions and 0 deletions
|
@ -50,6 +50,7 @@ const DefaultType = {
|
||||||
html: 'boolean',
|
html: 'boolean',
|
||||||
selector: '(string|boolean)',
|
selector: '(string|boolean)',
|
||||||
placement: '(string|function)',
|
placement: '(string|function)',
|
||||||
|
offset: '(array|string|function)',
|
||||||
container: '(string|element|boolean)',
|
container: '(string|element|boolean)',
|
||||||
fallbackPlacements: 'array',
|
fallbackPlacements: 'array',
|
||||||
boundary: '(string|element)',
|
boundary: '(string|element)',
|
||||||
|
@ -80,6 +81,7 @@ const Default = {
|
||||||
html: false,
|
html: false,
|
||||||
selector: false,
|
selector: false,
|
||||||
placement: 'top',
|
placement: 'top',
|
||||||
|
offset: [0, 0],
|
||||||
container: false,
|
container: false,
|
||||||
fallbackPlacements: ['top', 'right', 'bottom', 'left'],
|
fallbackPlacements: ['top', 'right', 'bottom', 'left'],
|
||||||
boundary: 'clippingParents',
|
boundary: 'clippingParents',
|
||||||
|
@ -473,6 +475,20 @@ class Tooltip extends BaseComponent {
|
||||||
return context
|
return context
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_getOffset() {
|
||||||
|
const { offset } = this.config
|
||||||
|
|
||||||
|
if (typeof offset === 'string') {
|
||||||
|
return offset.split(',').map(val => Number.parseInt(val, 10))
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof offset === 'function') {
|
||||||
|
return popperData => offset(popperData, this._element)
|
||||||
|
}
|
||||||
|
|
||||||
|
return offset
|
||||||
|
}
|
||||||
|
|
||||||
_getPopperConfig(attachment) {
|
_getPopperConfig(attachment) {
|
||||||
const defaultBsConfig = {
|
const defaultBsConfig = {
|
||||||
placement: attachment,
|
placement: attachment,
|
||||||
|
@ -484,6 +500,12 @@ class Tooltip extends BaseComponent {
|
||||||
fallbackPlacements: this.config.fallbackPlacements
|
fallbackPlacements: this.config.fallbackPlacements
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: 'offset',
|
||||||
|
options: {
|
||||||
|
offset: this._getOffset()
|
||||||
|
}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: 'preventOverflow',
|
name: 'preventOverflow',
|
||||||
options: {
|
options: {
|
||||||
|
|
|
@ -107,6 +107,41 @@ describe('Tooltip', () => {
|
||||||
tooltipInContainerEl.click()
|
tooltipInContainerEl.click()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should create offset modifier when offset is passed as a function', done => {
|
||||||
|
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Offset from function">'
|
||||||
|
|
||||||
|
const getOffset = jasmine.createSpy('getOffset').and.returnValue([10, 20])
|
||||||
|
const tooltipEl = fixtureEl.querySelector('a')
|
||||||
|
const tooltip = new Tooltip(tooltipEl, {
|
||||||
|
offset: getOffset,
|
||||||
|
popperConfig: {
|
||||||
|
onFirstUpdate: state => {
|
||||||
|
expect(getOffset).toHaveBeenCalledWith({
|
||||||
|
popper: state.rects.popper,
|
||||||
|
reference: state.rects.reference,
|
||||||
|
placement: state.placement
|
||||||
|
}, tooltipEl)
|
||||||
|
done()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
const offset = tooltip._getOffset()
|
||||||
|
|
||||||
|
expect(typeof offset).toEqual('function')
|
||||||
|
|
||||||
|
tooltip.show()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should create offset modifier when offset option is passed in data attribute', () => {
|
||||||
|
fixtureEl.innerHTML = '<a href="#" rel="tooltip" data-bs-offset="10,20" title="Another tooltip">'
|
||||||
|
|
||||||
|
const tooltipEl = fixtureEl.querySelector('a')
|
||||||
|
const tooltip = new Tooltip(tooltipEl)
|
||||||
|
|
||||||
|
expect(tooltip._getOffset()).toEqual([10, 20])
|
||||||
|
})
|
||||||
|
|
||||||
it('should allow to pass config to Popper with `popperConfig`', () => {
|
it('should allow to pass config to Popper with `popperConfig`', () => {
|
||||||
fixtureEl.innerHTML = '<a href="#" rel="tooltip">'
|
fixtureEl.innerHTML = '<a href="#" rel="tooltip">'
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue