1 | import * as React from 'react';
|
2 | import { HeaderTabs, Marker, ActiveComponent } from './Tabs.styled';
|
3 |
|
4 | interface IProps {
|
5 | headers: string[];
|
6 | }
|
7 |
|
8 | export default class extends React.PureComponent<IProps> {
|
9 | static Panel = ActiveComponent;
|
10 |
|
11 | state = {
|
12 | activePanel: '',
|
13 | marker: {
|
14 | width: 0,
|
15 | index: 0
|
16 | }
|
17 | }
|
18 | tab = React.createRef();
|
19 |
|
20 | changeTabState = (element: HTMLLIElement) => this.setState({
|
21 | marker: { width: element.clientWidth, index: element.offsetLeft },
|
22 | activePanel: element.textContent
|
23 | })
|
24 |
|
25 | activatePanel = ({ currentTarget }: React.MouseEvent<HTMLLIElement, MouseEvent>) =>
|
26 | this.changeTabState((currentTarget as HTMLLIElement));
|
27 |
|
28 | componentDidMount() {
|
29 |
|
30 | this.changeTabState((this.tab.current.children[0] as HTMLLIElement));
|
31 | }
|
32 |
|
33 | render = () => {
|
34 | const { children, headers } = this.props;
|
35 | const { activePanel, marker } = this.state;
|
36 | return (
|
37 | <React.Fragment>
|
38 | <HeaderTabs
|
39 |
|
40 | ref={this.tab}
|
41 | >
|
42 | {headers.map((header, index) => (
|
43 | <li
|
44 | onClick={this.activatePanel}
|
45 | role='button'
|
46 | key={index}
|
47 | className={`tab-header-item-${index}`}
|
48 | >
|
49 | <a>{header}</a>
|
50 | </li>
|
51 | ))}
|
52 | <Marker marked={marker} />
|
53 | </HeaderTabs>
|
54 | { React.Children.map(children, (child: any) => (child.props.fromHeader === activePanel) && child) }
|
55 | </React.Fragment>
|
56 | )
|
57 | }
|
58 | };
|