diff --git a/packages/web-components/src/components/va-accordion/va-accordion.e2e.ts b/packages/web-components/src/components/va-accordion/va-accordion.e2e.ts index bf9689b8d..9c523a756 100644 --- a/packages/web-components/src/components/va-accordion/va-accordion.e2e.ts +++ b/packages/web-components/src/components/va-accordion/va-accordion.e2e.ts @@ -14,7 +14,7 @@ describe('va-accordion', () => {
- diff --git a/packages/web-components/src/components/va-accordion/va-accordion.tsx b/packages/web-components/src/components/va-accordion/va-accordion.tsx index 2a16494e6..5badb1ae8 100644 --- a/packages/web-components/src/components/va-accordion/va-accordion.tsx +++ b/packages/web-components/src/components/va-accordion/va-accordion.tsx @@ -87,85 +87,88 @@ export class VaAccordion { @Listen('accordionItemToggled') itemToggledHandler(event: CustomEvent) { - // eslint-disable-next-line i18next/no-literal-string - const clickedItem = (event.target as HTMLElement).closest('va-accordion-item'); - // Usage for slot to provide context to analytics for header and level - const header = clickedItem.querySelector('[slot="headline"]'); - // using the slot to provide context to analytics for header and level - let headerText - let headerLevel - if (header) { - headerText = header?.innerHTML - headerLevel = parseInt(header?.tagName?.toLowerCase().split('')[1]); - } else { - // using the props to provide context to analytics for header and level - headerText = clickedItem.header - headerLevel = clickedItem.level - } - - if (this.openSingle) { - getSlottedNodes(this.el, 'va-accordion-item') - .filter(item => item !== clickedItem) - /* eslint-disable-next-line i18next/no-literal-string */ - .forEach(item => (item as Element).setAttribute('open', 'false')); - } - - const prevAttr = clickedItem.getAttribute('open') === 'true' ? true : false; - - if (!this.disableAnalytics) { - - const detail = { - componentName: 'va-accordion', - action: prevAttr ? 'collapse' : 'expand', - details: { - header: headerText || clickedItem.header, - subheader: clickedItem.subheader, - level: headerLevel || clickedItem.level, - sectionHeading: this.sectionHeading, - }, - }; - this.componentLibraryAnalytics.emit(detail); - } - - /* eslint-disable-next-line i18next/no-literal-string */ - clickedItem.setAttribute('open', !prevAttr ? "true" : "false"); - - if (!this.isScrolledIntoView(clickedItem)) { - clickedItem.scrollIntoView(); - } - - // Check if all accordions are open or closed due to user clicks - this.accordionsOpened(); + // eslint-disable-next-line i18next/no-literal-string + const clickedItem = (event.target as HTMLElement).closest( + 'va-accordion-item', + ); + // Usage for slot to provide context to analytics for header and level + const header = clickedItem.querySelector('[slot="headline"]'); + // using the slot to provide context to analytics for header and level + let headerText; + let headerLevel; + if (header) { + headerText = header?.innerHTML; + headerLevel = parseInt(header?.tagName?.toLowerCase().split('')[1]); + } else { + // using the props to provide context to analytics for header and level + headerText = clickedItem.header; + headerLevel = clickedItem.level; + } + + if (this.openSingle) { + getSlottedNodes(this.el, 'va-accordion-item') + .filter(item => item !== clickedItem) + /* eslint-disable-next-line i18next/no-literal-string */ + .forEach(item => (item as Element).setAttribute('open', 'false')); + } + + const prevAttr = clickedItem.getAttribute('open') === 'true' ? true : false; + + if (!this.disableAnalytics) { + const detail = { + componentName: 'va-accordion', + action: prevAttr ? 'collapse' : 'expand', + details: { + header: headerText || clickedItem.header, + subheader: clickedItem.subheader, + level: headerLevel || clickedItem.level, + sectionHeading: this.sectionHeading, + }, + }; + this.componentLibraryAnalytics.emit(detail); + } + + /* eslint-disable-next-line i18next/no-literal-string */ + clickedItem.setAttribute('open', !prevAttr ? 'true' : 'false'); + + if (!this.isScrolledIntoView(clickedItem)) { + clickedItem.scrollIntoView(); + } + + // Check if all accordions are open or closed due to user clicks + this.accordionsOpened(); } /** * Handles the accordion open state * @param method "some" or "every"; array methods to check if all or some of the accordion items are open */ - private accordionsOpened(method='every') { + private accordionsOpened(method = 'every') { // Track user clicks on va-accordion-item within an array to compare if all values are true or false const accordionItems = [...this.el.children] - .filter((el) => el.tagName.toLowerCase() === 'va-accordion-item') - .map((el) => el.open); + .filter(el => el.tagName.toLowerCase() === 'va-accordion-item') + .map(el => el.open); const allOpen = currentValue => currentValue === true; const allClosed = currentValue => currentValue === false; if (accordionItems[method](allOpen)) { - return this.expanded = true; + return (this.expanded = true); } if (accordionItems[method](allClosed)) { - return this.expanded = false; + return (this.expanded = false); } } // Expand or Collapse All Function for Button Click private expandCollapseAll = (expanded: boolean) => { this.expanded = expanded; - getSlottedNodes(this.el, 'va-accordion-item').forEach((item:HTMLElement) => { - item.setAttribute('open', `${expanded}`) - }); + getSlottedNodes(this.el, 'va-accordion-item').forEach( + (item: HTMLElement) => { + item.setAttribute('open', `${expanded}`); + }, + ); }; isScrolledIntoView(el: Element) { @@ -201,36 +204,35 @@ export class VaAccordion { 'usa-accordion': true, }); - const accordionItemIDs = [...this.el.children] - .filter((el) => el.tagName.toLowerCase() === 'va-accordion-item') - .map((el) => el.id); - return ( -
this.accordionContainer = accordionContainer}> - { - !openSingle ? ( - - ) : null +
+ (this.accordionContainer = accordionContainer) } + > + {!openSingle ? ( + + ) : null}
- ) + ); } }