Skip to content

Commit

Permalink
refactor(expandcollapse): Add ability to disable panels
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanoglesby08 committed Nov 6, 2017
1 parent 5b2c369 commit 9e16914
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/components/ExpandCollapse/ExpandCollapse.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { isEqual } from '../../utils/sets'
import PanelWrapper from './PanelWrapper/PanelWrapper'
import Panel from './Panel/Panel'

import styles from './ExpandCollapse.modules.scss'

class ExpandCollapse extends React.Component {
constructor(props) {
super(props)
Expand Down Expand Up @@ -53,16 +55,17 @@ class ExpandCollapse extends React.Component {
const { children, ...rest } = this.props

return (
<div {...safeRest(rest)}>
<div {...safeRest(rest)} className={styles.base}>
{React.Children.map(children, (panel, index) => {
const { id, header, subtext, onToggle } = panel.props
const { id, header, subtext, disabled, onToggle } = panel.props

return (
<PanelWrapper
panelId={id}
panelHeader={header}
panelSubtext={subtext}
panelOnToggle={onToggle}
panelDisabled={disabled}
open={this.state.panels.has(id)}
last={index === React.Children.count(children) - 1}
onClick={() => this.togglePanel(id)}
Expand Down
13 changes: 13 additions & 0 deletions src/components/ExpandCollapse/ExpandCollapse.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,16 @@ const trigger = () => {
</ExpandCollapse.Panel>
</ExpandCollapse>
```

### Disabled

```
<ExpandCollapse>
<ExpandCollapse.Panel id="panel-30" header="First panel title" disabled>
First panel
</ExpandCollapse.Panel>
<ExpandCollapse.Panel id="panel-31" header="Second panel title" subtext="Some subtext">
Second panel
</ExpandCollapse.Panel>
</ExpandCollapse>
```
5 changes: 5 additions & 0 deletions src/components/ExpandCollapse/ExpandCollapse.modules.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import '../../scss/settings/colours';

.base {
background-color: $color-white;
}
4 changes: 3 additions & 1 deletion src/components/ExpandCollapse/Panel/Panel.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ Panel.propTypes = {
id: PropTypes.string.isRequired,
header: PropTypes.oneOfType([PropTypes.string, PropTypes.node]).isRequired,
subtext: PropTypes.string,
disabled: PropTypes.bool,
onToggle: PropTypes.func,
children: PropTypes.node.isRequired,
}

Panel.defaultProps = {
subtext: undefined,
onToggle: undefined
disabled: false,
onToggle: undefined,
}

export default Panel
18 changes: 15 additions & 3 deletions src/components/ExpandCollapse/PanelWrapper/PanelWrapper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import Reveal from '../../Animation/Reveal'
import Translate from '../../Animation/Translate'
import Panel from '../Panel/Panel'

import joinClassNames from '../../../utils/joinClassNames'

import styles from './PanelWrapper.modules.scss'

class PanelWrapper extends React.Component {
Expand Down Expand Up @@ -80,7 +82,15 @@ class PanelWrapper extends React.Component {
}

render() {
const { panelId, panelHeader, panelSubtext, last, onClick, children } = this.props
const {
panelId,
panelHeader,
panelSubtext,
panelDisabled,
last,
onClick,
children,
} = this.props

return (
<div data-testid={panelId}>
Expand All @@ -90,13 +100,13 @@ class PanelWrapper extends React.Component {
onClick={onClick}
onMouseEnter={this.mouseEnter}
onMouseLeave={this.mouseLeave}
dangerouslyAddClassName={styles.header}
dangerouslyAddClassName={joinClassNames(styles.header, panelDisabled && styles.disabled)}
disabled={panelDisabled}
>
<Box vertical={3}>
{/* padding */}
<Flexbox direction="row">
<Box right={3}>
{' '}
{/* margin */}
<Translate timeout={300} in={this.state.hover} direction="y" length="0.25rem">
{() => <DecorativeIcon symbol="caretDown" variant="primary" />}
Expand Down Expand Up @@ -134,6 +144,7 @@ PanelWrapper.propTypes = {
panelHeader: PropTypes.oneOfType([PropTypes.string, PropTypes.node]).isRequired,
panelSubtext: PropTypes.string,
panelOnToggle: PropTypes.func,
panelDisabled: PropTypes.bool,
open: PropTypes.bool,
last: PropTypes.bool.isRequired,
onClick: PropTypes.func.isRequired,
Expand All @@ -142,6 +153,7 @@ PanelWrapper.propTypes = {

PanelWrapper.defaultProps = {
panelSubtext: undefined,
panelDisabled: false,
panelOnToggle: undefined,
open: false,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
@import '../../../scss/settings/colours';

.header {
width: 100%;
text-align: left;
}

.disabled {
background-color: $color-athens-grey;
cursor: default;
}
24 changes: 24 additions & 0 deletions src/components/ExpandCollapse/__tests__/ExpandCollapse.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { shallow, mount } from 'enzyme'
import Reveal from '../../Animation/Reveal'
import Translate from '../../Animation/Translate'
import Text from '../../Typography/Text/Text'
import DecorativeIcon from '../../Icons/DecorativeIcon/DecorativeIcon'
import ExpandCollapse from '../ExpandCollapse'

describe('ExpandCollapse', () => {
Expand Down Expand Up @@ -178,6 +179,29 @@ describe('ExpandCollapse', () => {
})
})

describe('disabled panels', () => {
it('are rendered in a disabled treatment', () => {
const { findPanelHeader } = doMount(
<ExpandCollapse>{aPanel({ id: 'panel-1', disabled: true })}</ExpandCollapse>
)

const panelHeader = findPanelHeader('panel-1')
expect(panelHeader).toHaveClassName('disabled')
expect(panelHeader).toBeDisabled()
expect(panelHeader.find(DecorativeIcon)).not.toBePresent()
})

it('are not able to be opened', () => {
const { clickPanel, findPanel } = doMount(
<ExpandCollapse>{aPanel({ id: 'panel-1', disabled: true })}</ExpandCollapse>
)

clickPanel('panel-1')

expectPanelToBeClosed(findPanel('panel-1'))
})
})

it('passes additional attributes to the element', () => {
const expandCollapse = doShallow({ id: 'the-id', 'data-some-attr': 'some value' })

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ exports[`ExpandCollapse renders 1`] = `
}
>
<div
className="base"
onToggle={undefined}
open={
Array [
Expand All @@ -22,6 +23,7 @@ exports[`ExpandCollapse renders 1`] = `
last={true}
onClick={[Function]}
open={true}
panelDisabled={false}
panelHeader="Panel title"
panelId="panel-1"
panelOnToggle={undefined}
Expand All @@ -41,12 +43,14 @@ exports[`ExpandCollapse renders 1`] = `
<Clickable
dangerouslyAddClassName="header"
dangerouslyAddStyle={undefined}
disabled={false}
onClick={[Function]}
onMouseEnter={[Function]}
onMouseLeave={[Function]}
>
<button
className="clickable header"
disabled={false}
onClick={[Function]}
onMouseEnter={[Function]}
onMouseLeave={[Function]}
Expand Down Expand Up @@ -243,6 +247,7 @@ exports[`ExpandCollapse renders 1`] = `
className="base baseFont color"
>
<Panel
disabled={false}
header="Panel title"
id="panel-1"
onToggle={undefined}
Expand Down

0 comments on commit 9e16914

Please sign in to comment.