Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/components/multi_select_state.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const withMultiSelectState = WrappedComponent =>
this.handleChange = this.handleChange.bind(this);
this.getList = this.getList.bind(this);
this.onKeyUp = this.onKeyUp.bind(this);
this.handleCopy = this.handleCopy.bind(this);

const { items, selectedItems } = props;
this.state = {
Expand Down Expand Up @@ -86,18 +87,26 @@ const withMultiSelectState = WrappedComponent =>

componentDidMount() {
window.addEventListener("keyup", this.onKeyUp);
window.addEventListener("copy", this.handleCopy);
}

componentWillUnmount() {
window.removeEventListener("keyup", this.onKeyUp, false);
window.removeEventListener("copy", this.handleCopy);
}

onKeyUp(event) {
if (event.keyCode === 16) {
this.setState({ firstItemShiftSelected: undefined });
}
}
handleCopy(event) {
const { selectedItems } = this.state;
const result = selectedItems.map(item => item.label).join("\n");

event.preventDefault();
event.clipboardData.setData("text/plain", result);
}
selectItem(event, id) {
const { items } = this.props;
const { selectedItems, firstItemShiftSelected } = this.state;
Expand Down Expand Up @@ -193,7 +202,6 @@ const withMultiSelectState = WrappedComponent =>
getList(ref) {
this.list = ref;
}

render() {
return (
<WrappedComponent
Expand Down
15 changes: 15 additions & 0 deletions tests/components/multi_select_state.spec.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -435,4 +435,19 @@ describe("withMultiSelectState", () => {
wrapper.update();
expect(wrapper.state("filteredItems")).toEqual([ITEM_1]);
});

test("simulate copy", () => {
const setData = jest.fn();
const event = {
preventDefault: jest.fn(),
clipboardData: { setData }
};
const ConditionalComponent = withMultiSelectState(CustomComponent);
const wrapper = shallow(<ConditionalComponent items={items} />);
wrapper.props().selectItem(EVENT_WITH_SHIFT, ITEM_1.id);
wrapper.props().selectItem(EVENT_WITH_SHIFT, ITEM_2.id);
wrapper.update();
wrapper.instance().handleCopy(event);
expect(setData).toBeCalledWith("text/plain", "item 0\nitem 1");
});
});