Skip to content

Commit 179633c

Browse files
authored
Fix Dynamically Decreasing Number of Handles Results in Incorrect Handles (#694)
* tests(Range): display dynamically decreased number of handles (#690) As expected, this test case currently fails due to the bug described in #690. * style: automatic formatting by Git hook * fix(Range): correctly handle dynamic decrease in number of handles (#690) * tests(Range): increase robustness of test for #690 The test that corresponds to issue #690 now also verifies that `Range` displays handles correctly after increasing their number.
1 parent 38e22f1 commit 179633c

File tree

2 files changed

+387
-88
lines changed

2 files changed

+387
-88
lines changed

src/Range.tsx

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -132,28 +132,36 @@ class Range extends React.Component<RangeProps, RangeState> {
132132
}
133133

134134
static getDerivedStateFromProps(props, state) {
135-
if ('value' in props || 'min' in props || 'max' in props) {
136-
const value = props.value || state.bounds;
137-
const nextBounds = value.map((v, i) =>
135+
if (!('value' in props || 'min' in props || 'max' in props)) return null;
136+
137+
const value = props.value || state.bounds;
138+
let nextBounds = value.map((v, i) =>
139+
trimAlignValue({
140+
value: v,
141+
handle: i,
142+
bounds: state.bounds,
143+
props,
144+
}),
145+
);
146+
147+
if (state.bounds.length === nextBounds.length) {
148+
if (nextBounds.every((v, i) => v === state.bounds[i])) {
149+
return null;
150+
}
151+
} else {
152+
nextBounds = value.map((v, i) =>
138153
trimAlignValue({
139154
value: v,
140155
handle: i,
141-
bounds: state.bounds,
142156
props,
143157
}),
144158
);
145-
if (
146-
nextBounds.length === state.bounds.length &&
147-
nextBounds.every((v, i) => v === state.bounds[i])
148-
) {
149-
return null;
150-
}
151-
return {
152-
...state,
153-
bounds: nextBounds,
154-
};
155159
}
156-
return null;
160+
161+
return {
162+
...state,
163+
bounds: nextBounds,
164+
};
157165
}
158166

159167
componentDidUpdate(prevProps, prevState) {

0 commit comments

Comments
 (0)