diff --git a/demo/src/code-examples.tsx b/demo/src/code-examples.tsx index 96d0e0e..37c7970 100644 --- a/demo/src/code-examples.tsx +++ b/demo/src/code-examples.tsx @@ -36,6 +36,21 @@ export const ExampleAllowMultipleExpanded = ` ))} `; +export const ExampleAllowMultipleExpandedFalse = ` + {items.map((item) => ( + + + + {item.heading} + + + + {item.content} + + + ))} +`; + export const ExampleAllowZeroExpanded = ` {items.map((item) => ( diff --git a/demo/src/index.tsx b/demo/src/index.tsx index b21cdc9..e61a455 100644 --- a/demo/src/index.tsx +++ b/demo/src/index.tsx @@ -19,6 +19,7 @@ import Code from './components/Code'; import { ExampleDefault, ExampleAllowMultipleExpanded, + ExampleAllowMultipleExpandedFalse, ExampleAllowZeroExpanded, ExamplePreExpanded, ExampleOnChange, @@ -95,6 +96,25 @@ const App = (): JSX.Element => ( +

+ Same as above except with allowMultipleExpanded=false +

+ + + {placeholders.map((placeholder: Placeholder) => ( + + + + {placeholder.heading} + + + {placeholder.panel} + + ))} + + + +

Collapsing the last expanded item

diff --git a/src/components/AccordionItem.tsx b/src/components/AccordionItem.tsx index 6799dd8..f818ced 100644 --- a/src/components/AccordionItem.tsx +++ b/src/components/AccordionItem.tsx @@ -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 { @@ -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 { + 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; @@ -35,18 +46,26 @@ const AccordionItem = ({ ); }; - if (rest.id) { - assertValidHtmlId(rest.id); - } + render(): JSX.Element { + const { + uuid = this.instanceUuid, + dangerouslySetExpanded, + ...rest + } = this.props; - return ( - - {renderChildren} - - ); -}; + assertValidHtmlId(uuid); + + if (rest.id) { + assertValidHtmlId(rest.id); + } -export default AccordionItem; + return ( + + {this.renderChildren} + + ); + } +}