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

Add 'horizontal' property #86

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
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ declare module 'react-native-switch' {
switchRightPx?: number;
switchWidthMultiplier?: number;
switchBorderRadius?: number;
horizontal?: boolean;
}

export class Switch extends Component<SwitchProps> {}
Expand Down
31 changes: 16 additions & 15 deletions lib/Switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ export class Switch extends Component {
switchLeftPx: PropTypes.number,
switchRightPx: PropTypes.number,
switchWidthMultiplier: PropTypes.number,
switchBorderRadius: PropTypes.number
switchBorderRadius: PropTypes.number,
horizontal: PropTypes.bool
};

static defaultProps = {
Expand Down Expand Up @@ -68,6 +69,7 @@ export class Switch extends Component {
switchRightPx: 2,
switchWidthMultiplier: 2,
switchBorderRadius: null,
horizontal: true,
testID: null
};

Expand All @@ -78,8 +80,8 @@ export class Switch extends Component {
value: props.value,
transformSwitch: new Animated.Value(
props.value
? props.circleSize / props.switchLeftPx
: -props.circleSize / props.switchRightPx
? props.circleSize / props.switchLeftPx * (props.horizontal ? 1 : -1)
: -props.circleSize / props.switchRightPx * (props.horizontal ? 1 : -1)
),
backgroundColor: new Animated.Value(props.value ? 75 : -75),
circleColor: new Animated.Value(props.value ? 75 : -75),
Expand Down Expand Up @@ -129,8 +131,8 @@ export class Switch extends Component {
Animated.parallel([
Animated.spring(this.state.transformSwitch, {
toValue: value
? this.props.circleSize / this.props.switchLeftPx
: -this.props.circleSize / this.props.switchRightPx
? this.props.circleSize / this.props.switchLeftPx * (this.props.horizontal ? 1 : -1)
: -this.props.circleSize / this.props.switchRightPx * (this.props.horizontal ? 1 : -1)
}),
Animated.timing(this.state.backgroundColor, {
toValue: value ? 75 : -75,
Expand Down Expand Up @@ -177,6 +179,7 @@ export class Switch extends Component {
renderInsideCircle,
switchWidthMultiplier,
switchBorderRadius,
horizontal,
value,
...restProps
} = this.props;
Expand Down Expand Up @@ -204,8 +207,8 @@ export class Switch extends Component {
containerStyle,
{
backgroundColor: interpolatedColorAnimation,
width: circleSize * switchWidthMultiplier,
height: barHeight || circleSize,
width: horizontal ? circleSize * switchWidthMultiplier : barHeight || circleSize,
height: horizontal ? barHeight || circleSize : circleSize * switchWidthMultiplier,
borderRadius: switchBorderRadius || circleSize
}
]}
Expand All @@ -214,8 +217,9 @@ export class Switch extends Component {
style={[
styles.animatedContainer,
{
left: transformSwitch,
width: circleSize * switchWidthMultiplier
top: horizontal ? 0 : transformSwitch,
left: horizontal ? transformSwitch : 0,
width: horizontal ? circleSize * switchWidthMultiplier : barHeight || circleSize,
},
outerCircleStyle
]}
Expand Down Expand Up @@ -258,15 +262,12 @@ export class Switch extends Component {

const styles = StyleSheet.create({
container: {
width: 71,
height: 30,
borderRadius: 30,
backgroundColor: "black"
backgroundColor: "black",
justifyContent: "center",
alignItems: "center"
},
animatedContainer: {
flex: 1,
width: 78,
flexDirection: "row",
justifyContent: "center",
alignItems: "center"
},
Expand Down