Skip to content

Commit

Permalink
feat(Sidebar): add component
Browse files Browse the repository at this point in the history
  • Loading branch information
fracmak committed Nov 20, 2016
1 parent 28c7122 commit 0d5b103
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ Any other issue labeled [`help wanted`][4] is ready for a PR.
| ✓ List | | | ✓ Rating | |
| ✓ Loader | | | ✓ Search | |
| ✓ Rail | | | Shape | |
| ✓ Reveal | | | Sidebar | |
| ✓ Reveal | | | Sidebar | |
| ✓ Segment | | | Sticky | |
| ✓ Step | | | Tab | |
| | | | Transition | |
Expand Down
84 changes: 84 additions & 0 deletions src/collections/Sidebar/Sidebar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import cx from 'classnames'
import React, { PropTypes } from 'react'

import {
AutoControlledComponent as Component,
META,
customPropTypes,
getUnhandledProps,
getElementType,
useKeyOnly,
} from '../../lib'

const _meta = {
name: 'Sidebar',
type: META.TYPES.MODULE,
props: {
animation: ['overlay', 'push', 'scale down', 'uncover', 'slide out', 'slide along'],
direction: ['top', 'right', 'bottom', 'left'],
width: ['very thin', 'thin', 'wide', 'very wide'],
},
}

/**
* A sidebar hides additional content beside a page.
*/
class Sidebar extends Component {
static _meta = _meta

static propTypes = {
/** An element type to render as (string or function). */
as: customPropTypes.as,

/** Animation style. */
animation: PropTypes.oneOf(Sidebar._meta.props.animation),

/** Primary content. */
children: PropTypes.node,

/** Additional classes. */
className: PropTypes.string,

/** Initial value of visible. */
defaultVisible: PropTypes.bool,

/** Direction the sidebar should appear on */
direction: PropTypes.oneOf(Sidebar._meta.props.direction),

/** Controls whether or not the sidebar is visible on the page. */
visible: PropTypes.bool,

/** Sidebar width */
width: PropTypes.oneOf(Sidebar._meta.props.width),
}

static defaultProps = {
direction: 'left',
}

static autoControlledProps = [
'visible',
]

render() {
const { animation, className, children, direction, visible, width } = this.props

const classes = cx(
'ui',
'sidebar',
'animating', // should be dynamically set only when animating
direction,
width,
animation,
useKeyOnly(visible, 'visible'),
className
)

const rest = getUnhandledProps(Sidebar, this.props)
const ElementType = getElementType(Sidebar, this.props)

return <ElementType {...rest} className={classes}>{children}</ElementType>
}
}

export default Sidebar
1 change: 1 addition & 0 deletions src/collections/Sidebar/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default from './Sidebar'
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export { default as MessageHeader } from './collections/Message/MessageHeader'
export { default as MessageItem } from './collections/Message/MessageItem'
export { default as MessageList } from './collections/Message/MessageList'

export { default as Sidebar } from './collections/Sidebar'

export { default as Table } from './collections/Table'
export { default as TableBody } from './collections/Table/TableBody'
export { default as TableCell } from './collections/Table/TableCell'
Expand Down
29 changes: 29 additions & 0 deletions test/specs/collections/Sidebar/Sidebar-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react'
import _ from 'lodash'

import Sidebar from 'src/collections/Sidebar/Sidebar'
import * as common from 'test/specs/commonTests'

describe('Sidebar', () => {
common.isConformant(Sidebar)
common.rendersChildren(Sidebar)
common.hasUIClassName(Sidebar)

common.propKeyOnlyToClassName(Sidebar, 'visible')
common.propValueOnlyToClassName(Sidebar, 'width')
common.propValueOnlyToClassName(Sidebar, 'animation')

it('renders a <div /> element', () => {
shallow(<Sidebar />)
.should.have.tagName('div')
})
it('renders children of the sidebar', () => {
const wrapper = mount(<Sidebar><span /></Sidebar>)
wrapper.children().at(0).should.have.tagName('span')
})
it('adds direction value to className', () => {
_.each(_.get(Sidebar, '_meta.props.direction'), propValue => {
shallow(<Sidebar direction={propValue} />).should.have.className(propValue)
})
})
})

0 comments on commit 0d5b103

Please sign in to comment.