Skip to content
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

Update bigSlider.js from example #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
95 changes: 44 additions & 51 deletions BigSlider.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,58 @@
* @flow
*/


import React, { Component } from 'react'
import {
Animated,
PanResponder,
StyleSheet,
Text,
View,
} from 'react-native'

import React, { Component } from "react"
import { Animated, PanResponder, StyleSheet, Text, View } from "react-native"

export default class BigSlider extends Component {
static defaultProps = {
value: 40,
maximumValue: 100,
minimumValue: 0,
onSlidingStart: () => { },
onValueChange: () => { },
onSlidingComplete: () => { },
onSlidingStart: () => {},
onValueChange: () => {},
onSlidingComplete: () => {}
}


constructor (props: Object) {
constructor(props: Object) {
super()
this.state = {
anchorValue: props.value,
value: props.value,
width: 120, // provisional value
height: 360, // provisional value
height: 360 // provisional value
}


this.range = props.maximumValue - props.minimumValue
}

state: {
anchorValue: number,
value: number,
width: number,
height: number,
height: number
}

componentWillMount () {
componentWillMount() {
this.panResponder = PanResponder.create({
onStartShouldSetPanResponder: () => true,
onPanResponderGrant: () => {
this.props.onSlidingStart()
this.setState({ anchorValue: this.state.value })
},
onPanResponderMove: Animated.event([null, {}], { listener: this.handleSlide }),
onPanResponderRelease: () => { this.props.onSlidingComplete() },
onPanResponderMove: Animated.event([null, {}], {
listener: this.handleSlide
}),
onPanResponderRelease: () => {
this.props.onSlidingComplete()
}
})
}

onLayout = ({ nativeEvent }: Object) => {
this.setState({
width: nativeEvent.layout.width,
height: nativeEvent.layout.height,
height: nativeEvent.layout.height
})
}

Expand All @@ -75,80 +69,79 @@ export default class BigSlider extends Component {
nextValue = nextValue >= maximumValue ? maximumValue : nextValue
nextValue = nextValue <= minimumValue ? minimumValue : nextValue


this.props.onValueChange(nextValue)
this.setState({ value: nextValue })
}

panResponder: Object
range: number

render () {
render() {
const value = this.state.value
const unitValue = (value - this.props.minimumValue) / this.range


return (
<View
onLayout={this.onLayout}
style={[styles.container, this.props.style]}
{...this.panResponder.panHandlers}>
{...this.panResponder.panHandlers}
>
<View style={[styles.pendingTrack, { flex: 1 - unitValue }]} />
<View style={[styles.track, { flex: unitValue }, this.props.trackStyle]}>
<View
style={[styles.track, { flex: unitValue }, this.props.trackStyle]}
>
<View style={styles.thumb} />
{this.props.renderLabel
? this.props.renderLabel()
: <View style={styles.trackLabel}>
{this.props.renderLabel ? (
this.props.renderLabel()
) : (
<View style={styles.trackLabel}>
<Text style={styles.trackLabelText}>
{this.props.label || `${formatNumber(this.props.value)}%`}
</Text>
</View>
}
)}
</View>
</View>
)
}
}


const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'rgb(241, 242, 247)',
justifyContent: "center",
alignItems: "center",
backgroundColor: "rgb(241, 242, 247)",
borderRadius: 12,
overflow: 'hidden',
width: 120,
},
pendingTrack: {
overflow: "hidden",
width: 120
},
pendingTrack: {},
track: {
flex: 1,
backgroundColor: 'rgb(1, 160, 188)',
backgroundColor: "rgb(1, 160, 188)",
borderRadius: 12,
alignSelf: 'stretch',
alignSelf: "stretch"
},
trackLabel: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
justifyContent: "center",
alignItems: "center"
},
trackLabelText: {
color: 'white',
fontWeight: '600',
color: "white",
fontWeight: "600"
},
thumb: {
backgroundColor: 'rgba(0,0,0,.5)',
backgroundColor: "rgba(0,0,0,.5)",
borderRadius: 12,
height: 3,
width: 24,
marginTop: 6,
alignSelf: 'center',
},
alignSelf: "center"
}
})


