Allows individual or group selection of items using the mouse.
There have been significant changes in the 0.2 release. Please read about them here.
npm install react-selectable
import React from 'react';
import { render } from 'react-dom';
import { SelectableGroup, createSelectable } from 'react-selectable';
import SomeComponent from './some-component';
const SelectableComponent = createSelectable(SomeComponent);
class App extends React.Component {
constructor (props) {
super(props);
this.state = {
selectedKeys: []
};
}
render () {
return (
<SelectableGroup onSelection={this.handleSelection}>
{this.props.items.map((item, i) => {
let selected = this.state.selectedKeys.indexOf(item.id) > -1;
return (
<SelectableComponent key={i} selected={selected} selectableKey={item.id}>
{item.title}
</SelectableComponent>
);
})}
</SelectableGroup>
);
},
handleSelection (selectedKeys) {
this.setState({ selectedKeys });
}
}
The <SelectableGroup />
component accepts a few optional props:
onSelection
(Function) Callback fired after user completes selectiontolerance
(Number) The amount of buffer to add around your<SelectableGroup />
container, in pixels.component
(String) The component to render. Defaults todiv
.fixedPosition
(Boolean) Whether the<SelectableGroup />
container is a fixed/absolute position element or the grandchild of one. Note: if you get an error thatValue must be omitted for boolean attributes
when you try<SelectableGroup fixedPosition={true} />
, simply use Javascript's boolean object function:<SelectableGroup fixedPosition={Boolean(true)} />
.selectOnMouseMove
(Boolean) Enable to fire theonSelection
callback while the mouse is moving. Throttled to 50ms for performance in IE/EdgepreventDefault
(Boolean) Allows to enable/disable preventing the default action of the onmousedown event (with e.preventDefault). True by default. Disable if your app needs to capture this event for other functionalities.