Skip to content

Commit 64ccb58

Browse files
committed
Merge pull request #43 from IanVS/add-drag-callbacks
Add drag callbacks
2 parents bc03a73 + bc11f13 commit 64ccb58

File tree

3 files changed

+56
-12
lines changed

3 files changed

+56
-12
lines changed

README.md

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ Calls back with (`id: number or string`, `direction: string`)
152152

153153
#### `onResize`: PropTypes.func
154154

155-
Calls when Pane component resize.
156-
Calls back with (`id: number or string`, `direction: string`, `coputedSize: object`, `clientSize: object`)
155+
Calls when Pane component resizes.
156+
Calls back with (`id: number or string`, `direction: string`, `computedSize: object`, `clientSize: object`)
157157

158158
- id: pane id
159159
- direction: `x` or `y` or `xy`
@@ -166,8 +166,8 @@ For example, when `<Resizable width={100} height={200} style={{ padding: '20px'}
166166

167167
#### `onResizeStop`: PropTypes.func
168168

169-
Calls when Pane component resizeStop.
170-
Calls back with (`id: number or string`, `direction: string`, `coputedSize: object`, `clientSize: object`)
169+
Calls when Pane component resize stops.
170+
Calls back with (`id: number or string`, `direction: string`, `computedSize: object`, `clientSize: object`)
171171

172172
- id: pane id
173173
- direction: `x` or `y` or `xy`
@@ -178,19 +178,35 @@ Calls back with (`id: number or string`, `direction: string`, `coputedSize: obje
178178

179179
For example, when `<Resizable width={100} height={200} style={{ padding: '20px'}} onResize={...} />` mounted and resize 'x', this callback is called with `('x', { width: 100, height: 200 }, { width: 140, height: 240 })`
180180

181+
#### `onDragStart`: PropTypes.function
182+
183+
Calls when a Pane component is clicked (e.g. to sort it). It will not be executed if `isSortable` is `false`.
184+
Calls back with (`id: number or string`)
185+
186+
- id: pane id
187+
188+
#### `onDragEnd`: PropTypes.function
189+
190+
Calls when a Pane component is released (finished sorting). It will not be executed if `isSortable` is `false`.
191+
Calls back with (`id: number or string`)
192+
193+
- id: pane id
194+
181195
## Pane Component
182196

183197
#### `id`: PropTypes.oneOfType([PropTypes.string, PropTypes.number ]).isRequired
184198

185199
The `id` property is used to Pane component id.
186200

187-
#### `width`: PropTypes.number
201+
#### `width`: oneOfType([PropTypes.number, PropTypes.string])
188202

189203
The `width` property is used to set the width of a Pane component.
204+
For example, it can be 300, '300px', or 50%.
190205

191-
#### `height`: PropTypes.number
206+
#### `height`: oneOfType([PropTypes.number, PropTypes.string])
192207

193208
The `height` property is used to set the width of a Pane component.
209+
For example, it can be 300, '300px', or 50%.
194210

195211
#### `minWidth`: PropTypes.number
196212

@@ -208,6 +224,16 @@ The `maxWidth` property is used to set the maximum width of a Pane component.
208224

209225
The `maxHeight` property is used to set the maximum height of a Pane component.
210226

227+
#### `onDragStart`: PropTypes.function
228+
229+
If provided, an `onDragStart` function will be executed when the mouse is pressed
230+
down on the pane to drag it. It will not be executed if `isSortable` is `false`.
231+
232+
#### `onDragEnd`: PropTypes.function
233+
234+
If provided, an `onDragEnd` function will be executed when the mouse is released
235+
after clicking a pane. It will not be executed if `isSortable` is `false`.
236+
211237
#### `className`: PropTypes.string
212238

213239
The `className` property is used to set the css class name of a Pane component.

src/index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ class SortablePane extends Component {
2626
onResizeStart: PropTypes.func,
2727
onResize: PropTypes.func,
2828
onResizeStop: PropTypes.func,
29-
disableEffect: PropTypes.bool,
29+
onDragStart: PropTypes.func,
30+
onDragEnd: PropTypes.func,
3031
onOrderChange: PropTypes.func,
3132
className: PropTypes.string,
33+
disableEffect: PropTypes.bool,
3234
isResizable: PropTypes.shape({
3335
x: React.PropTypes.bool,
3436
y: React.PropTypes.bool,
@@ -46,6 +48,8 @@ class SortablePane extends Component {
4648
onResizeStart: () => null,
4749
onResize: () => null,
4850
onResizeStop: () => null,
51+
onDragStart: () => null,
52+
onDragEnd: () => null,
4953
onOrderChange: () => null,
5054
customStyle: {},
5155
className: '',
@@ -310,6 +314,8 @@ class SortablePane extends Component {
310314
isPressed: true,
311315
lastPressed: pos,
312316
});
317+
this.props.children[pos].props.onDragStart();
318+
this.props.onDragStart(this.props.children[pos].props.id);
313319
}
314320

315321
handleMouseMove({ pageX, pageY }) {
@@ -338,6 +344,9 @@ class SortablePane extends Component {
338344

339345
handleMouseUp() {
340346
this.setState({ isPressed: false, delta: 0 });
347+
this.props.children[this.state.lastPressed].props.onDragEnd();
348+
const lastPressedId = this.props.children[this.state.lastPressed].props.id;
349+
this.props.onDragEnd(lastPressedId);
341350
}
342351

343352
renderPanes() {

src/pane.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,31 @@ import React, { Component, PropTypes } from 'react';
33
export default class Pane extends Component {
44
static propTypes = {
55
id: PropTypes.oneOfType([
6-
React.PropTypes.string,
7-
React.PropTypes.number,
6+
PropTypes.string,
7+
PropTypes.number,
88
]).isRequired,
9-
width: PropTypes.number,
10-
height: PropTypes.number,
9+
width: PropTypes.oneOfType([
10+
PropTypes.number,
11+
PropTypes.string,
12+
]),
13+
height: PropTypes.oneOfType([
14+
PropTypes.number,
15+
PropTypes.string,
16+
]),
1117
minWidth: PropTypes.number,
1218
maxWidth: PropTypes.number,
1319
minHeight: PropTypes.number,
1420
maxHeight: PropTypes.number,
21+
onDragStart: PropTypes.func,
22+
onDragEnd: PropTypes.func,
1523
style: PropTypes.object,
1624
className: PropTypes.string,
1725
children: PropTypes.any,
1826
};
1927

2028
static defaultProps = {
29+
onDragStart: () => null,
30+
onDragEnd: () => null,
2131
style: {},
2232
className: '',
2333
};
@@ -28,4 +38,3 @@ export default class Pane extends Component {
2838
);
2939
}
3040
}
31-

0 commit comments

Comments
 (0)