/
AccordionAccordion.js
149 lines (122 loc) · 4.21 KB
/
AccordionAccordion.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import cx from 'classnames'
import _ from 'lodash'
import PropTypes from 'prop-types'
import React from 'react'
import {
ModernAutoControlledComponent as Component,
childrenUtils,
createShorthandFactory,
customPropTypes,
getElementType,
getUnhandledProps,
} from '../../lib'
import AccordionPanel from './AccordionPanel'
const warnIfPropsAreInvalid = (props, state) => {
const { exclusive } = props
const { activeIndex } = state
/* eslint-disable no-console */
if (exclusive && typeof activeIndex !== 'number') {
console.error('`activeIndex` must be a number if `exclusive` is true')
} else if (!exclusive && !_.isArray(activeIndex)) {
console.error('`activeIndex` must be an array if `exclusive` is false')
}
/* eslint-enable no-console */
}
/**
* An Accordion can contain sub-accordions.
*/
export default class AccordionAccordion extends Component {
static propTypes = {
/** An element type to render as (string or function). */
as: PropTypes.elementType,
/** Index of the currently active panel. */
activeIndex: customPropTypes.every([
customPropTypes.disallow(['children']),
PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.number), PropTypes.number]),
]),
/** Primary content. */
children: PropTypes.node,
/** Additional classes. */
className: PropTypes.string,
/** Initial activeIndex value. */
defaultActiveIndex: customPropTypes.every([
customPropTypes.disallow(['children']),
PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.number), PropTypes.number]),
]),
/** Only allow one panel open at a time. */
exclusive: PropTypes.bool,
/**
* Called when a panel title is clicked.
*
* @param {SyntheticEvent} event - React's original SyntheticEvent.
* @param {object} data - All item props.
*/
onTitleClick: customPropTypes.every([customPropTypes.disallow(['children']), PropTypes.func]),
/** Shorthand array of props for Accordion. */
panels: customPropTypes.every([
customPropTypes.disallow(['children']),
PropTypes.arrayOf(
PropTypes.shape({
content: customPropTypes.itemShorthand,
title: customPropTypes.itemShorthand,
}),
),
]),
}
static defaultProps = {
exclusive: true,
}
static autoControlledProps = ['activeIndex']
getInitialAutoControlledState({ exclusive }) {
return { activeIndex: exclusive ? -1 : [] }
}
componentDidMount() {
if (process.env.NODE_ENV !== 'production') {
warnIfPropsAreInvalid(this.props, this.state)
}
}
componentDidUpdate() {
if (process.env.NODE_ENV !== 'production') {
warnIfPropsAreInvalid(this.props, this.state)
}
}
computeNewIndex = (index) => {
const { exclusive } = this.props
const { activeIndex } = this.state
if (exclusive) return index === activeIndex ? -1 : index
// check to see if index is in array, and remove it, if not then add it
return _.includes(activeIndex, index) ? _.without(activeIndex, index) : [...activeIndex, index]
}
handleTitleClick = (e, titleProps) => {
const { index } = titleProps
this.setState({ activeIndex: this.computeNewIndex(index) })
_.invoke(this.props, 'onTitleClick', e, titleProps)
}
isIndexActive = (index) => {
const { exclusive } = this.props
const { activeIndex } = this.state
return exclusive ? activeIndex === index : _.includes(activeIndex, index)
}
render() {
const { className, children, panels } = this.props
const classes = cx('accordion', className)
const rest = getUnhandledProps(AccordionAccordion, this.props)
const ElementType = getElementType(AccordionAccordion, this.props)
return (
<ElementType {...rest} className={classes}>
{childrenUtils.isNil(children)
? _.map(panels, (panel, index) =>
AccordionPanel.create(panel, {
defaultProps: {
active: this.isIndexActive(index),
index,
onTitleClick: this.handleTitleClick,
},
}),
)
: children}
</ElementType>
)
}
}
AccordionAccordion.create = createShorthandFactory(AccordionAccordion, (content) => ({ content }))