Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add consistent types to functions #1245

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ export class VaAccordionItem {

/**
* Optional accordion item subheader text. Default is null.
*/
*/
@Prop() subheader?: string = null;

/**
* True if the item is open
*/
*/
@Prop() open?: boolean = false;

/**
Expand Down Expand Up @@ -85,7 +85,7 @@ export class VaAccordionItem {
// Data access from slot html element is being perfomed by this function
// Function allows us to provide context to state
// State is then being digested by the Header Function below
private populateStateValues() {
private populateStateValues(): void {
getSlottedNodes(this.el, null).forEach((node: HTMLSlotElement) => {
this.slotHeader = node.innerHTML;
this.slotTag = node.tagName.toLowerCase();
Expand All @@ -106,21 +106,22 @@ export class VaAccordionItem {
}

render() {
const {uswds} = this;
const { uswds } = this;

if (uswds) {
const {bordered, header, subheader, level, open} = this;
const { bordered, header, subheader, level, open } = this;
const accordionItemClass = classNames({
'usa-accordion--bordered': bordered,
});
const Header = () => {
const headline = this.el.querySelector('[slot="headline"]');
const ieSlotCheckHeader = headline?.innerHTML;
// eslint-disable-next-line i18next/no-literal-string
const Tag = (headline && headline.tagName.includes("H"))
? headline.tagName.toLowerCase()
// eslint-disable-next-line i18next/no-literal-string
: `h${level}`;
const Tag =
headline && headline.tagName.includes('H')
? headline.tagName.toLowerCase()
: // eslint-disable-next-line i18next/no-literal-string
`h${level}`;

return (
<Tag class="usa-accordion__heading">
Expand All @@ -139,44 +140,47 @@ export class VaAccordionItem {
<slot name="icon" />
{this.slotHeader || header || ieSlotCheckHeader}
</span>
{this.subheader &&
{this.subheader && (
<span class="va-accordion__subheader">
<slot name="subheader-icon" />
{subheader}
</span>}
</span>
)}
</button>
</Tag >
</Tag>
);
}
};

return (
<Host>
<div class={accordionItemClass}>
<Header/>
<slot name="headline" onSlotchange={() => this.populateStateValues()} />
<Header />
<slot
name="headline"
onSlotchange={() => this.populateStateValues()}
/>
<div
id="content"
class="usa-accordion__content usa-prose"
hidden={!open}
part="accordion-content"
>
<slot/>
<slot />
</div>
</div>
</Host>
)

);
} else {

const Header = () => {
const headline = this.el.querySelector('[slot="headline"]');
const ieSlotCheckHeader = headline?.innerHTML;

// eslint-disable-next-line i18next/no-literal-string
const Tag = (headline && headline.tagName.includes("H"))
? headline.tagName.toLowerCase()
// eslint-disable-next-line i18next/no-literal-string
: `h${this.level}`;
const Tag =
headline && headline.tagName.includes('H')
? headline.tagName.toLowerCase()
: // eslint-disable-next-line i18next/no-literal-string
`h${this.level}`;

return (
<Tag>
Expand All @@ -193,19 +197,23 @@ export class VaAccordionItem {
<slot name="icon" />
{this.slotHeader || this.header || ieSlotCheckHeader}
</span>
{this.subheader &&
<p class="subheader" part='accordion-subheader'>
{this.subheader && (
<p class="subheader" part="accordion-subheader">
<slot name="subheader-icon" />
{this.subheader}
</p>}
</p>
)}
</button>
</Tag >
</Tag>
);
}
};
return (
<Host>
<Header />
<slot name="headline" onSlotchange={() => this.populateStateValues()} />
<slot
name="headline"
onSlotchange={() => this.populateStateValues()}
/>
<div id="content">
<slot />
</div>
Expand Down
156 changes: 81 additions & 75 deletions packages/web-components/src/components/va-accordion/va-accordion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,58 +92,59 @@ 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'));
}
// 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;
}

const prevAttr = clickedItem.getAttribute('open') === 'true' ? true : false;
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'));
}

if (!this.disableAnalytics) {
const prevAttr = clickedItem.getAttribute('open') === 'true' ? true : false;

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);
}
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");
/* eslint-disable-next-line i18next/no-literal-string */
clickedItem.setAttribute('open', !prevAttr ? 'true' : 'false');

if (!this.isScrolledIntoView(clickedItem)) {
clickedItem.scrollIntoView();
}
if (!this.isScrolledIntoView(clickedItem)) {
clickedItem.scrollIntoView();
}

// Check if all accordions are open or closed due to user clicks
this.accordionsOpened();
// Check if all accordions are open or closed due to user clicks
this.accordionsOpened();
}

