Skip to content

Commit 14c347c

Browse files
committed
Add update method for Collapse plugin
1 parent fb98304 commit 14c347c

File tree

3 files changed

+42
-15
lines changed

3 files changed

+42
-15
lines changed

docs/4.0/components/collapse.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,10 @@ Shows a collapsible element. **Returns to the caller before the collapsible elem
226226

227227
Hides a collapsible element. **Returns to the caller before the collapsible element has actually been hidden** (i.e. before the `hidden.bs.collapse` event occurs).
228228

229+
#### `.collapse('update')`
230+
231+
Update a collapsible element if it changed during its lifetime.
232+
229233
### Events
230234

231235
Bootstrap's collapse class exposes a few events for hooking into collapse functionality.

js/src/collapse.js

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ const Collapse = (() => {
8888
}
8989
}
9090

91-
this._parent = this._config.parent ? this._getParent() : null
91+
this._parent = this._config.parent ? this._getParent() : null
92+
this._showClass = this._getShowClass()
9293

9394
if (!this._config.parent) {
9495
this._addAriaAndCollapsedClass(this._element, this._triggerArray)
@@ -114,17 +115,16 @@ const Collapse = (() => {
114115
// public
115116

116117
toggle() {
117-
if ($(this._element).hasClass(ClassName.SHOW) || $(this._element).hasClass(ClassName.FLEXSHOW)) {
118+
if ($(this._element).hasClass(this._showClass)) {
118119
this.hide()
119120
} else {
120121
this.show()
121122
}
122123
}
123124

124125
show() {
125-
const showClass = this._getShowClass()
126126
if (this._isTransitioning ||
127-
$(this._element).hasClass(showClass)) {
127+
$(this._element).hasClass(this._showClass)) {
128128
return
129129
}
130130

@@ -178,7 +178,7 @@ const Collapse = (() => {
178178
$(this._element)
179179
.removeClass(ClassName.COLLAPSING)
180180
.addClass(ClassName.COLLAPSE)
181-
.addClass(showClass)
181+
.addClass(this._showClass)
182182

183183
this._element.style[dimension] = ''
184184

@@ -203,9 +203,8 @@ const Collapse = (() => {
203203
}
204204

205205
hide() {
206-
const showClass = this._getShowClass()
207206
if (this._isTransitioning ||
208-
!$(this._element).hasClass(showClass)) {
207+
!$(this._element).hasClass(this._showClass)) {
209208
return
210209
}
211210

@@ -215,16 +214,15 @@ const Collapse = (() => {
215214
return
216215
}
217216

218-
const dimension = this._getDimension()
219-
217+
const dimension = this._getDimension()
220218
this._element.style[dimension] = `${this._element.getBoundingClientRect()[dimension]}px`
221219

222220
Util.reflow(this._element)
223221

224222
$(this._element)
225223
.addClass(ClassName.COLLAPSING)
226224
.removeClass(ClassName.COLLAPSE)
227-
.removeClass(showClass)
225+
.removeClass(this._showClass)
228226

229227
if (this._triggerArray.length) {
230228
for (let i = 0; i < this._triggerArray.length; i++) {
@@ -266,6 +264,16 @@ const Collapse = (() => {
266264
this._isTransitioning = isTransitioning
267265
}
268266

267+
update() {
268+
if ($(this._element).hasClass(this._showClass)) {
269+
$(this._element).removeClass(this._showClass)
270+
this._showClass = this._getShowClass()
271+
$(this._element).addClass(this._showClass)
272+
} else {
273+
this._showClass = this._getShowClass()
274+
}
275+
}
276+
269277
dispose() {
270278
$.removeData(this._element, DATA_KEY)
271279

@@ -320,15 +328,28 @@ const Collapse = (() => {
320328

321329
_getShowClass() {
322330
const tabClass = this._element.classList
323-
let useFlex = $(this._element).css('display') === 'flex'
331+
332+
// Detect flex for inline style
333+
let useFlex = $(this._element).css('display').indexOf('flex') !== -1
334+
335+
// Create a wrapper to hide our flex detection
336+
const $tmpWrapper = $('<div class="d-none"></div>')
337+
$tmpWrapper.insertAfter($(this._element))
338+
339+
const $tmpElem = $('<div></div>')
340+
$tmpWrapper.append($tmpElem)
341+
324342
// Detect flex in used classes
325343
for (let i = 0; i < tabClass.length; i++) {
326-
const tmpDisplay = $('<div></div>').addClass(tabClass[i]).css('display')
327-
if (tmpDisplay === 'flex') {
344+
$tmpElem.addClass(tabClass[i])
345+
const tmpDisplay = $tmpElem.css('display')
346+
$tmpElem.removeClass(tabClass[i])
347+
if (tmpDisplay.indexOf('flex') !== -1) {
328348
useFlex = true
329349
break
330350
}
331351
}
352+
$tmpWrapper.remove()
332353
return !useFlex ? ClassName.SHOW : ClassName.FLEXSHOW
333354
}
334355

scss/_transitions.scss

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
}
1919

2020
tr {
21-
&.collapse.show {
21+
&.collapse.show,
22+
&.collapse.flexshow {
2223
display: table-row;
2324
}
2425
}
2526

2627
tbody {
27-
&.collapse.show {
28+
&.collapse.show,
29+
&.collapse.flexshow {
2830
display: table-row-group;
2931
}
3032
}

0 commit comments

Comments
 (0)