Comments (1)
i have customize this component
`import React, { Component } from 'react';
import {
View,
Text,
TouchableOpacity,
StyleSheet,
Animated
} from 'react-native';
import PropTypes from 'prop-types';
export default class Switch extends Component {
static propTypes = {
onValueChange: PropTypes.func,
activeSwitch: PropTypes.any,
};
static defaultProps = {
onValueChange: () => null
};
constructor(props) {
super(props);
this.state = {
activeSwitch: props.activeSwitch ? props.activeSwitch : 1,
sbWidth: 100,
sbHeight: 44,
direction: 'ltr',
offsetX: props.activeSwitch ? props.activeSwitch === 1 ? new Animated.Value(0) : new Animated.Value(100) : new Animated.Value(0),
};
this._switchDirection = this._switchDirection.bind(this);
}
_switchDirection(direction) {
let dir = 'row';
if (direction === 'rtl') {
dir = 'row-reverse';
}
else {
dir = 'row';
}
return dir;
}
_switchThump(direction) {
const { onValueChange, disabled } = this.props;
this.props.setActiveSwitch(this.props.activeSwitch)
// onSignIn = async () => {console.log("signing in")}
let dirsign = 1;
if (direction === 'rtl') {
dirsign = -1;
}
else {
dirsign = 1;
}
if (this.props.activeSwitch === 1) {
this.setState({ activeSwitch: 2 }, () => onValueChange(this.state.activeSwitch));
Animated.timing(
this.state.offsetX,
{
toValue: (((this.props.switchWidth || this.state.sbWidth) / 2) - 6) * dirsign,
duration: this.props.switchSpeedChange || 100
}
).start();
}
else {
this.setState({ activeSwitch: 1 }, () => onValueChange(this.state.activeSwitch));
Animated.timing(
this.state.offsetX,
{
toValue: 0,
duration: this.props.switchSpeedChange || 100
}
).start();
}
}
render() {
return (
<View>
<TouchableOpacity activeOpacity={1} onPress={() => { this._switchThump(this.props.switchdirection || this.state.direction) }}>
<View
style={[{
width: this.props.switchWidth || this.state.sbWidth,
height: this.props.switchHeight || this.state.sbHeight,
borderRadius: this.props.switchBorderRadius !== undefined ? this.props.switchBorderRadius : this.state.sbHeight / 2,
borderWidth: 1,
borderColor: this.props.switchBorderColor || "#d4d4d4",
backgroundColor: this.props.switchBackgroundColor || "#fff"
}]}
>
<View style={[{ flexDirection: this._switchDirection(this.props.switchdirection || this.state.direction) }]} >
<Animated.View style={{ transform: [{ translateX: this.state.offsetX }] }}>
<View
style={[switchStyles.wayBtnActive,
{
width: this.props.switchWidth / 2 || this.state.sbWidth / 2,
height: this.props.switchHeight - 6 || this.state.sbHeight - 6,
borderRadius: this.props.switchBorderRadius !== undefined ? this.props.switchBorderRadius : this.state.sbHeight / 2,
borderColor: this.props.btnBorderColor || "#00a4b9",
backgroundColor: this.props.btnBackgroundColor || "#00bcd4"
}]}
/>
</Animated.View>
<View style={[switchStyles.textPos,
{
width: this.props.switchWidth / 2 || this.state.sbWidth / 2,
height: this.props.switchHeight - 6 || this.state.sbHeight - 6,
left: 0
}]}
>
<Text style={[this.state.activeSwitch === 1 ? { color: this.props.activeFontColor || "#fff" } : { color: this.props.fontColor || "#b1b1b1" }]}>
{this.props.text1 || 'ON'}
</Text>
</View>
<View
style={[switchStyles.textPos,
{
width: this.props.switchWidth / 2 || this.state.sbWidth / 2,
height: this.props.switchHeight - 6 || this.state.sbHeight - 6,
right: 0
}]}
>
<Text style={[this.state.activeSwitch === 2 ? { color: this.props.activeFontColor || "#fff" } : { color: this.props.fontColor || "#b1b1b1" }]}>
{this.props.text2 || 'OFF'}
</Text>
</View>
</View>
</View>
</TouchableOpacity>
{ this.props.children}
</View>
);
}
}
const switchStyles = StyleSheet.create({
textPos: {
position: 'absolute',
justifyContent: 'center',
alignItems: 'center'
},
rtl: {
flexDirection: 'row-reverse'
},
ltr: {
flexDirection: 'row'
},
wayBtnActive: {
borderWidth: 1,
marginTop: 2,
marginRight: 2,
marginLeft: 2
}
});
`
above the the switch class that is customised...
you can use it like...
<AH_LocalSwitch activeSwitch={this.state.activeSwitchValue} setActiveSwitch={this.btnActionSetActiveSwitch} text1='Value1' text2='Value2' // activeSwitch={1} switchWidth={200} switchHeight={60} switchdirection='ltr' switchBorderRadius={0} switchSpeedChange={500} switchBorderColor={'#F0F0F0'} switchBackgroundColor={'#F0F0F0'} btnBorderColor={theme.appColor.color} btnBackgroundColor={theme.appColor.color} fontColor={theme.gray.color} activeFontColor={'#fff'} switchBorderRadius={5} />
`btnActionSetActiveSwitch = async (data) => {
if (data === 1) {
this.setState({
activeSwitchValue: 2,
})
} else {
this.setState({
activeSwitchValue: 1,
})
}
}`
this.state.activeSwitchValue is the value that can be used to changed the value of switch.
from switch-button-react-native.
Related Issues (3)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from switch-button-react-native.