Skip to content

Commit 8bf00fa

Browse files
committed
feat (Accordion): add onInit event
1 parent e9c57b5 commit 8bf00fa

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

examples/accessible-accordion/index.html

+3
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,9 @@ <h2>Code</h2>
753753
'#accordion-demo-5': { closedDefault: true },
754754
'#accordion-demo-6': {
755755
forceExpand: false,
756+
onInit: function(el) {
757+
console.log(`${el.id} initialized`)
758+
},
756759
onClose: function(panel) {
757760
alert(`${panel.id} closed`)
758761
},

src/classes/Accordion.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,22 @@ class Accordion extends AbstractDomElement {
5252
* @author Milan Ricoul
5353
*/
5454
init() {
55-
this.active = true
56-
5755
const el = this._element
58-
const { closedDefault, mediaQuery, onReachBreakpoint, panelSelector, prefixId, triggerSelector } = this._settings
56+
const { closedDefault, mediaQuery, onInit, onReachBreakpoint, panelSelector, prefixId, triggerSelector } = this._settings
5957
const pattern = /\d+/g
6058
const triggers = el.querySelectorAll(triggerSelector)
6159
const panels = el.querySelectorAll(panelSelector)
6260
const id = randomId()
6361

6462
el.dataset.id = id
6563

64+
if (onInit && !this.active) {
65+
this.active = true
66+
onInit.bind(this)(el)
67+
}
68+
69+
this.active = true
70+
6671
if (mediaQuery && onReachBreakpoint && pattern.test(mediaQuery.media)) {
6772
if (mediaQuery.media.includes('min')) {
6873
this.hasReachBreakpoint = window.innerWidth > parseInt(mediaQuery.media.match(pattern)[0]) ? 'above' : 'below'
@@ -427,6 +432,7 @@ Accordion.defaults = {
427432
forceExpand: true,
428433
hasAnimation: false,
429434
mediaQuery: null,
435+
onInit: null,
430436
onReachBreakpoint: null,
431437
onOpen: null,
432438
onClose: null,

0 commit comments

Comments
 (0)