-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OEL-3547: Added collapse/expand buttons in accordion.
[skip chromatic]
- Loading branch information
Showing
10 changed files
with
664 additions
and
488 deletions.
There are no files selected for viewing
949 changes: 508 additions & 441 deletions
949
src/components/bcl-accordion/__snapshots__/accordion.test.js.snap
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/themes/default/src/js/accordion-toggle/accordion-toggle.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* eslint-disable prefer-destructuring */ | ||
import Collapse from "@openeuropa/bcl-bootstrap/js/src/collapse"; | ||
import EventHandler from "@openeuropa/bcl-bootstrap/js/src/dom/event-handler"; | ||
import SelectorEngine from "@openeuropa/bcl-bootstrap/js/src/dom/selector-engine"; | ||
|
||
class AccordionToggle { | ||
static isInitialized = false; | ||
|
||
constructor(buttonElement) { | ||
this.buttonElement = buttonElement; | ||
this.targetAccordionId = buttonElement.getAttribute("data-target"); | ||
this.action = buttonElement.getAttribute("data-action"); | ||
|
||
this.accordionElement = SelectorEngine.findOne(`#${this.targetAccordionId}`); | ||
this.accordionItems = SelectorEngine.find(".accordion-collapse", this.accordionElement); | ||
|
||
this.addEventListeners(); | ||
} | ||
|
||
addEventListeners() { | ||
EventHandler.on(this.buttonElement, "click", (event) => this.handleAccordionAction(event)); | ||
} | ||
|
||
handleAccordionAction(event) { | ||
const item = event.target; | ||
const action = item.getAttribute('data-action'); | ||
const accordionItems = this.accordionItems; | ||
|
||
accordionItems.forEach((accordionItem) => { | ||
const collapseInstance = Collapse.getOrCreateInstance(accordionItem, { toggle: false }); | ||
|
||
if (action === 'expand') { | ||
collapseInstance.show(); | ||
} else if (action === 'collapse') { | ||
collapseInstance.hide(); | ||
} | ||
}); | ||
} | ||
|
||
static init() { | ||
if (AccordionToggle.isInitialized) { | ||
return; | ||
} | ||
|
||
const toggleButtons = SelectorEngine.find('[data-action][data-target]'); | ||
toggleButtons.forEach(buttonElement => new AccordionToggle(buttonElement)); | ||
|
||
AccordionToggle.isInitialized = true; | ||
} | ||
} | ||
|
||
document.addEventListener("DOMContentLoaded", () => AccordionToggle.init()); | ||
|
||
export default AccordionToggle; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters