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

Move dropdown offset function logic into private function #28138

Merged
merged 1 commit into from Feb 5, 2019
Merged
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
19 changes: 13 additions & 6 deletions js/src/dropdown.js
Expand Up @@ -319,24 +319,30 @@ class Dropdown {
return $(this._element).closest('.navbar').length > 0
}

_getPopperConfig() {
const offsetConf = {}
_getOffset() {
const offset = {}

if (typeof this._config.offset === 'function') {
offsetConf.fn = (data) => {
offset.fn = (data) => {
data.offsets = {
...data.offsets,
...this._config.offset(data.offsets) || {}
...this._config.offset(data.offsets, this._element) || {}
}

return data
}
} else {
offsetConf.offset = this._config.offset
offset.offset = this._config.offset
}

return offset
}

_getPopperConfig() {
const popperConfig = {
placement: this._getPlacement(),
modifiers: {
offset: offsetConf,
offset: this._getOffset(),
flip: {
enabled: this._config.flip
},
Expand All @@ -352,6 +358,7 @@ class Dropdown {
enabled: false
}
}

return popperConfig
}

Expand Down
55 changes: 55 additions & 0 deletions js/tests/unit/dropdown.js
Expand Up @@ -1361,4 +1361,59 @@ $(function () {
$dropdown.hide()
assert.ok($dropdown.parent('.dropdown').hasClass('show'))
})

QUnit.test('should create offset modifier correctly when offset option is a function', function (assert) {
assert.expect(2)

var getOffset = function (offsets) {
return offsets
}

var dropdownHTML =
'<div class="dropdown">' +
' <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' +
' <div class="dropdown-menu">' +
' <a class="dropdown-item" href="#">Another link</a>' +
' </div>' +
'</div>'

var $dropdown = $(dropdownHTML)
.appendTo('#qunit-fixture')
.find('[data-toggle="dropdown"]')
.bootstrapDropdown({
offset: getOffset
})

var dropdown = $dropdown.data('bs.dropdown')
var offset = dropdown._getOffset()

assert.ok(typeof offset.offset === 'undefined')
assert.ok(typeof offset.fn === 'function')
})

QUnit.test('should create offset modifier correctly when offset option is not a function', function (assert) {
assert.expect(2)

var dropdownHTML =
'<div class="dropdown">' +
' <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' +
' <div class="dropdown-menu">' +
' <a class="dropdown-item" href="#">Another link</a>' +
' </div>' +
'</div>'

var myOffset = 42
var $dropdown = $(dropdownHTML)
.appendTo('#qunit-fixture')
.find('[data-toggle="dropdown"]')
.bootstrapDropdown({
offset: myOffset
})

var dropdown = $dropdown.data('bs.dropdown')
var offset = dropdown._getOffset()

assert.strictEqual(offset.offset, myOffset)
assert.ok(typeof offset.fn === 'undefined')
})
})
6 changes: 5 additions & 1 deletion site/docs/4.2/components/dropdowns.md
Expand Up @@ -845,7 +845,11 @@ Options can be passed via data attributes or JavaScript. For data attributes, ap
<td>offset</td>
<td>number | string | function</td>
<td>0</td>
<td>Offset of the dropdown relative to its target. For more information refer to Popper.js's <a href="https://popper.js.org/popper-documentation.html#modifiers..offset.offset">offset docs</a>.</td>
<td>
<p>Offset of the dropdown relative to its target.</p>
<p>When a function is used to determine the offset, it is called with an object containing the offset data as its first argument. The function must return an object with the same structure. The triggering element DOM node is passed as the second argument.</p>
<p>For more information refer to Popper.js's <a href="https://popper.js.org/popper-documentation.html#modifiers..offset.offset">offset docs</a>.</p>
</td>
</tr>
<tr>
<td>flip</td>
Expand Down