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

fix(accordion): update tests correct functionality #2035

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/smart-swans-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rhds/elements": patch
---

`<rh-accordion>`: corrected use of `expanded-index` and `expanded`
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ <h2><rh-accordion-header expanded>Item One</rh-accordion-header></h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
</rh-accordion-panel>

<h2><rh-accordion-header>Item Two</rh-accordion-header></h2>
<h2><rh-accordion-header expanded>Item Two</rh-accordion-header></h2>
<rh-accordion-panel>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
</rh-accordion-panel>
Expand Down
21 changes: 21 additions & 0 deletions elements/rh-accordion/demo/expanded-index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<rh-accordion expanded-index="0, 2">

<h2><rh-accordion-header>Item One</rh-accordion-header></h2>
<rh-accordion-panel>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
</rh-accordion-panel>

<h2><rh-accordion-header>Item Two</rh-accordion-header></h2>
<rh-accordion-panel>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
</rh-accordion-panel>

<h2><rh-accordion-header>Item Three</rh-accordion-header></h2>
<rh-accordion-panel>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
</rh-accordion-panel>
</rh-accordion>

<script type="module">
import '@rhds/elements/rh-accordion/rh-accordion.js';
</script>
6 changes: 3 additions & 3 deletions elements/rh-accordion/demo/rh-accordion.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<rh-accordion>

<!-- H2 tags will be removed on upgrade, rh-accordion-header will set aria-level internally using the header tag that wraps it -->
<h2><rh-accordion-header>Item One</rh-accordion-header></h2>
<rh-accordion-panel>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
</rh-accordion-panel>

<!-- H2 tags will be removed on upgrade, rh-accordion-header will set aria-level internally using the header tag that wraps it -->
<h2><rh-accordion-header>Item Two</rh-accordion-header></h2>
<rh-accordion-panel>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
</rh-accordion-panel>

<!-- H2 tags will be removed on upgrade, rh-accordion-header will set aria-level internally using the header tag that wraps it -->
<h2><rh-accordion-header>Item Three</rh-accordion-header></h2>
<rh-accordion-panel>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
Expand Down
26 changes: 19 additions & 7 deletions elements/rh-accordion/rh-accordion-header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ import { property } from 'lit/decorators/property.js';
import { getRandomId } from '@patternfly/pfe-core/functions/random.js';
import { observes } from '@patternfly/pfe-core/decorators/observes.js';

import { HeadingLevelController } from '@rhds/elements/lib/context/headings/controller.js';
import { InternalsController } from '@patternfly/pfe-core/controllers/internals-controller.js';

import { DirController } from '../../lib/DirController.js';
import { colorContextConsumer, type ColorTheme } from '../../lib/context/color/consumer.js';

import { consume } from '@lit/context';

import { context } from './context.js';

import styles from './rh-accordion-header.css';
import { HeadingLevelController } from '@rhds/elements/lib/context/headings/controller.js';

