Skip to content
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
15 changes: 15 additions & 0 deletions demo/src/code-examples.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ export const ExampleAllowMultipleExpanded = `<Accordion allowMultipleExpanded>
))}
</Accordion>`;

export const ExampleAllowMultipleExpandedFalse = `<Accordion allowMultipleExpanded={false}>
{items.map((item) => (
<AccordionItem key={item.uuid}>
<AccordionItemHeading>
<AccordionItemButton>
{item.heading}
</AccordionItemButton>
</AccordionItemHeading>
<AccordionItemPanel>
{item.content}
</AccordionItemPanel>
</AccordionItem>
))}
</Accordion>`;

export const ExampleAllowZeroExpanded = `<Accordion allowZeroExpanded>
{items.map((item) => (
<AccordionItem key={item.uuid}>
Expand Down
20 changes: 20 additions & 0 deletions demo/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import Code from './components/Code';
import {
ExampleDefault,
ExampleAllowMultipleExpanded,
ExampleAllowMultipleExpandedFalse,
ExampleAllowZeroExpanded,
ExamplePreExpanded,
ExampleOnChange,
Expand Down Expand Up @@ -95,6 +96,25 @@ const App = (): JSX.Element => (

<Code code={ExampleAllowMultipleExpanded} />

<h2 className="u-margin-top">
Same as above except with allowMultipleExpanded=false
</h2>

<Accordion allowMultipleExpanded={false}>
{placeholders.map((placeholder: Placeholder) => (
<AccordionItem key={placeholder.heading}>
<AccordionItemHeading>
<AccordionItemButton>
{placeholder.heading}
</AccordionItemButton>
</AccordionItemHeading>
<AccordionItemPanel>{placeholder.panel}</AccordionItemPanel>
</AccordionItem>
))}
</Accordion>

<Code code={ExampleAllowMultipleExpandedFalse} />

<h2 className="u-margin-top">Collapsing the last expanded item</h2>

<p>
Expand Down
63 changes: 41 additions & 22 deletions src/components/AccordionItem.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import DisplayName from '../helpers/DisplayName';
import { DivAttributes } from '../helpers/types';
import { assertValidHtmlId, nextUuid } from '../helpers/uuid';
import {
Expand All @@ -9,20 +10,30 @@ import {
} from './ItemContext';

type Props = DivAttributes & {
className?: string;
uuid?: UUID;
activeClassName?: string;
dangerouslySetExpanded?: boolean;
};

const AccordionItem = ({
uuid = nextUuid(),
className = 'accordion__item',
activeClassName,
dangerouslySetExpanded,
...rest
}: Props): JSX.Element => {
const renderChildren = (itemContext: ItemContext): JSX.Element => {
const defaultProps = {
className: 'accordion__item',
};

export default class AccordionItem extends React.Component<Props> {
static defaultProps: typeof defaultProps = defaultProps;

static displayName: DisplayName.AccordionItem = DisplayName.AccordionItem;

instanceUuid: UUID = nextUuid();

renderChildren = (itemContext: ItemContext): JSX.Element => {
const {
uuid,
className,
activeClassName,
dangerouslySetExpanded,
...rest
} = this.props;
const { expanded } = itemContext;
const cx = expanded && activeClassName ? activeClassName : className;

Expand All @@ -35,18 +46,26 @@ const AccordionItem = ({
);
};

if (rest.id) {
assertValidHtmlId(rest.id);
}
render(): JSX.Element {
const {
uuid = this.instanceUuid,
dangerouslySetExpanded,
...rest
} = this.props;

return (
<ItemProvider
uuid={uuid}
dangerouslySetExpanded={dangerouslySetExpanded}
>
<ItemConsumer>{renderChildren}</ItemConsumer>
</ItemProvider>
);
};
assertValidHtmlId(uuid);

if (rest.id) {
assertValidHtmlId(rest.id);
}

export default AccordionItem;
return (
<ItemProvider
uuid={uuid}
dangerouslySetExpanded={dangerouslySetExpanded}
>
<ItemConsumer>{this.renderChildren}</ItemConsumer>
</ItemProvider>
);
}
}