Skip to content

Commit 99e18e8

Browse files
authored
make sure that we reset size only when the necessary props change (#293)
1 parent a7aefa5 commit 99e18e8

File tree

1 file changed

+41
-11
lines changed

1 file changed

+41
-11
lines changed

src/SplitPane.js

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,14 @@ class SplitPane extends React.Component {
4848

4949
// order of setting panel sizes.
5050
// 1. size
51-
// 2. defaultSize
52-
// 3. minSize
51+
// 2. getDefaultSize(defaultSize, minsize, maxSize)
5352

54-
const { size, defaultSize, minSize, primary } = props;
53+
const { size, defaultSize, minSize, maxSize, primary } = props;
5554

5655
const initialSize =
5756
size !== undefined
5857
? size
59-
: defaultSize !== undefined
60-
? defaultSize
61-
: minSize;
58+
: getDefaultSize(defaultSize, minSize, maxSize, null);
6259

6360
this.state = {
6461
active: false,
@@ -69,15 +66,19 @@ class SplitPane extends React.Component {
6966
// previous props that we need in static methods
7067
instanceProps: {
7168
primary,
69+
size,
70+
defaultSize,
71+
minSize,
72+
maxSize,
7273
},
7374
};
7475
}
7576

7677
componentDidMount() {
77-
this.setState(SplitPane.setSize(this.props, this.state));
7878
document.addEventListener('mouseup', this.onMouseUp);
7979
document.addEventListener('mousemove', this.onMouseMove);
8080
document.addEventListener('touchmove', this.onTouchMove);
81+
this.setState(SplitPane.setSize(this.props, this.state));
8182
}
8283

8384
static getDerivedStateFromProps(nextProps, prevState) {
@@ -207,9 +208,11 @@ class SplitPane extends React.Component {
207208
}
208209
}
209210

211+
// TODO: find a more elegant way to fix this. memoize calls to setSize?
212+
// we have to check values since gDSFP is called on every render
210213
static setSize(props, state) {
214+
const { instanceProps } = state;
211215
const newState = {};
212-
const isPanel1Primary = props.primary === 'first';
213216

214217
const newSize =
215218
props.size !== undefined
@@ -221,17 +224,44 @@ class SplitPane extends React.Component {
221224
state.draggedSize
222225
);
223226

224-
newState[isPanel1Primary ? 'pane1Size' : 'pane2Size'] = newSize;
227+
const defaultSizeChanged =
228+
props.defaultSize !== instanceProps.defaultSize ||
229+
props.minSize !== instanceProps.minSize ||
230+
props.maxSize !== instanceProps.maxSize;
225231

226-
if (props.size !== undefined && props.size !== state.draggedSize) {
232+
const shouldUpdateSize =
233+
props.size !== undefined
234+
? props.size !== instanceProps.size
235+
: defaultSizeChanged;
236+
237+
if (
238+
props.size !== undefined &&
239+
props.size !== state.draggedSize &&
240+
shouldUpdateSize
241+
) {
227242
newState.draggedSize = newSize;
228243
}
229244

245+
const isPanel1Primary = props.primary === 'first';
246+
247+
if (shouldUpdateSize || props.primary !== state.instanceProps.primary) {
248+
newState[isPanel1Primary ? 'pane1Size' : 'pane2Size'] = newSize;
249+
}
250+
251+
// unset the size on the non primary panel
230252
if (props.primary !== state.instanceProps.primary) {
231253
newState[isPanel1Primary ? 'pane2Size' : 'pane1Size'] = undefined;
232-
newState.instanceProps = { primary: props.primary };
233254
}
234255

256+
// update the values in instanceProps
257+
instanceProps.primary = props.primary;
258+
instanceProps.size = props.size;
259+
instanceProps.defaultSize = props.defaultSize;
260+
instanceProps.minSize = props.minSize;
261+
instanceProps.maxSize = props.maxSize;
262+
263+
newState.instanceProps = instanceProps;
264+
235265
return newState;
236266
}
237267

0 commit comments

Comments
 (0)