export class AccordionHeaderChangeEvent extends Event {
declare target: RhAccordionHeader;
Expand Down Expand Up @@ -49,6 +49,12 @@ const isAccordion = (x: EventTarget): x is RhAccordion =>
export class RhAccordionHeader extends LitElement {
static readonly styles = [styles];

// Allow focus to apply to shadow button
static override readonly shadowRootOptions: ShadowRootInit = {
...LitElement.shadowRootOptions,
delegatesFocus: true,
};

@property({ type: Boolean, reflect: true }) expanded = false;

@consume({ context, subscribe: true })
Expand All @@ -67,12 +73,14 @@ export class RhAccordionHeader extends LitElement {

#heading = new HeadingLevelController(this);

#belongsTo?: RhAccordion | null;

override connectedCallback() {
super.connectedCallback();
this.id ||= getRandomId(this.localName);
const accordion = this.closest('rh-accordion');
this.#belongsTo = this.closest<RhAccordion>('rh-accordion');
const heading = this.closest('h1,h2,h3,h4,h5,h6');
if (heading && accordion?.contains(heading)) {
if (heading && this.#belongsTo?.contains(heading)) {
this.#internals.ariaLevel = heading.localName.replace('h', '');
heading.replaceWith(this);
} else {
Expand Down Expand Up @@ -102,16 +110,20 @@ export class RhAccordionHeader extends LitElement {
`;
}

#onClick(event: MouseEvent) {
const accordion = event.composedPath().find(isAccordion);
#onClick() {
this.expanded = !this.expanded;
}

#dispatchChange(accordion?: RhAccordion | null) {
if (accordion) {
this.dispatchEvent(new AccordionHeaderChangeEvent(!this.expanded, this, accordion));
this.dispatchEvent(new AccordionHeaderChangeEvent(this.expanded, this, accordion));
}
}

@observes('expanded')
private expandedChanged() {
this.#internals.ariaExpanded = String(!!this.expanded) as 'true' | 'false';
this.#dispatchChange(this.#belongsTo);
}
}

Expand Down
10 changes: 7 additions & 3 deletions elements/rh-accordion/rh-accordion-panel.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import { html, LitElement } from 'lit';
import { classMap } from 'lit/directives/class-map.js';

import { customElement } from 'lit/decorators/custom-element.js';
import { property } from 'lit/decorators/property.js';

import { colorContextConsumer, type ColorTheme } from '../../lib/context/color/consumer.js';
import { colorContextProvider, type ColorPalette } from '../../lib/context/color/provider.js';

import { getRandomId } from '@patternfly/pfe-core/functions/random.js';

import styles from './rh-accordion-panel.css';
import { observes } from '@patternfly/pfe-core/decorators/observes.js';

import { consume } from '@lit/context';
import { context, type RhAccordionContext } from './context.js';

import styles from './rh-accordion-panel.css';
/**
* Accordion Panel
*
Expand Down Expand Up @@ -53,6 +52,11 @@ export class RhAccordionPanel extends LitElement {
</div>
`;
}

@observes('expanded')
private expandedChanged() {
this.hidden = !this.expanded;
}
}

declare global {
Expand Down
65 changes: 40 additions & 25 deletions elements/rh-accordion/rh-accordion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import { RhAccordionPanel } from './rh-accordion-panel.js';

import { context, type RhAccordionContext } from './context.js';

export * from './rh-accordion-header.js';
export * from './rh-accordion-panel.js';

import styles from './rh-accordion.css';

export class AccordionExpandEvent extends ComposedEvent {
Expand Down Expand Up @@ -111,16 +114,6 @@ export class RhAccordion extends LitElement {

set expandedIndex(value) {
this.#expandedIndex = value;
this.#expanded = !!this.#expandedIndex.length;
this.headers.forEach((header, i) => {
const expanded = this.#expandedIndexSet.has(i);
header.expanded = expanded;
const panel = this.panels[i];
if (panel) {
panel.expanded = expanded;
panel.hidden = !expanded;
}
});
}

/** All headers for this accordion */
Expand Down Expand Up @@ -174,14 +167,14 @@ export class RhAccordion extends LitElement {
return c && results.every(Boolean);
}

override firstUpdated() {
this.headers.forEach((header, index) => {
if (header.expanded) {
this.#expandedIndexSet.add(index);
@observes('expandedIndex')
private updateExpanded() {
this.#expandedIndex.forEach(headerIndex => {
if (!this.headers[headerIndex]) {
return;
}
this.#expand(headerIndex);
});
this.expandedIndex = [...this.#expandedIndexSet];
this.#expanded = !!this.#expandedIndex.length;
}

@observes('accents')
Expand Down Expand Up @@ -209,25 +202,47 @@ export class RhAccordion extends LitElement {

#expand(index: number) {
// If this index is not already listed in the expandedSets array, add it
this.expandedIndex = [...this.#expandedIndexSet.add(index)];
if (this.#expandedIndexSet.has(index)) {
return;
}

this.#expandedIndexSet.add(index);

const header = this.headers[index];
const panel = this.panels[index];

if (header && panel) {
header.expanded = true;
panel.expanded = true;
}
}

#collapse(index: number) {
if (this.#expandedIndexSet.delete(index)) {
this.expandedIndex = [...this.#expandedIndexSet];
if (!this.#expandedIndexSet.has(index)) {
return;
}

const header = this.headers[index];
const panel = this.panels[index];
if (header && panel) {
header.expanded = false;
panel.expanded = false;
}
this.#expandedIndexSet.delete(index);
}

#onChange(event: AccordionHeaderChangeEvent) {
if (RhAccordion.isAccordionChangeEvent(event)) {
const index = this.#getIndex(event.target);

if (event.expanded) {
this.expand(index, event.accordion);
} else {
this.collapse(index);
this.#expand(index);
}

if (!event.expanded) {
this.#collapse(index);
}
}
this.requestUpdate('expandedIndex');
}

#allHeaders(accordion: RhAccordion = this): RhAccordionHeader[] {
Expand Down Expand Up @@ -283,9 +298,9 @@ export class RhAccordion extends LitElement {
const header = headers[index];

if (!header.expanded) {
await this.expand(index);
await this.#expand(index);
} else {
await this.collapse(index);
await this.#collapse(index);
}
}

Expand Down
Loading
Loading