Skip to content

Commit 27e64b7

Browse files
committed
Update AccordionItem to use hooks
1 parent 98a9889 commit 27e64b7

File tree

1 file changed

+23
-40
lines changed

1 file changed

+23
-40
lines changed

src/components/AccordionItem.tsx

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,14 @@ type Props = DivAttributes & {
1515
dangerouslySetExpanded?: boolean;
1616
};
1717

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;
18+
const AccordionItem = ({
19+
uuid = nextUuid(),
20+
dangerouslySetExpanded,
21+
className = 'accordion__item',
22+
activeClassName,
23+
...rest
24+
}: Props): JSX.Element => {
25+
const renderChildren = (itemContext: ItemContext): JSX.Element => {
3726
const { expanded } = itemContext;
3827
const cx = expanded && activeClassName ? activeClassName : className;
3928

@@ -46,26 +35,20 @@ export default class AccordionItem extends React.Component<Props> {
4635
);
4736
};
4837

49-
render(): JSX.Element {
50-
const {
51-
uuid = this.instanceUuid,
52-
dangerouslySetExpanded,
53-
...rest
54-
} = this.props;
55-
56-
assertValidHtmlId(uuid);
38+
assertValidHtmlId(uuid);
39+
if (rest.id) {
40+
assertValidHtmlId(rest.id);
41+
}
42+
return (
43+
<ItemProvider
44+
uuid={uuid}
45+
dangerouslySetExpanded={dangerouslySetExpanded}
46+
>
47+
<ItemConsumer>{renderChildren}</ItemConsumer>
48+
</ItemProvider>
49+
);
50+
};
5751

58-
if (rest.id) {
59-
assertValidHtmlId(rest.id);
60-
}
52+
AccordionItem.displayName = DisplayName.AccordionItem;
6153

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

0 commit comments

Comments
 (0)