Skip to content

Commit

Permalink
Offset option for dropdown can be function (twbs#24222)
Browse files Browse the repository at this point in the history
* Offset option can be function (Popper.js)

* Fix...add function type for offset option

* Remove constants for popper config

* Optimize code. Remove foreach loop.

* Refactoring. Remove getOffset method
  • Loading branch information
romanlex authored and Johann-S committed Oct 3, 2017
1 parent 898708d commit 527f55c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/4.0/components/dropdowns.md
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ Options can be passed via data attributes or JavaScript. For data attributes, ap
<tbody>
<tr>
<td>offset</td>
<td>number | string</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>
</tr>
Expand Down
15 changes: 11 additions & 4 deletions js/src/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const Dropdown = (() => {
}

const DefaultType = {
offset : '(number|string)',
offset : '(number|string|function)',
flip : 'boolean'
}

Expand Down Expand Up @@ -246,12 +246,19 @@ const Dropdown = (() => {
}

_getPopperConfig() {
const offsetConf = {}
if (typeof this._config.offset === 'function') {
offsetConf.fn = (data) => {
data.offsets = $.extend({}, data.offsets, this._config.offset(data.offsets) || {})
return data
}
} else {
offsetConf.offset = this._config.offset
}
const popperConfig = {
placement : this._getPlacement(),
modifiers : {
offset : {
offset : this._config.offset
},
offset : offsetConf,
flip : {
enabled : this._config.flip
}
Expand Down

0 comments on commit 527f55c

Please sign in to comment.