Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | 17x 17x 2x 15x 4x 13x 10x 3x 15x 10x 10x 10x 10x 10x 10x 4x 6x 6x 15x 15x 15x 15x 13x 13x 13x 13x 1x 2x 2x 4x 2x 2x 2x 2x 4x 4x 4x 4x 8x 8x 8x 1x 1x 1x 1x 8x 8x 8x 1x | import React, {Children, cloneElement, Component} from 'react'
import PropTypes from 'prop-types'
import classNames from 'classnames'
import {omit} from 'lodash'
// Converts string or array of strings to string-to-bool object mapping
// "one" -> {one: true}
// ["one", "two"] -> {one: true, two: true}
/* eslint complexity: [2, 4] */
function activeToObject(active) {
let obj = {}
if (active === null) {
return obj
}
if (active instanceof Array) {
active.forEach((name) => (obj[name] = true))
} else if (typeof active === 'object') {
obj = active
} else {
obj[active] = true
}
return obj
}
class AccordionItem extends Component {
static propTypes = {
onClick: PropTypes.func,
href: PropTypes.string,
title: PropTypes.string,
active: PropTypes.oneOfType([
PropTypes.number,
PropTypes.array,
PropTypes.object,
]),
renderHiddenPanes: PropTypes.bool,
contentKey: PropTypes.number,
className: PropTypes.string,
children: PropTypes.node,
}
/* eslint complexity: [2, 5] */
render() {
const {
children,
className,
onClick,
href,
title,
active,
renderHiddenPanes,
...props
} = this.props
const liClassName = classNames(className, 'rev-AccordionItem', {
'rev-AccordionItem--selected': active,
})
const aClassName = classNames('rev-AccordionItem-title', {
'rev-AccordionItem-title--selected': active,
})
const divClassName = classNames('rev-AccordionItem-content', {
'rev-AccordionItem-content--selected': active,
})
const liProps = omit(props, ['contentKey', 'mappedActive'])
if (!active && !renderHiddenPanes) {
return (
<li {...liProps} className={liClassName}>
<a className={aClassName} href={href || '#'} onClick={onClick}>
{title}
</a>
</li>
)
}
const div = <div className={divClassName}>{children}</div>
return (
<li {...liProps} className={liClassName}>
<a className={aClassName} href={href || '#'} onClick={onClick}>
{title}
</a>
{div}
</li>
)
}
}
export default class Accordion extends Component {
static propTypes = {
active: PropTypes.oneOfType([
PropTypes.number,
PropTypes.array,
PropTypes.object,
]),
renderHiddenPanes: PropTypes.bool,
className: PropTypes.string,
children: PropTypes.node,
}
static defaultProps = {
active: null,
}
rewriteChild = (child) => {
const {active, renderHiddenPanes} = this.props
const activeMap = activeToObject(active)
const mappedActive = activeMap[child.props.contentKey] || false
return cloneElement(child, {active: mappedActive, renderHiddenPanes})
}
render() {
const {children, className, ...props} = this.props
const ulClassName = classNames(className, 'rev-Accordion')
const ulProps = omit(props, ['active', 'defaultActive', 'multi'])
return (
<ul {...ulProps} className={ulClassName}>
{Children.map(children, this.rewriteChild)}
</ul>
)
}
}
Accordion.Item = AccordionItem
class StatefulAccordion extends Component {
static propTypes = {
defaultActive: PropTypes.number,
multi: PropTypes.bool,
children: PropTypes.node,
}
constructor(props) {
super(props)
this.state = {
active: activeToObject(props.defaultActive),
}
}
setExclusivelyActive = (contentKey) => {
if (this.state.active[contentKey]) {
return
}
const active = {}
active[contentKey] = true
this.setState({active})
}
toggleActiveStatus = (contentKey) => {
const currentStatus = this.state.active[contentKey]
const active = {...this.state.active}
active[contentKey] = !currentStatus
this.setState({active})
}
rewriteChild = (child) => {
const {multi} = this.props
const {contentKey, onClick} = child.props
const newOnClick = (e, ...args) => {
e.preventDefault()
this[multi ? 'toggleActiveStatus' : 'setExclusivelyActive'](contentKey)
Eif (onClick) {
return onClick(e, ...args)
}
return null
}
return cloneElement(child, {onClick: newOnClick})
}
render() {
const {children, ...props} = this.props
return (
<Accordion {...props} active={this.state.active}>
{Children.map(children, this.rewriteChild)}
</Accordion>
)
}
}
Accordion.Stateful = StatefulAccordion
|