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): adds prop to heading level of parent component #2290

Merged
merged 1 commit into from
Jun 18, 2019
Merged
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 @@ -2,13 +2,28 @@ import React from 'react';
import { shallow, mount } from 'enzyme';
import { Accordion } from './Accordion';
import { AccordionToggle } from './AccordionToggle';
import { AccordionContent } from './AccordionContent';
import { AccordionItem } from './AccordionItem';

describe('Accordion', () => {
test('Accordion default', () => {
const view = shallow(<Accordion aria-label="this is a simple accordion" />);
expect(view.render()).toMatchSnapshot();
});

test('Accordion with non-default headingLevel', () => {
const view = shallow(
<Accordion headingLevel="h2">
<AccordionItem>
<AccordionToggle id="item-1">Item One</AccordionToggle>
<AccordionContent>Item One Content</AccordionContent>
</AccordionItem>
</Accordion>
);
expect(view.render()).toMatchSnapshot();
});


test('It should pass optional aria props', () => {
const view = mount(
<AccordionToggle aria-label="Toggle details for" aria-labelledby="ex-toggle2 ex-item2" id="ex-toggle2" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,28 @@ import * as React from 'react';
import { css } from '@patternfly/react-styles';
import styles from '@patternfly/react-styles/css/components/Accordion/accordion';

export const AccordionContext = React.createContext('h3');

export interface AccordionProps extends React.HTMLProps<HTMLDListElement> {
/** Content rendered inside the Accordion */
children?: React.ReactNode;
/** Additional classes added to the Accordion */
className?: string;
/** Adds accessible text to the Accordion */
'aria-label'?: string;
/** the heading level to use */
headingLevel?: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6';
}

export const Accordion: React.FunctionComponent<AccordionProps> = ({
children = null,
className = '',
'aria-label': ariaLabel = '',
headingLevel = 'h3',
...props
}: AccordionProps) => (
<dl className={css(styles.accordion, className)} aria-label={ariaLabel} {...props}>
<AccordionContext.Provider value={headingLevel}>{children}</AccordionContext.Provider>
{children}
</dl>
);
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { css } from '@patternfly/react-styles';
import styles from '@patternfly/react-styles/css/components/Accordion/accordion';
import { AngleRightIcon } from '@patternfly/react-icons';
import { Omit } from '../../helpers/typeUtils';
import { AccordionContext } from './Accordion';

export interface AccordionToggleProps extends Omit<React.HTMLProps<HTMLButtonElement>, 'type'> {
/** Content rendered inside the Accordion toggle */
Expand All @@ -23,16 +24,20 @@ export const AccordionToggle: React.FunctionComponent<AccordionToggleProps> = ({
...props
}: AccordionToggleProps) => (
<dt>
<h3>
<button
id={id}
className={css(styles.accordionToggle, isExpanded && styles.modifiers.expanded, className)}
{...props}
aria-expanded={isExpanded}
>
<span className={css(styles.accordionToggleText)}>{children}</span>
<AngleRightIcon className={css(styles.accordionToggleIcon)} />
</button>
</h3>
<AccordionContext.Consumer>
{(AccordionHeadingLevel: any) => (
<AccordionHeadingLevel>
<button
id={id}
className={css(styles.accordionToggle, isExpanded && styles.modifiers.expanded, className)}
{...props}
aria-expanded={isExpanded}
>
<span className={css(styles.accordionToggleText)}>{children}</span>
<AngleRightIcon className={css(styles.accordionToggleIcon)} />
</button>
</AccordionHeadingLevel>
)}
</AccordionContext.Consumer>
</dt>
);
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,93 @@ exports[`Accordion Accordion default 1`] = `
class="pf-c-accordion"
/>
`;

exports[`Accordion Accordion with non-default headingLevel 1`] = `
<dl
aria-label=""
class="pf-c-accordion"
>
<dt>
<h2>
<button
aria-expanded="false"
class="pf-c-accordion__toggle"
id="item-1"
>
<span
class="pf-c-accordion__toggle-text"
>
Item One
</span>
<svg
aria-hidden="true"
class="pf-c-accordion__toggle-icon"
fill="currentColor"
height="1em"
role="img"
style="vertical-align:-0.125em"
viewBox="0 0 256 512"
width="1em"
>
<path
d="M224.3 273l-136 136c-9.4 9.4-24.6 9.4-33.9 0l-22.6-22.6c-9.4-9.4-9.4-24.6 0-33.9l96.4-96.4-96.4-96.4c-9.4-9.4-9.4-24.6 0-33.9L54.3 103c9.4-9.4 24.6-9.4 33.9 0l136 136c9.5 9.4 9.5 24.6.1 34z"
transform=""
/>
</svg>
</button>
</h2>
</dt>
<dd
aria-label=""
class="pf-c-accordion__expanded-content pf-m-expanded"
id=""
>
<div
class="pf-c-accordion__expanded-content-body"
>
Item One Content
</div>
</dd>
<dt>
<h3>
<button
aria-expanded="false"
class="pf-c-accordion__toggle"
id="item-1"
>
<span
class="pf-c-accordion__toggle-text"
>
Item One
</span>
<svg
aria-hidden="true"
class="pf-c-accordion__toggle-icon"
fill="currentColor"
height="1em"
role="img"
style="vertical-align:-0.125em"
viewBox="0 0 256 512"
width="1em"
>
<path
d="M224.3 273l-136 136c-9.4 9.4-24.6 9.4-33.9 0l-22.6-22.6c-9.4-9.4-9.4-24.6 0-33.9l96.4-96.4-96.4-96.4c-9.4-9.4-9.4-24.6 0-33.9L54.3 103c9.4-9.4 24.6-9.4 33.9 0l136 136c9.5 9.4 9.5 24.6.1 34z"
transform=""
/>
</svg>
</button>
</h3>
</dt>
<dd
aria-label=""
class="pf-c-accordion__expanded-content pf-m-expanded"
id=""
>
<div
class="pf-c-accordion__expanded-content-body"
>
Item One Content
</div>
</dd>
</dl>
`;