function formatNumber (x) {
return x.toFixed(1).replace(/\.?0*$/, '')
function formatNumber(x) {
return x.toFixed(1).replace(/\.?0*$/, "")
}
112 changes: 62 additions & 50 deletions example/BigSlider.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,8 @@
* @flow
*/

import React, { Component } from 'react';
import {
Animated,
PanResponder,
StyleSheet,
Text,
View,
} from 'react-native';
import React, { Component } from "react"
import { Animated, PanResponder, StyleSheet, Text, View } from "react-native"

export default class BigSlider extends Component {
static defaultProps = {
Expand All @@ -19,46 +13,58 @@ export default class BigSlider extends Component {
minimumValue: 0,
onSlidingStart: () => {},
onValueChange: () => {},
onSlidingComplete: () => {},
onSlidingComplete: () => {}
}

constructor(props) {
constructor(props: Object) {
super()
this.state = {
anchorValue: props.value,
value: props.value,
width: 120, // provisional value
height: 360 // provisional value
}

this.range = props.maximumValue - props.minimumValue
}

componentWillMount () {
state: {
anchorValue: number,
value: number,
width: number,
height: number
}

componentWillMount() {
this.panResponder = PanResponder.create({
onStartShouldSetPanResponder: (e, gestureState) => true,
onPanResponderGrant: (evt, gestureState) => {
onStartShouldSetPanResponder: () => true,
onPanResponderGrant: () => {
this.props.onSlidingStart()
this.setState({ anchorValue: this.state.value })
},
onPanResponderMove: Animated.event([null, {}], { listener: this.handleSlide }),
onPanResponderRelease: (evt, gestureState) => {
onPanResponderMove: Animated.event([null, {}], {
listener: this.handleSlide
}),
onPanResponderRelease: () => {
this.props.onSlidingComplete()
},
}
})
}

slideTo = (value) => {
this.setState({value})
}

onLayout = ({ nativeEvent }) => {
onLayout = ({ nativeEvent }: Object) => {
this.setState({
width: nativeEvent.layout.width,
height: nativeEvent.layout.height,
height: nativeEvent.layout.height
})
}

handleSlide = (evt, gestureState) => {
slideTo = (value: number) => {
this.setState({ value })
}

handleSlide = (evt: Object, gestureState: Object) => {
const { maximumValue, minimumValue } = this.props
let valueIncrement = (-gestureState.dy * this.range) / this.state.height
const valueIncrement = (-gestureState.dy * this.range) / this.state.height
let nextValue = this.state.anchorValue + valueIncrement
nextValue = nextValue >= maximumValue ? maximumValue : nextValue
nextValue = nextValue <= minimumValue ? minimumValue : nextValue
Expand All @@ -67,69 +73,75 @@ export default class BigSlider extends Component {
this.setState({ value: nextValue })
}

render () {
panResponder: Object
range: number

render() {
const value = this.state.value
const unitValue = (value - this.props.minimumValue) / this.range

return (
<View
onLayout={this.onLayout}
style={[styles.container, this.props.style]}
{...this.panResponder.panHandlers}>
{...this.panResponder.panHandlers}
>
<View style={[styles.pendingTrack, { flex: 1 - unitValue }]} />
<View style={[styles.track, { flex: unitValue }, this.props.trackStyle]}>
<View
style={[styles.track, { flex: unitValue }, this.props.trackStyle]}
>
<View style={styles.thumb} />
{this.props.renderLabel
? this.props.renderLabel()
: <View style={styles.trackLabel}>
{this.props.renderLabel ? (
this.props.renderLabel()
) : (
<View style={styles.trackLabel}>
<Text style={styles.trackLabelText}>
{this.props.label || `${formatNumber(this.props.value)}%`}
</Text>
</View>
}
)}
</View>
</View>
);
)
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'rgb(241, 242, 247)',
justifyContent: "center",
alignItems: "center",
backgroundColor: "rgb(241, 242, 247)",
borderRadius: 12,
overflow: 'hidden',
width: 120,
},
pendingTrack: {
overflow: "hidden",
width: 120
},
pendingTrack: {},
track: {
flex: 1,
backgroundColor: 'rgb(1, 160, 188)',
backgroundColor: "rgb(1, 160, 188)",
borderRadius: 12,
alignSelf: 'stretch',
alignSelf: "stretch"
},
trackLabel: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
justifyContent: "center",
alignItems: "center"
},
trackLabelText: {
color: 'white',
fontWeight: '600',
color: "white",
fontWeight: "600"
},
thumb: {
backgroundColor: 'rgba(0,0,0,.5)',
backgroundColor: "rgba(0,0,0,.5)",
borderRadius: 12,
height: 3,
width: 24,
marginTop: 6,
alignSelf: 'center',
},
alignSelf: "center"
}
})

function formatNumber (x) {
return x.toFixed(1).replace(/\.?0*$/, '')
function formatNumber(x) {
return x.toFixed(1).replace(/\.?0*$/, "")
}