import React from 'react'; import { SelectableGroup, createSelectable } from 'react-selectable'; import Album from './Album'; const isNodeInRoot = (node, root) => { while (node) { if (node === root) { return true; } node = node.parentNode; } return false; }; const SelectableAlbum = createSelectable(Album); class App extends React.Component { constructor (props) { super(props); this.state = { selectedItems: [], tolerance: 0, selectOnMouseMove: false, } this.handleSelection = this.handleSelection.bind(this); this.clearItems = this.clearItems.bind(this); this.handleToleranceChange = this.handleToleranceChange.bind(this); this.toggleSelectOnMouseMove = this.toggleSelectOnMouseMove.bind(this); } componentDidMount () { document.addEventListener('click', this.clearItems); } componentWillUnmount () { document.removeEventListener('click', this.clearItems); } handleSelection (keys) { this.setState({ selectedItems: keys }); } clearItems (e) { if(!isNodeInRoot(e.target, this.refs.selectable)) { this.setState({ selectedItems: [] }); } } handleToleranceChange (e) { this.setState({ tolerance: parseInt(e.target.value) }); } toggleSelectOnMouseMove () { this.setState({ selectOnMouseMove: !this.state.selectOnMouseMove }); } render () { return (

React Selectable Demo

Tolerance: {this.state.tolerance}
The number of pixels that must be in the bounding box in order for an item to be selected.

{this.state.selectedItems.length > 0 &&

You have selected the following items:

} {this.state.selectedItems.length === 0 &&

Please select some items from the right by clicking and dragging a box around them.

}
    {this.state.selectedItems.map(function (key,i) { return
  • {this.props.items[key].title}
  • }.bind(this))}
{this.props.items.map((item, i) => { const selected = this.state.selectedItems.indexOf(i) > -1; return ( ); })}
); } } export default App;