Skip to content

Commit a43f831

Browse files
author
Matthew Holloway
committed
AccordionItem preserving uuid on instance
1 parent 1136095 commit a43f831

File tree

3 files changed

+78
-22
lines changed

3 files changed

+78
-22
lines changed

demo/src/code-examples.tsx

+15
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

+20
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

+43-22
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,23 +10,35 @@ 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

40+
console.log({ rest });
41+
2942
return (
3043
<div
3144
data-accordion-component="AccordionItem"
@@ -35,18 +48,26 @@ const AccordionItem = ({
3548
);
3649
};
3750

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

42-
return (
43-
<ItemProvider
44-
uuid={uuid}
45-
dangerouslySetExpanded={dangerouslySetExpanded}
46-
>
47-
<ItemConsumer>{renderChildren}</ItemConsumer>
48-
</ItemProvider>
49-
);
50-
};
58+
assertValidHtmlId(uuid);
5159

52-
export default AccordionItem;
60+
if (rest.id) {
61+
assertValidHtmlId(rest.id);
62+
}
63+
``;
64+
return (
65+
<ItemProvider
66+
uuid={uuid}
67+
dangerouslySetExpanded={dangerouslySetExpanded}
68+
>
69+
<ItemConsumer>{this.renderChildren}</ItemConsumer>
70+
</ItemProvider>
71+
);
72+
}
73+
}

0 commit comments

Comments
 (0)