Skip to content

Commit 2a2e587

Browse files
committed
Added examples to TestLayout.
1 parent 843f699 commit 2a2e587

File tree

5 files changed

+109
-28
lines changed

5 files changed

+109
-28
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[View the Demo](https://strml.github.io/react-resizable/examples/1.html)
44

5-
A simple widget that can be resized via a handle.
5+
A simple widget that can be resized via one or more handles.
66

77
You can either use the `<Resizable>` element directly, or use the much simpler `<ResizableBox>` element.
88

@@ -59,6 +59,7 @@ These props apply to both `<Resizable>` and `<ResizableBox>`.
5959
onResizeStop?: ?(e: SyntheticEvent, data: ResizeCallbackData) => any,
6060
onResizeStart?: ?(e: SyntheticEvent, data: ResizeCallbackData) => any,
6161
onResize?: ?(e: SyntheticEvent, data: ResizeCallbackData) => any,
62-
draggableOpts?: ?Object
62+
draggableOpts?: ?Object,
63+
resizeHandles?: ?Array<'s' | 'w' | 'e' | 'n' | 'sw' | 'nw' | 'se' | 'ne'> = ['se']
6364
};
6465
```

lib/Resizable.js

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ type DragCallbackData = {
2020
};
2121
export type ResizeCallbackData = {
2222
node: HTMLElement,
23-
size: {width: number, height: number}
23+
size: {width: number, height: number},
24+
handle: ResizeHandle
2425
};
2526
export type Props = {
2627
children: ReactElement<any>,
2728
className?: ?string,
2829
width: number,
2930
height: number,
30-
handle: ReactElement<any>,
31+
handle: ReactElement<any> | (resizeHandle: ResizeHandle) => ReactElement<any>,
3132
handleSize: [number, number],
3233
lockAspectRatio: boolean,
3334
axis: Axis,
@@ -183,12 +184,13 @@ export default class Resizable extends React.Component<Props, State> {
183184
const canDragX = (this.props.axis === 'both' || this.props.axis === 'x') && ['n', 's'].indexOf(axis) === -1;
184185
const canDragY = (this.props.axis === 'both' || this.props.axis === 'y') && ['e', 'w'].indexOf(axis) === -1;
185186

186-
if (axis[0] === 'n') {
187-
deltaY = -deltaY;
188-
}
189-
if (axis[axis.length - 1] === 'w') {
187+
// reverse delta if using top or left drag handles
188+
if (canDragX && axis[axis.length - 1] === 'w') {
190189
deltaX = -deltaX;
191190
}
191+
if (canDragY && axis[0] === 'n') {
192+
deltaY = -deltaY;
193+
}
192194

193195
// Update w/h
194196
let width = this.state.width + (canDragX ? deltaX : 0);
@@ -217,36 +219,29 @@ export default class Resizable extends React.Component<Props, State> {
217219
const hasCb = typeof this.props[handlerName] === 'function';
218220
if (hasCb) {
219221
if (typeof e.persist === 'function') e.persist();
220-
this.setState(newState, () => this.props[handlerName](e, {node, size: {width, height}}));
222+
this.setState(newState, () => this.props[handlerName](e, {node, size: {width, height}, handle: axis}));
221223
} else {
222224
this.setState(newState);
223225
}
224226
};
225227
}
226228

227-
renderResizeHandles(): ReactNode {
228-
const {draggableOpts, handle, resizeHandles} = this.props;
229+
renderResizeHandle(resizeHandle: ResizeHandle): ReactNode {
230+
const {handle} = this.props;
229231
if (handle) {
232+
if (typeof handle === 'function') {
233+
return handle(resizeHandle);
234+
}
230235
return handle;
231236
}
232-
return resizeHandles.map(h => (
233-
<DraggableCore
234-
{...draggableOpts}
235-
key={`resizableHandle-${h}`}
236-
onStop={this.resizeHandler('onResizeStop', h)}
237-
onStart={this.resizeHandler('onResizeStart', h)}
238-
onDrag={this.resizeHandler('onResize', h)}
239-
>
240-
<span key={h} className={`react-resizable-handle react-resizable-handle-${h}`} />
241-
</DraggableCore>
242-
));
237+
return <span className={`react-resizable-handle react-resizable-handle-${resizeHandle}`} />;
243238
}
244239

245240
render(): ReactNode {
246241
// eslint-disable-next-line no-unused-vars
247-
const {children, width, height, handle, handleSize,
242+
const {children, draggableOpts, width, height, handleSize,
248243
lockAspectRatio, axis, minConstraints, maxConstraints, onResize,
249-
onResizeStop, onResizeStart, ...p} = this.props;
244+
onResizeStop, onResizeStart, resizeHandles, ...p} = this.props;
250245

251246
const className = p.className ?
252247
`${p.className} react-resizable`:
@@ -261,7 +256,17 @@ export default class Resizable extends React.Component<Props, State> {
261256
className,
262257
children: [
263258
children.props.children,
264-
this.renderResizeHandles()
259+
resizeHandles.map(h => (
260+
<DraggableCore
261+
{...draggableOpts}
262+
key={`resizableHandle-${h}`}
263+
onStop={this.resizeHandler('onResizeStop', h)}
264+
onStart={this.resizeHandler('onResizeStart', h)}
265+
onDrag={this.resizeHandler('onResize', h)}
266+
>
267+
{this.renderResizeHandle(h)}
268+
</DraggableCore>
269+
))
265270
]
266271
});
267272
}

lib/ResizableBox.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ export default class ResizableBox extends React.Component<ResizableProps, State>
4848
// Basic wrapper around a Resizable instance.
4949
// If you use Resizable directly, you are responsible for updating the child component
5050
// with a new width and height.
51-
const {handle, handleSize, onResize, onResizeStart, onResizeStop, draggableOpts,
52-
minConstraints, maxConstraints, lockAspectRatio, axis, width, height, ...props} = this.props;
51+
const {handle, handleSize, onResize, onResizeStart, onResizeStop, draggableOpts, minConstraints,
52+
maxConstraints, lockAspectRatio, axis, width, height, resizeHandles, ...props} = this.props;
5353
return (
5454
<Resizable
5555
handle={handle}
@@ -64,6 +64,7 @@ export default class ResizableBox extends React.Component<ResizableProps, State>
6464
maxConstraints={maxConstraints}
6565
lockAspectRatio={lockAspectRatio}
6666
axis={axis}
67+
resizeHandles={resizeHandles}
6768
>
6869
<div style={{width: this.state.width + 'px', height: this.state.height + 'px'}} {...props} />
6970
</Resizable>

test/TestLayout.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22
import Resizable from '../lib/Resizable';
33
import ResizableBox from '../lib/ResizableBox';
44
import 'style-loader!css-loader!../css/styles.css';
5+
import 'style-loader!css-loader!./test.css';
56

67
export default class TestLayout extends React.Component<{}, {width: number, height: number}> {
78
state = {width: 200, height: 200};
@@ -10,7 +11,8 @@ export default class TestLayout extends React.Component<{}, {width: number, heig
1011
this.setState({width: 200, height: 200});
1112
};
1213

13-
onResize = (event, {element, size}) => {
14+
onResize = (event, {element, size, handle}) => {
15+
console.log(handle);
1416
this.setState({width: size.width, height: size.height});
1517
};
1618

@@ -27,6 +29,23 @@ export default class TestLayout extends React.Component<{}, {width: number, heig
2729
<ResizableBox className="box" width={200} height={200}>
2830
<span className="text">{"<ResizableBox>, same as above."}</span>
2931
</ResizableBox>
32+
<ResizableBox
33+
className="custom-box box"
34+
width={200}
35+
height={200}
36+
handle={<span className="custom-handle custom-handle-se" />}
37+
handleSize={[8, 8]}>
38+
<span className="text">{"<ResizableBox> with custom handle in SE corner."}</span>
39+
</ResizableBox>
40+
<ResizableBox
41+
className="custom-box box"
42+
width={200}
43+
height={200}
44+
handle={(h) => <span className={`custom-handle custom-handle-${h}`} />}
45+
handleSize={[8, 8]}
46+
resizeHandles={['sw', 'se', 'nw', 'ne', 'w', 'e', 'n', 's']}>
47+
<span className="text">{"<ResizableBox> with custom handles in all locations."}</span>
48+
</ResizableBox>
3049
<ResizableBox className="box" width={200} height={200} draggableOpts={{grid: [25, 25]}}>
3150
<span className="text">Resizable box that snaps to even intervals of 25px.</span>
3251
</ResizableBox>

test/test.css

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
.custom-box {
2+
overflow: visible;
3+
}
4+
.custom-handle {
5+
position: absolute;
6+
width: 8px;
7+
height: 8px;
8+
background-color: #1153aa;
9+
opacity: 0.75;
10+
border-radius: 4px;
11+
}
12+
.custom-handle-sw {
13+
bottom: -4px;
14+
left: -4px;
15+
cursor: sw-resize;
16+
}
17+
.custom-handle-se {
18+
bottom: -4px;
19+
right: -4px;
20+
cursor: se-resize;
21+
}
22+
.custom-handle-nw {
23+
top: -4px;
24+
left: -4px;
25+
cursor: nw-resize;
26+
}
27+
.custom-handle-ne {
28+
top: -4px;
29+
right: -4px;
30+
cursor: ne-resize;
31+
}
32+
.custom-handle-w,
33+
.custom-handle-e {
34+
top: 50%;
35+
margin-top: -4px;
36+
cursor: ew-resize;
37+
}
38+
.custom-handle-w {
39+
left: -4px;
40+
}
41+
.custom-handle-e {
42+
right: -4px;
43+
}
44+
.custom-handle-n,
45+
.custom-handle-s {
46+
left: 50%;
47+
margin-left: -4px;
48+
cursor: ns-resize;
49+
}
50+
.custom-handle-n {
51+
top: -4px;
52+
}
53+
.custom-handle-s {
54+
bottom: -4px;
55+
}

0 commit comments

Comments
 (0)