-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathComponentSidebarSection.js
68 lines (59 loc) · 1.95 KB
/
ComponentSidebarSection.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
import _ from 'lodash'
import PropTypes from 'prop-types'
import React, { PureComponent } from 'react'
import { Accordion, Icon, Menu } from 'semantic-ui-react'
export default class ComponentSidebarSection extends PureComponent {
state = {}
static getDerivedStateFromProps(props, state) {
const isActiveByProps = (props.examples || []).some(
(item) => item.examplePath === props.activePath,
)
const didCloseByProps = state.isActiveByProps && !isActiveByProps
// We allow the user to open accordions, but we close them when we scroll passed them
return {
isActiveByProps,
isActiveByUser: didCloseByProps ? false : state.isActiveByUser,
}
}
handleItemClick = (examplePath) => (e) => {
this.props.onItemClick?.(e, { examplePath })
}
handleTitleClick = () => {
this.setState((prevState) => ({ isActiveByUser: !prevState.isActiveByUser }))
}
render() {
const { activePath, examples, sectionName } = this.props
const { isActiveByProps, isActiveByUser } = this.state
const active = isActiveByUser || isActiveByProps
return (
<Menu.Item>
<Accordion.Title active={active} onClick={this.handleTitleClick}>
<b>{sectionName}</b>
<Icon name='dropdown' />
</Accordion.Title>
<Accordion.Content as={Menu.Menu} active={active}>
{_.map(examples, ({ title, examplePath }) => (
<Menu.Item
key={examplePath}
active={activePath === examplePath}
content={title}
onClick={this.handleItemClick(examplePath)}
/>
))}
</Accordion.Content>
</Menu.Item>
)
}
}
ComponentSidebarSection.propTypes = {
activePath: PropTypes.string,
examples: PropTypes.arrayOf(
PropTypes.shape({
title: PropTypes.string,
examplePath: PropTypes.string,
}),
),
sectionName: PropTypes.string,
onItemClick: PropTypes.func,
onTitleClick: PropTypes.func,
}