Skip to content

Commit 7ad5ea1

Browse files
committed
Add update method for Collapse plugin
1 parent c413342 commit 7ad5ea1

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
@@ -87,7 +87,8 @@ const Collapse = (($) => {
8787
}
8888
}
8989

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

9293
if (!this._config.parent) {
9394
this._addAriaAndCollapsedClass(this._element, this._triggerArray)
@@ -113,17 +114,16 @@ const Collapse = (($) => {
113114
// public
114115

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

123124
show() {
124-
const showClass = this._getShowClass()
125125
if (this._isTransitioning ||
126-
$(this._element).hasClass(showClass)) {
126+
$(this._element).hasClass(this._showClass)) {
127127
return
128128
}
129129

@@ -177,7 +177,7 @@ const Collapse = (($) => {
177177
$(this._element)
178178
.removeClass(ClassName.COLLAPSING)
179179
.addClass(ClassName.COLLAPSE)
180-
.addClass(showClass)
180+
.addClass(this._showClass)
181181

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

@@ -202,9 +202,8 @@ const Collapse = (($) => {
202202
}
203203

204204
hide() {
205-
const showClass = this._getShowClass()
206205
if (this._isTransitioning ||
207-
!$(this._element).hasClass(showClass)) {
206+
!$(this._element).hasClass(this._showClass)) {
208207
return
209208
}
210209

@@ -214,16 +213,15 @@ const Collapse = (($) => {
214213
return
215214
}
216215

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

221219
Util.reflow(this._element)
222220

223221
$(this._element)
224222
.addClass(ClassName.COLLAPSING)
225223
.removeClass(ClassName.COLLAPSE)
226-
.removeClass(showClass)
224+
.removeClass(this._showClass)
227225

228226
if (this._triggerArray.length) {
229227
for (let i = 0; i < this._triggerArray.length; i++) {
@@ -265,6 +263,16 @@ const Collapse = (($) => {
265263
this._isTransitioning = isTransitioning
266264
}
267265

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

@@ -319,15 +327,28 @@ const Collapse = (($) => {
319327

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

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)