Skip to content

Commit 7175dd2

Browse files
authored
Refactor/remove draggable state (#112)
* refactor(Resizable): remove draggable state This must have been a vestigal bit of state from before the Resizable/ResizableBox refactor years ago. It is not actually needed and with the React 16.9 refactor to remove CWRP, it became obvious it was actually not useful. Fixes #99 Supersedes #100 Partially fixes #107, #110 * fix(ResizableBox): Fix 16.9 CWRP deprecation
1 parent 6932b5d commit 7175dd2

File tree

2 files changed

+29
-39
lines changed

2 files changed

+29
-39
lines changed

lib/Resizable.js

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ import type {Element as ReactElement, Node as ReactNode} from 'react';
88
type Axis = 'both' | 'x' | 'y' | 'none';
99
type ResizeHandle = 's' | 'w' | 'e' | 'n' | 'sw' | 'nw' | 'se' | 'ne';
1010
type State = {
11-
resizing: boolean,
12-
width: number, height: number,
13-
slackW: number, slackH: number
11+
slackW: number, slackH: number,
1412
};
1513
type DragCallbackData = {
1614
node: HTMLElement,
@@ -109,22 +107,9 @@ export default class Resizable extends React.Component<Props, State> {
109107
};
110108

111109
state: State = {
112-
resizing: false,
113-
width: this.props.width, height: this.props.height,
114110
slackW: 0, slackH: 0
115111
};
116112

117-
componentWillReceiveProps(nextProps: Object) {
118-
// If parent changes height/width, set that in our state.
119-
if (!this.state.resizing &&
120-
(nextProps.width !== this.props.width || nextProps.height !== this.props.height)) {
121-
this.setState({
122-
width: nextProps.width,
123-
height: nextProps.height
124-
});
125-
}
126-
}
127-
128113
lockAspectRatio(width: number, height: number, aspectRatio: number): [number, number] {
129114
height = width / aspectRatio;
130115
width = height * aspectRatio;
@@ -134,23 +119,23 @@ export default class Resizable extends React.Component<Props, State> {
134119
// If you do this, be careful of constraints
135120
runConstraints(width: number, height: number): [number, number] {
136121
const [min, max] = [this.props.minConstraints, this.props.maxConstraints];
122+
if (!min && !max) return [width, height];
137123

124+
// Fit width & height to aspect ratio
138125
if (this.props.lockAspectRatio) {
139-
if (height === this.state.height) {
140-
const ratio = this.state.width / this.state.height;
126+
if (height === this.props.height) {
127+
const ratio = this.props.width / this.props.height;
141128
height = width / ratio;
142129
width = height * ratio;
143130
} else {
144131
// Take into account vertical resize with N/S handles on locked aspect
145132
// ratio. Calculate the change height-first, instead of width-first
146-
const ratio = this.state.height / this.state.width;
133+
const ratio = this.props.height / this.props.width;
147134
width = height / ratio;
148135
height = width * ratio;
149136
}
150137
}
151138

152-
if (!min && !max) return [width, height];
153-
154139
const [oldW, oldH] = [width, height];
155140

156141
// Add slack to the values used to calculate bound position. This will ensure that if
@@ -201,27 +186,24 @@ export default class Resizable extends React.Component<Props, State> {
201186
}
202187

203188
// Update w/h
204-
let width = this.state.width + (canDragX ? deltaX : 0);
205-
let height = this.state.height + (canDragY ? deltaY : 0);
189+
let width = this.props.width + (canDragX ? deltaX : 0);
190+
let height = this.props.height + (canDragY ? deltaY : 0);
206191

207192
// Early return if no change
208-
const widthChanged = width !== this.state.width, heightChanged = height !== this.state.height;
193+
const widthChanged = width !== this.props.width, heightChanged = height !== this.props.height;
209194
if (handlerName === 'onResize' && !widthChanged && !heightChanged) return;
210195

211196
[width, height] = this.runConstraints(width, height);
212197

213198
// Set the appropriate state for this handler.
214199
const newState = {};
215200
if (handlerName === 'onResizeStart') {
216-
newState.resizing = true;
201+
// nothing
217202
} else if (handlerName === 'onResizeStop') {
218-
newState.resizing = false;
219203
newState.slackW = newState.slackH = 0;
220204
} else {
221205
// Early return if no change after constraints
222-
if (width === this.state.width && height === this.state.height) return;
223-
newState.width = width;
224-
newState.height = height;
206+
if (width === this.props.width && height === this.props.height) return;
225207
}
226208

227209
const hasCb = typeof this.props[handlerName] === 'function';

lib/ResizableBox.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import Resizable from './Resizable';
55
import type {Props as ResizableProps, ResizeCallbackData} from './Resizable';
66
import type {Node as ReactNode} from 'react';
77

8-
type State = {width: number, height: number};
8+
type State = {
9+
width: number, height: number,
10+
propsWidth: number, propsHeight: number,
11+
};
912

1013
// An example use of Resizable.
1114
export default class ResizableBox extends React.Component<ResizableProps, State> {
@@ -21,8 +24,22 @@ export default class ResizableBox extends React.Component<ResizableProps, State>
2124
state: State = {
2225
width: this.props.width,
2326
height: this.props.height,
27+
propsWidth: this.props.width,
28+
propsHeight: this.props.height,
2429
};
2530

31+
static getDerivedStateFromProps(props: ResizableProps, state: State) {
32+
// If parent changes height/width, set that in our state.
33+
if (state.propsWidth !== props.width || state.propsHeight !== props.height) {
34+
return {
35+
width: props.width,
36+
height: props.height,
37+
propsWidth: props.width,
38+
propsHeight: props.height,
39+
};
40+
}
41+
}
42+
2643
onResize = (e: SyntheticEvent<>, data: ResizeCallbackData) => {
2744
const {size} = data;
2845
const {width, height} = size;
@@ -35,15 +52,6 @@ export default class ResizableBox extends React.Component<ResizableProps, State>
3552
}
3653
};
3754

38-
componentWillReceiveProps(nextProps: ResizableProps) {
39-
if (nextProps.width !== this.props.width || nextProps.height !== this.props.height) {
40-
this.setState({
41-
width: nextProps.width,
42-
height: nextProps.height
43-
});
44-
}
45-
}
46-
4755
render(): ReactNode {
4856
// Basic wrapper around a Resizable instance.
4957
// If you use Resizable directly, you are responsible for updating the child component

0 commit comments

Comments
 (0)