Skip to content

Feat/slider orientation support #1273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion demo/src/screens/componentScreens/CarouselScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ interface State {
autoplay: boolean;
}

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

constructor(props: Props) {
Expand Down
2 changes: 1 addition & 1 deletion demo/src/screens/componentScreens/SliderScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default class SliderScreen extends Component {
<Image assetName={'megaphone'} style={styles.image}/>
<Slider
onValueChange={this.onSliderValueChange}
value={INITIAL_VALUE}
value={INITIAL_VALUE}
minimumValue={0}
maximumValue={100}
step={1}
Expand Down
37 changes: 27 additions & 10 deletions src/components/slider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,9 @@ export default class Slider extends PureBaseComponent {
};

this.initialValue = this.getRoundedValue(props.value);
this.initialThumbSize = THUMB_SIZE;
this.lastValue = this.initialValue;

this.initialThumbSize = THUMB_SIZE;
this.checkProps(props);

this.createPanResponderConfig();
Expand Down Expand Up @@ -174,6 +175,15 @@ export default class Slider extends PureBaseComponent {
}
}

componentDidMount() {
Constants.addDimensionsEventListener(this.onOrientationChanged);
}

componentWillUnmount() {
Constants.removeDimensionsEventListener(this.onOrientationChanged);
}


/* Gesture Recognizer */

handleMoveShouldSetPanResponder = () => {
Expand Down Expand Up @@ -291,9 +301,9 @@ export default class Slider extends PureBaseComponent {
}

getXForValue(v) {
const {minimumValue, maximumValue} = this.props;
const {minimumValue} = this.props;
const range = this.getRange();
const relativeValue = maximumValue > 0 ? minimumValue - v : maximumValue - v; // for negatives in min value
const relativeValue = minimumValue - v;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to test this part. In unit tests or e2e?

const value = minimumValue < 0 ? Math.abs(relativeValue) : v - minimumValue; // for negatives
const ratio = value / range;
const x = ratio * (this.state.trackSize.width - this.initialThumbSize.width / 2);
Expand Down Expand Up @@ -348,18 +358,24 @@ export default class Slider extends PureBaseComponent {

updateTrackStepAndStyle = ({nativeEvent}) => {
this._x = nativeEvent.locationX;
this.updateValue(nativeEvent.locationX);
this.updateValue(this._x);

if (this.props.step > 0) {
this.bounceToStep();
} else {
this.updateStyles(nativeEvent.locationX);
this.updateStyles(this._x);
}
}

onOrientationChanged = () => {
this.initialValue = this.lastValue;
this.setState({measureCompleted: false});
};

/* Events */

onValueChange = value => {
this.lastValue = value;
_.invoke(this.props, 'onValueChange', value);
};

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

onTrackLayout = ({nativeEvent}) => {
this.setState({measureCompleted: false});
this.handleMeasure('trackSize', nativeEvent);
};

Expand All @@ -401,15 +418,15 @@ export default class Slider extends PureBaseComponent {
if (currentSize && width === currentSize.width && height === currentSize.height) {
return;
}

this[layoutName] = size;

if (this.containerSize && this.thumbSize && this.trackSize) {
// console.warn('post return');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can delete this console

this.setState({
containerSize: this.containerSize,
trackSize: this.trackSize,
thumbSize: this.thumbSize,
measureCompleted: true
thumbSize: this.thumbSize
}, () => {
this.setState({measureCompleted: true});
});
}
};
Expand Down