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

Support disable status for the Switch component #39874

Merged
merged 11 commits into from
Apr 11, 2024
15 changes: 14 additions & 1 deletion src/components/Switch.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import React, {useEffect, useRef} from 'react';
import {Animated} from 'react-native';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import useNativeDriver from '@libs/useNativeDriver';
import CONST from '@src/CONST';
import Icon from './Icon';
import * as Expensicons from './Icon/Expensicons';
import PressableWithFeedback from './Pressable/PressableWithFeedback';

type SwitchProps = {
Expand All @@ -27,6 +30,7 @@ const OFFSET_X = {
function Switch({isOn, onToggle, accessibilityLabel, disabled}: SwitchProps) {
const styles = useThemeStyles();
const offsetX = useRef(new Animated.Value(isOn ? OFFSET_X.ON : OFFSET_X.OFF));
const theme = useTheme();

useEffect(() => {
Animated.timing(offsetX.current, {
Expand All @@ -49,7 +53,16 @@ function Switch({isOn, onToggle, accessibilityLabel, disabled}: SwitchProps) {
hoverDimmingValue={1}
pressDimmingValue={0.8}
>
<Animated.View style={[styles.switchThumb, styles.switchThumbTransformation(offsetX.current)]} />
<Animated.View style={[styles.switchThumb, styles.switchThumbTransformation(offsetX.current)]}>
{disabled && (
<Icon
src={Expensicons.Lock}
fill={isOn ? theme.text : theme.icon}
width={styles.toggleSwitchLockIcon.width}
height={styles.toggleSwitchLockIcon.height}
/>
)}
</Animated.View>
</PressableWithFeedback>
);
}
Expand Down
5 changes: 4 additions & 1 deletion src/pages/workspace/workflows/ToggleSettingsOptionRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ type ToggleSettingOptionRowProps = {
errors?: Errors;
/** Callback to close the error messages */
onCloseError?: () => void;
/** Whether the toggle should be disabled */
disabled?: boolean;
};
const ICON_SIZE = 48;

function ToggleSettingOptionRow({icon, title, subtitle, onToggle, subMenuItems, isActive, pendingAction, errors, onCloseError}: ToggleSettingOptionRowProps) {
function ToggleSettingOptionRow({icon, title, subtitle, onToggle, subMenuItems, isActive, pendingAction, errors, onCloseError, disabled = false}: ToggleSettingOptionRowProps) {
const styles = useThemeStyles();

return (
Expand Down Expand Up @@ -77,6 +79,7 @@ function ToggleSettingOptionRow({icon, title, subtitle, onToggle, subMenuItems,
accessibilityLabel={subtitle}
onToggle={onToggle}
isOn={isActive}
disabled={disabled}
/>
</View>
{isActive && subMenuItems}
Expand Down
9 changes: 8 additions & 1 deletion src/styles/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2861,7 +2861,7 @@ const styles = (theme: ThemeColors) =>
},

switchInactive: {
backgroundColor: theme.border,
backgroundColor: theme.appBG,
Copy link
Contributor

@shubham1206agra shubham1206agra Apr 11, 2024

Choose a reason for hiding this comment

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

Suggested change
backgroundColor: theme.appBG,
backgroundColor: theme.icon,

Small bug. cc @hayata-suenaga

},

switchThumb: {
Expand All @@ -2870,6 +2870,8 @@ const styles = (theme: ThemeColors) =>
borderRadius: 11,
position: 'absolute',
left: 4,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: theme.appBG,
},

Expand All @@ -2889,6 +2891,11 @@ const styles = (theme: ThemeColors) =>
alignItems: 'center',
},

toggleSwitchLockIcon: {
width: variables.iconSizeExtraSmall,
height: variables.iconSizeExtraSmall,
},

checkedContainer: {
backgroundColor: theme.checkBox,
},
Expand Down
Loading