Skip to content

Commit fd6a062

Browse files
authored
Feat/slider orientation support (#1273)
* Support Slider react to orientation change * showcase in demo-screen supporting orientation * misplaced space * removed unused argument * using initial_value since the component is uncontrolled * Fix orientation change Slider calculation
1 parent 3c49ce4 commit fd6a062

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

demo/src/screens/componentScreens/CarouselScreen.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ interface State {
4545
autoplay: boolean;
4646
}
4747

48-
class CarouselScreen extends Component<Props ,State> {
48+
class CarouselScreen extends Component<Props, State> {
4949
carousel = React.createRef<typeof Carousel>();
5050

5151
constructor(props: Props) {

demo/src/screens/componentScreens/SliderScreen.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export default class SliderScreen extends Component {
4141
<Image assetName={'megaphone'} style={styles.image}/>
4242
<Slider
4343
onValueChange={this.onSliderValueChange}
44-
value={INITIAL_VALUE}
44+
value={INITIAL_VALUE}
4545
minimumValue={0}
4646
maximumValue={100}
4747
step={1}

src/components/slider/index.js

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,9 @@ export default class Slider extends PureBaseComponent {
130130
};
131131

132132
this.initialValue = this.getRoundedValue(props.value);
133-
this.initialThumbSize = THUMB_SIZE;
133+
this.lastValue = this.initialValue;
134134

135+
this.initialThumbSize = THUMB_SIZE;
135136
this.checkProps(props);
136137

137138
this.createPanResponderConfig();
@@ -174,6 +175,15 @@ export default class Slider extends PureBaseComponent {
174175
}
175176
}
176177

178+
componentDidMount() {
179+
Constants.addDimensionsEventListener(this.onOrientationChanged);
180+
}
181+
182+
componentWillUnmount() {
183+
Constants.removeDimensionsEventListener(this.onOrientationChanged);
184+
}
185+
186+
177187
/* Gesture Recognizer */
178188

179189
handleMoveShouldSetPanResponder = () => {
@@ -291,9 +301,9 @@ export default class Slider extends PureBaseComponent {
291301
}
292302

293303
getXForValue(v) {
294-
const {minimumValue, maximumValue} = this.props;
304+
const {minimumValue} = this.props;
295305
const range = this.getRange();
296-
const relativeValue = maximumValue > 0 ? minimumValue - v : maximumValue - v; // for negatives in min value
306+
const relativeValue = minimumValue - v;
297307
const value = minimumValue < 0 ? Math.abs(relativeValue) : v - minimumValue; // for negatives
298308
const ratio = value / range;
299309
const x = ratio * (this.state.trackSize.width - this.initialThumbSize.width / 2);
@@ -348,18 +358,24 @@ export default class Slider extends PureBaseComponent {
348358

349359
updateTrackStepAndStyle = ({nativeEvent}) => {
350360
this._x = nativeEvent.locationX;
351-
this.updateValue(nativeEvent.locationX);
352-
361+
this.updateValue(this._x);
362+
353363
if (this.props.step > 0) {
354364
this.bounceToStep();
355365
} else {
356-
this.updateStyles(nativeEvent.locationX);
366+
this.updateStyles(this._x);
357367
}
358368
}
359369

370+
onOrientationChanged = () => {
371+
this.initialValue = this.lastValue;
372+
this.setState({measureCompleted: false});
373+
};
374+
360375
/* Events */
361376

362377
onValueChange = value => {
378+
this.lastValue = value;
363379
_.invoke(this.props, 'onValueChange', value);
364380
};
365381

@@ -376,6 +392,7 @@ export default class Slider extends PureBaseComponent {
376392
};
377393

378394
onTrackLayout = ({nativeEvent}) => {
395+
this.setState({measureCompleted: false});
379396
this.handleMeasure('trackSize', nativeEvent);
380397
};
381398

@@ -401,15 +418,15 @@ export default class Slider extends PureBaseComponent {
401418
if (currentSize && width === currentSize.width && height === currentSize.height) {
402419
return;
403420
}
404-
405421
this[layoutName] = size;
406-
407422
if (this.containerSize && this.thumbSize && this.trackSize) {
423+
// console.warn('post return');
408424
this.setState({
409425
containerSize: this.containerSize,
410426
trackSize: this.trackSize,
411-
thumbSize: this.thumbSize,
412-
measureCompleted: true
427+
thumbSize: this.thumbSize
428+
}, () => {
429+
this.setState({measureCompleted: true});
413430
});
414431
}
415432
};

0 commit comments

Comments
 (0)