@@ -19,7 +19,8 @@ type DragCallbackData = {
19
19
} ;
20
20
export type ResizeCallbackData = {
21
21
node : HTMLElement ,
22
- size : { width : number , height : number }
22
+ size : { width : number , height : number } ,
23
+ axis : Axis ,
23
24
} ;
24
25
export type Props = {
25
26
children : ReactElement < any > ,
@@ -157,12 +158,13 @@ export default class Resizable extends React.Component<Props, State> {
157
158
* @param {String } handlerName Handler name to wrap.
158
159
* @return {Function } Handler function.
159
160
*/
160
- resizeHandler ( handlerName : string ) : Function {
161
+ resizeHandler ( handlerName : string , resizeAxis : Axis ) : Function {
161
162
return ( e : SyntheticEvent < > | MouseEvent , { node, deltaX, deltaY} : DragCallbackData ) => {
162
163
163
164
// Axis restrictions
164
- const canDragX = this . props . axis === 'both' || this . props . axis === 'x' ;
165
- const canDragY = this . props . axis === 'both' || this . props . axis === 'y' ;
165
+ const axis = resizeAxis || this . props . axis ;
166
+ const canDragX = axis === 'both' || axis === 'x' ;
167
+ const canDragY = axis === 'both' || axis === 'y' ;
166
168
167
169
// Update w/h
168
170
let width = this . state . width + ( canDragX ? deltaX : 0 ) ;
@@ -191,13 +193,27 @@ export default class Resizable extends React.Component<Props, State> {
191
193
const hasCb = typeof this . props [ handlerName ] === 'function' ;
192
194
if ( hasCb ) {
193
195
if ( typeof e . persist === 'function' ) e . persist ( ) ;
194
- this . setState ( newState , ( ) => this . props [ handlerName ] ( e , { node, size : { width, height} } ) ) ;
196
+ this . setState ( newState , ( ) => this . props [ handlerName ] ( e , { node, size : { width, height} , axis } ) ) ;
195
197
} else {
196
198
this . setState ( newState ) ;
197
199
}
198
200
} ;
199
201
}
200
202
203
+
204
+ renderAxis ( axis : Axis ) : ReactNode {
205
+ const { draggableOpts } = this . props ;
206
+ return < DraggableCore
207
+ { ...draggableOpts }
208
+ key = { `resizableHandle-${ axis } ` }
209
+ onStop = { this . resizeHandler ( 'onResizeStop' , axis ) }
210
+ onStart = { this . resizeHandler ( 'onResizeStart' , axis ) }
211
+ onDrag = { this . resizeHandler ( 'onResize' , axis ) }
212
+ >
213
+ < span className = { `react-resizable-handle ${ axis === 'both' ? '' : 'react-resizable-handle-' + axis } ` } />
214
+ </ DraggableCore > ;
215
+ }
216
+
201
217
render ( ) : ReactNode {
202
218
// eslint-disable-next-line no-unused-vars
203
219
const { children, draggableOpts, width, height, handleSize,
@@ -208,6 +224,11 @@ export default class Resizable extends React.Component<Props, State> {
208
224
`${ p . className } react-resizable` :
209
225
'react-resizable' ;
210
226
227
+ const draggableCores = [
228
+ axis === 'both' ? this . renderAxis ( 'both' ) : null ,
229
+ axis === 'both' || axis === 'x' ? this . renderAxis ( 'x' ) : null ,
230
+ axis === 'both' || axis === 'y' ? this . renderAxis ( 'y' ) : null ,
231
+ ] ;
211
232
// What we're doing here is getting the child of this element, and cloning it with this element's props.
212
233
// We are then defining its children as:
213
234
// Its original children (resizable's child's children), and
@@ -217,15 +238,7 @@ export default class Resizable extends React.Component<Props, State> {
217
238
className,
218
239
children : [
219
240
children . props . children ,
220
- < DraggableCore
221
- { ...draggableOpts }
222
- key = "resizableHandle"
223
- onStop = { this . resizeHandler ( 'onResizeStop' ) }
224
- onStart = { this . resizeHandler ( 'onResizeStart' ) }
225
- onDrag = { this . resizeHandler ( 'onResize' ) }
226
- >
227
- < span className = "react-resizable-handle" />
228
- </ DraggableCore >
241
+ ...draggableCores ,
229
242
]
230
243
} ) ;
231
244
}
0 commit comments