Skip to content

Commit

Permalink
AccordionItem preserving uuid on instance
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Holloway committed Aug 6, 2020
1 parent 1136095 commit a43f831
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 22 deletions.
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
65 changes: 43 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,23 +10,35 @@ 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;

console.log({ rest });

return (
<div
data-accordion-component="AccordionItem"
Expand All @@ -35,18 +48,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);

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

0 comments on commit a43f831

Please sign in to comment.