Skip to content

Commit

Permalink
Merge pull request #23857 from huzaifa-99/16204-migrate-switch-to-fun…
Browse files Browse the repository at this point in the history
…ction-comp

Migrated Switch to function component syntax
  • Loading branch information
deetergp authored Jul 31, 2023
2 parents b041a2c + c85c33f commit 3706f0b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 52 deletions.
84 changes: 32 additions & 52 deletions src/components/Switch.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React, {Component} from 'react';
import {Animated} from 'react-native';
import PropTypes from 'prop-types';
import styles from '../styles/styles';
import * as Pressables from './Pressable';
import React, {useEffect, useRef} from 'react';
import {Animated} from 'react-native';
import CONST from '../CONST';
import styles from '../styles/styles';
import PressableWithFeedback from './Pressable/PressableWithFeedback';

const propTypes = {
/** Whether the switch is toggled to the on position */
Expand All @@ -16,61 +16,41 @@ const propTypes = {
accessibilityLabel: PropTypes.string.isRequired,
};

const PressableWithFeedback = Pressables.PressableWithFeedback;
class Switch extends Component {
constructor(props) {
super(props);
this.offPosition = 0;
this.onPosition = 20;
this.offsetX = new Animated.Value(props.isOn ? this.onPosition : this.offPosition);

this.toggleSwitch = this.toggleSwitch.bind(this);
this.toggleAction = this.toggleAction.bind(this);
}

componentDidUpdate(prevProps) {
if (prevProps.isOn === this.props.isOn) {
return;
}
const OFFSET_X = {
OFF: 0,
ON: 20,
};

this.toggleSwitch();
}
function Switch(props) {
const offsetX = useRef(new Animated.Value(props.isOn ? OFFSET_X.ON : OFFSET_X.OFF));

// animates the switch to the on or off position
toggleSwitch() {
Animated.timing(this.offsetX, {
toValue: this.props.isOn ? this.onPosition : this.offPosition,
useEffect(() => {
Animated.timing(offsetX.current, {
toValue: props.isOn ? OFFSET_X.ON : OFFSET_X.OFF,
duration: 300,
useNativeDriver: true,
}).start();
}

// executes the callback passed in as a prop
toggleAction() {
this.props.onToggle(!this.props.isOn);
}

render() {
const switchTransform = {transform: [{translateX: this.offsetX}]};
return (
<PressableWithFeedback
style={[styles.switchTrack, !this.props.isOn && styles.switchInactive]}
onPress={this.toggleAction}
onLongPress={this.toggleAction}
accessibilityRole={CONST.ACCESSIBILITY_ROLE.SWITCH}
accessibilityState={{checked: this.props.isOn}}
aria-checked={this.props.isOn}
accessibilityLabel={this.props.accessibilityLabel}
// disable hover dim for switch
hoverDimmingValue={1}
pressDimmingValue={0.8}
>
<Animated.View style={[styles.switchThumb, switchTransform]} />
</PressableWithFeedback>
);
}
}, [props.isOn]);

return (
<PressableWithFeedback
style={[styles.switchTrack, !props.isOn && styles.switchInactive]}
onPress={() => props.onToggle(!props.isOn)}
onLongPress={() => props.onToggle(!props.isOn)}
accessibilityRole={CONST.ACCESSIBILITY_ROLE.SWITCH}
accessibilityState={{checked: props.isOn}}
aria-checked={props.isOn}
accessibilityLabel={props.accessibilityLabel}
// disable hover dim for switch
hoverDimmingValue={1}
pressDimmingValue={0.8}
>
<Animated.View style={[styles.switchThumb, styles.switchThumbTransformation(offsetX.current)]} />
</PressableWithFeedback>
);
}

Switch.propTypes = propTypes;
Switch.displayName = 'Switch';

export default Switch;
4 changes: 4 additions & 0 deletions src/styles/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -2504,6 +2504,10 @@ const styles = {
backgroundColor: themeColors.appBG,
},

switchThumbTransformation: (translateX) => ({
transform: [{translateX}],
}),

radioButtonContainer: {
backgroundColor: themeColors.componentBG,
borderRadius: 10,
Expand Down

0 comments on commit 3706f0b

Please sign in to comment.