private accordionsOpened() {
private accordionsOpened(): void {
// Track user clicks on va-accordion-item within an array to compare if all values are true or false
let accordionItems = [];
getSlottedNodes(this.el, 'va-accordion-item').forEach(item => {
Expand All @@ -160,14 +161,16 @@ export class VaAccordion {
}

// Expand or Collapse All Function for Button Click
private expandCollapseAll = (expanded: boolean) => {
private expandCollapseAll = (expanded: boolean): void => {
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) {
isScrolledIntoView(el: Element): boolean {
const elemTop = el?.getBoundingClientRect().top;

if (!elemTop && elemTop !== 0) {
Expand All @@ -189,45 +192,48 @@ export class VaAccordion {
}

render() {
const {uswds, openSingle} = this;
const { uswds, openSingle } = this;
if (uswds) {
const accordionClass = classNames({
'usa-accordion': true,
});
const accordionItemIDs = [...this.el.children]
.filter((el) => el.tagName.toLowerCase() === 'va-accordion-item')
.map((el) => el.id);
.filter(el => el.tagName.toLowerCase() === 'va-accordion-item')
.map(el => el.id);
return (
<Host>
<div class={ accordionClass } ref={(accordionContainer) => this.accordionContainer = accordionContainer}>
{
!openSingle ? (
<button
class="va-accordion__button"
ref={el => (this.expandCollapseBtn = el as HTMLButtonElement)}
onClick={() => this.expandCollapseAll(!this.expanded)}
aria-label={
this.expanded
? i18next.t('collapse-all-aria-label')
: i18next.t('expand-all-aria-label')
}
aria-controls={accordionItemIDs.join(' ')}
aria-expanded={`${this.expanded}`}
>
{this.expanded
? `${i18next.t('collapse-all')} -`
: `${i18next.t('expand-all')} +`}
</button>
) : null
<div
class={accordionClass}
ref={accordionContainer =>
(this.accordionContainer = accordionContainer)
}
>
{!openSingle ? (
<button
class="va-accordion__button"
ref={el => (this.expandCollapseBtn = el as HTMLButtonElement)}
onClick={() => this.expandCollapseAll(!this.expanded)}
aria-label={
this.expanded
? i18next.t('collapse-all-aria-label')
: i18next.t('expand-all-aria-label')
}
aria-controls={accordionItemIDs.join(' ')}
aria-expanded={`${this.expanded}`}
>
{this.expanded
? `${i18next.t('collapse-all')} -`
: `${i18next.t('expand-all')} +`}
</button>
) : null}
<slot></slot>
</div>
</Host>
)
);
} else {
const accordionItemIDs = [...this.el.children]
.filter((el) => el.tagName.toLowerCase() === 'va-accordion-item')
.map((el) => el.id);
.filter(el => el.tagName.toLowerCase() === 'va-accordion-item')
.map(el => el.id);
return (
<Host>
{!openSingle && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class VaAdditionalInfo {
}

// Ensures that the CSS animation is consistent and uses the correct max-height for its content
updateInfoMaxHeight() {
updateInfoMaxHeight(): void {
const infoElm = this.el.shadowRoot.getElementById('info');
/* eslint-disable i18next/no-literal-string */
const contentHeight = infoElm.scrollHeight + 'px';
Expand All @@ -102,8 +102,8 @@ export class VaAdditionalInfo {
render() {
if (this.uswds) {
const infoClass = classnames({
'open': this.open,
'closed': !this.open
open: this.open,
closed: !this.open,
});
return (
<Host>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class VaAlertExpandable {

// Ensures that the CSS animation is consistent and uses the correct max-height for its content
/* eslint-disable i18next/no-literal-string */
updateAlertBodyMaxHeight() {
updateAlertBodyMaxHeight(): void {
const bodyElm = this.el.shadowRoot.getElementById('alert-body');
const contentHeight = bodyElm.scrollHeight + 'px';
// the additional 2em is #alert-body margin-top and margin-bottom when open
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class VaBackToTop {
revealObserver.observe(this.revealPixel);
}

navigateToTop(event) {
navigateToTop(event): void {
event.preventDefault();
// Focus the h1 tag on the page.
const el = document.querySelector('h1');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@ export class VaBanner {
componentLibraryAnalytics: EventEmitter;

/* eslint-disable-next-line i18next/no-literal-string */
private prepareBannerID = () => `${this.headline}:${this.el.innerHTML}`;
private prepareBannerID = (): string =>
`${this.headline}:${this.el.innerHTML}`;

private dismiss = () => {
private dismiss = (): string => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This returns void, not a string.

// Derive the current banner ID.
const currentBannerID = this.prepareBannerID();

Expand Down
Loading
Loading