Created
July 25, 2016 13:46
-
-
Save danpecher/786387b747461b1a491e8295efb33585 to your computer and use it in GitHub Desktop.
react component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
import prettifySize from './prettify_size' | |
const AttributeFilter = React.createClass({ | |
propTypes: { | |
targetName: React.PropTypes.string.isRequired, | |
items: React.PropTypes.array.isRequired, | |
activeItems: React.PropTypes.arrayOf(React.PropTypes.number) || [] | |
}, | |
getInitialState() { | |
return { | |
checkedItems: this.props.activeItems || [] | |
} | |
}, | |
render() { | |
let items = this.props.items | |
if (this.props.targetName == 'size') { | |
items = items.sort((a, b) => a.name - b.name) | |
} | |
items = items.map((item, i) => { | |
let label = this.props.targetName == 'size' ? prettifySize(item.name) : item.name | |
return <li key={i}> | |
<label className="inp-item"> | |
<input checked={this._isActive(item.id)} | |
onChange={this._toggleItem.bind(null, item.id)} type="checkbox" | |
name="category"/> | |
<span>{label}</span> | |
</label> | |
</li> | |
}) | |
const columns = this.props.columns ? ' columns' : '' | |
return ( | |
<ul className={`reset inp-items${columns}`}> | |
{items} | |
</ul> | |
) | |
}, | |
_toggleItem(id) { | |
let checkedItems = this.state.checkedItems | |
let index | |
if( (index = $.inArray(id, checkedItems)) !== -1 ) { | |
checkedItems.splice(index, 1) | |
} else { | |
checkedItems.push(id) | |
} | |
this.setState({checkedItems}, () => { | |
const e = new Event('filter.toggle') | |
e.itemId = id | |
e.name = this.props.targetName | |
document.dispatchEvent(e) | |
}) | |
}, | |
_isActive(id) { | |
return this.state.checkedItems.indexOf(id) !== -1 | |
} | |
}) | |
export default AttributeFilter |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment