UNPKG

1.15 kBTypeScriptView Raw
1import * as React from 'react';
2
3import WrapperAccordion from './AccordionWrapper';
4
5interface IProps {
6 items: {
7 header: string| React.ReactNode |React.ReactChild;
8 content: string| React.ReactNode |React.ReactChild;
9 labels?: string| React.ReactNode |React.ReactChild;
10 }[];
11 automatic?: boolean;
12}
13
14interface IState {
15 active: null | boolean | number;
16 activeIndex: number | null;
17}
18
19export default class Accordion extends React.PureComponent<IProps, IState> {
20 state = {
21 active: false,
22 activeIndex: null,
23 }
24
25 toggle = (e: boolean, index: number) => {
26 this.setState(({ active: e, activeIndex: index }));
27 if (e && this.props.automatic && this.state.activeIndex === index) {
28 this.setState({ activeIndex: null });
29 }
30 }
31
32 render = () => {
33 return this.props.items.map((item, index) => (
34 <WrapperAccordion
35 active={index}
36 activeIndex={this.state.activeIndex}
37 key={index}
38 click={(e) => this.toggle(e, index)}
39 header={item.header}
40 content={item.content}
41 labels={item.labels}
42 automatic={this.props.automatic}
43 />
44 ));
45 }
46}
\No newline at end of file