-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
71 lines (60 loc) · 1.77 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/** @jsx dom */
import deepEqual from 'deep-equal';
import dom from 'magic-virtual-element';
import objectAssign from 'object-assign';
const propTypes = {
class: {
type: 'string'
},
items: {
type: 'array'
},
onClick: {
type: 'function'
}
};
const initialState = () => {
return {
activeTab: 0
};
};
const onHeadingClick = (x, activeTab, i, setState, onClick) => () => {
if (activeTab === i) {
return;
}
setState({activeTab: i});
if (onClick) {
onClick(objectAssign(x, {i}));
}
};
const getHeadings = ({items, onClick}, {activeTab}, setState) => {
return items.map((x, i) => (
<div class={['Tabs-heading', {'Tabs-heading--active': activeTab === i}]} onClick={onHeadingClick(x, activeTab, i, setState, onClick)}>
{x.heading}
</div>
));
};
const getTabs = ({items}, {activeTab}) => {
return items.map(({content}, i) => (
<div class={['Tab', {'Tab--active': activeTab === i}]} style={{display: activeTab === i ? 'block' : 'none'}}>
{content}
</div>
));
};
const setActive = (items, setState) => items.forEach((el, i) => el.active === true && setState({activeTab: i}));
const afterMount = ({props}, el, setState) => setActive(props.items, setState);
const shouldUpdate = ({props, state}, nextProps, {activeTab}) => !deepEqual(props, nextProps) || state.activeTab !== activeTab;
const afterUpdate = ({props}, prevProps, prevState, setState) => !deepEqual(props, prevProps) && setActive(props.items, setState);
const render = ({props, state}, setState) => {
return (
<div class={['Tabs', props.class]}>
<div class='Tabs-headings'>
{getHeadings(props, state, setState)}
</div>
<div class='Tabs-content'>
{getTabs(props, state)}
</div>
</div>
);
};
export default {afterMount, afterUpdate, initialState, propTypes, render, shouldUpdate};