diff options
Diffstat (limited to 'src/components/ui/Tabs/Tabs.tsx')
-rw-r--r-- | src/components/ui/Tabs/Tabs.tsx | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src/components/ui/Tabs/Tabs.tsx b/src/components/ui/Tabs/Tabs.tsx new file mode 100644 index 000000000..2c34d7e24 --- /dev/null +++ b/src/components/ui/Tabs/Tabs.tsx | |||
@@ -0,0 +1,75 @@ | |||
1 | import { Children, Component, ReactElement, ReactPortal } from 'react'; | ||
2 | import { observer } from 'mobx-react'; | ||
3 | import classnames from 'classnames'; | ||
4 | import { IProps as TabItemProps } from './TabItem'; | ||
5 | |||
6 | interface IProps { | ||
7 | children: | ||
8 | | ReactElement<TabItemProps> | ||
9 | | (boolean | ReactElement<TabItemProps>)[]; | ||
10 | active?: number; | ||
11 | } | ||
12 | |||
13 | interface IState { | ||
14 | active: number; | ||
15 | } | ||
16 | |||
17 | @observer | ||
18 | class Tab extends Component<IProps, IState> { | ||
19 | constructor(props: IProps) { | ||
20 | super(props); | ||
21 | |||
22 | this.state = { | ||
23 | active: this.props.active || 0, | ||
24 | }; | ||
25 | } | ||
26 | |||
27 | switchTab(index: number): void { | ||
28 | this.setState({ active: index }); | ||
29 | } | ||
30 | |||
31 | render(): ReactElement { | ||
32 | const { children: childElements } = this.props; | ||
33 | const children = Children.toArray(childElements); // removes all null values | ||
34 | |||
35 | if (children.length === 1) { | ||
36 | return <div>{children}</div>; | ||
37 | } | ||
38 | |||
39 | return ( | ||
40 | <div className="content-tabs"> | ||
41 | <div className="content-tabs__tabs"> | ||
42 | {Children.map(children, (child, i) => ( | ||
43 | <button | ||
44 | key={i} | ||
45 | className={classnames({ | ||
46 | 'content-tabs__item': true, | ||
47 | 'is-active': this.state.active === i, | ||
48 | })} | ||
49 | onClick={() => this.switchTab(i)} | ||
50 | type="button" | ||
51 | > | ||
52 | {(child as ReactPortal).props.title} | ||
53 | </button> | ||
54 | ))} | ||
55 | </div> | ||
56 | <div className="content-tabs__content"> | ||
57 | {Children.map(children, (child, i) => ( | ||
58 | <div | ||
59 | key={i} | ||
60 | className={classnames({ | ||
61 | 'content-tabs__item': true, | ||
62 | 'is-active': this.state.active === i, | ||
63 | })} | ||
64 | // type="button" | ||
65 | > | ||
66 | {child} | ||
67 | </div> | ||
68 | ))} | ||
69 | </div> | ||
70 | </div> | ||
71 | ); | ||
72 | } | ||
73 | } | ||
74 | |||
75 | export default Tab; | ||