Skip to content

Commit 98a9889

Browse files
author
Matthew Holloway
authored
Merge pull request #285 from springload/bug/accordion-item-instance-uuid
AccordionItem preserving uuid on instance
2 parents 1136095 + 5c1b365 commit 98a9889

File tree

3 files changed

+76
-22
lines changed

3 files changed

+76
-22
lines changed

demo/src/code-examples.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,21 @@ export const ExampleAllowMultipleExpanded = `<Accordion allowMultipleExpanded>
3636
))}
3737
</Accordion>`;
3838

39+
export const ExampleAllowMultipleExpandedFalse = `<Accordion allowMultipleExpanded={false}>
40+
{items.map((item) => (
41+
<AccordionItem key={item.uuid}>
42+
<AccordionItemHeading>
43+
<AccordionItemButton>
44+
{item.heading}
45+
</AccordionItemButton>
46+
</AccordionItemHeading>
47+
<AccordionItemPanel>
48+
{item.content}
49+
</AccordionItemPanel>
50+
</AccordionItem>
51+
))}
52+
</Accordion>`;
53+
3954
export const ExampleAllowZeroExpanded = `<Accordion allowZeroExpanded>
4055
{items.map((item) => (
4156
<AccordionItem key={item.uuid}>

demo/src/index.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import Code from './components/Code';
1919
import {
2020
ExampleDefault,
2121
ExampleAllowMultipleExpanded,
22+
ExampleAllowMultipleExpandedFalse,
2223
ExampleAllowZeroExpanded,
2324
ExamplePreExpanded,
2425
ExampleOnChange,
@@ -95,6 +96,25 @@ const App = (): JSX.Element => (
9596

9697
<Code code={ExampleAllowMultipleExpanded} />
9798

99+
<h2 className="u-margin-top">
100+
Same as above except with allowMultipleExpanded=false
101+
</h2>
102+
103+
<Accordion allowMultipleExpanded={false}>
104+
{placeholders.map((placeholder: Placeholder) => (
105+
<AccordionItem key={placeholder.heading}>
106+
<AccordionItemHeading>
107+
<AccordionItemButton>
108+
{placeholder.heading}
109+
</AccordionItemButton>
110+
</AccordionItemHeading>
111+
<AccordionItemPanel>{placeholder.panel}</AccordionItemPanel>
112+
</AccordionItem>
113+
))}
114+
</Accordion>
115+
116+
<Code code={ExampleAllowMultipleExpandedFalse} />
117+
98118
<h2 className="u-margin-top">Collapsing the last expanded item</h2>
99119

100120
<p>

src/components/AccordionItem.tsx

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as React from 'react';
2+
import DisplayName from '../helpers/DisplayName';
23
import { DivAttributes } from '../helpers/types';
34
import { assertValidHtmlId, nextUuid } from '../helpers/uuid';
45
import {
@@ -9,20 +10,30 @@ import {
910
} from './ItemContext';
1011

1112
type Props = DivAttributes & {
12-
className?: string;
1313
uuid?: UUID;
1414
activeClassName?: string;
1515
dangerouslySetExpanded?: boolean;
1616
};
1717

18-
const AccordionItem = ({
19-
uuid = nextUuid(),
20-
className = 'accordion__item',
21-
activeClassName,
22-
dangerouslySetExpanded,
23-
...rest
24-
}: Props): JSX.Element => {
25-
const renderChildren = (itemContext: ItemContext): JSX.Element => {
18+
const defaultProps = {
19+
className: 'accordion__item',
20+
};
21+
22+
export default class AccordionItem extends React.Component<Props> {
23+
static defaultProps: typeof defaultProps = defaultProps;
24+
25+
static displayName: DisplayName.AccordionItem = DisplayName.AccordionItem;
26+
27+
instanceUuid: UUID = nextUuid();
28+
29+
renderChildren = (itemContext: ItemContext): JSX.Element => {
30+
const {
31+
uuid,
32+
className,
33+
activeClassName,
34+
dangerouslySetExpanded,
35+
...rest
36+
} = this.props;
2637
const { expanded } = itemContext;
2738
const cx = expanded && activeClassName ? activeClassName : className;
2839

@@ -35,18 +46,26 @@ const AccordionItem = ({
3546
);
3647
};
3748

38-
if (rest.id) {
39-
assertValidHtmlId(rest.id);
40-
}
49+
render(): JSX.Element {
50+
const {
51+
uuid = this.instanceUuid,
52+
dangerouslySetExpanded,
53+
...rest
54+
} = this.props;
4155

42-
return (
43-
<ItemProvider
44-
uuid={uuid}
45-
dangerouslySetExpanded={dangerouslySetExpanded}
46-
>
47-
<ItemConsumer>{renderChildren}</ItemConsumer>
48-
</ItemProvider>
49-
);
50-
};
56+
assertValidHtmlId(uuid);
57+
58+
if (rest.id) {
59+
assertValidHtmlId(rest.id);
60+
}
5161

52-
export default AccordionItem;
62+
return (
63+
<ItemProvider
64+
uuid={uuid}
65+
dangerouslySetExpanded={dangerouslySetExpanded}
66+
>
67+
<ItemConsumer>{this.renderChildren}</ItemConsumer>
68+
</ItemProvider>
69+
);
70+
}
71+
}

0 commit comments

Comments
 (0)