From ac711ac740d6da79c185555bec8d915212b97768 Mon Sep 17 00:00:00 2001 From: Alexander Petcoglo Date: Wed, 30 Oct 2019 11:26:55 +0200 Subject: [PATCH] Return endPoint along with selected indices --- README.md | 2 +- src/index.js | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5642f55..14cb46f 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ The should exist before rendering the `Selection` component. #### `onSelectionChange` -Function that will be executed when the selection changes. An array of element indexes will be passed (with the same indexes as the `elements` prop). +Function that will be executed when the selection changes. An array of element indexes will be passed (with the same indexes as the `elements` prop) as the first argument. The second argument will contain the dragging `endPoint` object with `x` and `y` values. This is where you want to update your state, to highlight them as selected for example. diff --git a/src/index.js b/src/index.js index af299b2..e98c491 100644 --- a/src/index.js +++ b/src/index.js @@ -18,7 +18,7 @@ export type Box = { type Props = { disabled?: boolean, target: HTMLElement, - onSelectionChange?: (elements: Array) => void, + onSelectionChange?: (elements: Array, point: Point) => void, onHighlightChange?: (elements: Array) => void, elements: Array, // eslint-disable-next-line react/no-unused-prop-types @@ -189,6 +189,8 @@ export default class Selection extends React.PureComponent { // es window.document.removeEventListener('mouseup', this.onMouseUp); window.document.removeEventListener('touchend', this.onMouseUp); + const endPoint = {...this.state.endPoint}; + this.setState({ mouseDown: false, startPoint: null, @@ -197,7 +199,7 @@ export default class Selection extends React.PureComponent { // es }); if (this.props.onSelectionChange) { - this.props.onSelectionChange(this.selectedChildren); + this.props.onSelectionChange(this.selectedChildren, endPoint); } if (this.props.onHighlightChange) {