@@ -19,7 +19,8 @@ type DragCallbackData = {
1919} ;
2020export type ResizeCallbackData = {
2121 node : HTMLElement ,
22- size : { width : number , height : number }
22+ size : { width : number , height : number } ,
23+ axis : Axis ,
2324} ;
2425export type Props = {
2526 children : ReactElement < any > ,
@@ -157,12 +158,13 @@ export default class Resizable extends React.Component<Props, State> {
157158 * @param {String } handlerName Handler name to wrap.
158159 * @return {Function } Handler function.
159160 */
160- resizeHandler ( handlerName : string ) : Function {
161+ resizeHandler ( handlerName : string , resizeAxis : Axis ) : Function {
161162 return ( e : SyntheticEvent < > | MouseEvent , { node, deltaX, deltaY} : DragCallbackData ) => {
162163
163164 // 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' ;
166168
167169 // Update w/h
168170 let width = this . state . width + ( canDragX ? deltaX : 0 ) ;
@@ -191,13 +193,27 @@ export default class Resizable extends React.Component<Props, State> {
191193 const hasCb = typeof this . props [ handlerName ] === 'function' ;
192194 if ( hasCb ) {
193195 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 } ) ) ;
195197 } else {
196198 this . setState ( newState ) ;
197199 }
198200 } ;
199201 }
200202
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+
201217 render ( ) : ReactNode {
202218 // eslint-disable-next-line no-unused-vars
203219 const { children, draggableOpts, width, height, handleSize,
@@ -208,6 +224,11 @@ export default class Resizable extends React.Component<Props, State> {
208224 `${ p . className } react-resizable` :
209225 'react-resizable' ;
210226
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+ ] ;
211232 // What we're doing here is getting the child of this element, and cloning it with this element's props.
212233 // We are then defining its children as:
213234 // Its original children (resizable's child's children), and
@@ -217,15 +238,7 @@ export default class Resizable extends React.Component<Props, State> {
217238 className,
218239 children : [
219240 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 ,
229242 ]
230243 } ) ;
231244 }
0 commit comments