-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
65421b2
commit 886e97f
Showing
15 changed files
with
443 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default as useSwitchPropsResolver } from './useSwitchPropsResolver'; | ||
export { default as useSwitchAnimation } from './useSwitchAnimation'; |
28 changes: 28 additions & 0 deletions
28
src/components/main/Switch/hooks/useSwitchAccessibilityProps.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { useMemo } from 'react'; | ||
import { Platform } from 'react-native'; | ||
|
||
interface IUseSwitchAccessibilityProps { | ||
label?: string, | ||
selected: boolean, | ||
disabled: boolean | ||
} | ||
|
||
const useSwitchAccessibilityProps = ( { | ||
label, | ||
selected, | ||
disabled | ||
}: IUseSwitchAccessibilityProps ) => useMemo( | ||
() => ( { | ||
accessible: true, | ||
accessibilityRole: 'switch', | ||
accessibilityLabel: label, | ||
accessibilityState: { checked: selected, disabled }, | ||
...( Platform.OS === 'web' | ||
? { accessibilityChecked: selected } | ||
: {} ) | ||
|
||
} ), | ||
[ label, selected, disabled ] | ||
); | ||
|
||
export default useSwitchAccessibilityProps; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import React from 'react'; | ||
|
||
import UIKitIcon from '../../../../icons/UIKitIcon'; | ||
import Icon from '../../Icon'; | ||
|
||
const defaultOnIcon = <Icon as={UIKitIcon} name="checkmark-outline" />; | ||
const defaultOffIcon = <Icon as={UIKitIcon} name="close-outline" />; | ||
|
||
interface IUseSwitchIconProps { | ||
on: boolean, | ||
onIcon?: JSX.Element, | ||
offIcon?: JSX.Element | ||
} | ||
|
||
const useSwitchIcon = ( { | ||
on, | ||
onIcon = defaultOnIcon, | ||
offIcon = defaultOffIcon | ||
}: IUseSwitchIconProps ) => ( | ||
on ? onIcon : offIcon | ||
); | ||
|
||
export default useSwitchIcon; |
56 changes: 56 additions & 0 deletions
56
src/components/main/Switch/hooks/useSwitchPropsResolver.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { useComponentPropsResolver } from '../../../../hooks'; | ||
import type { IBoxProps } from '../../Box/types'; | ||
import type { IIconProps } from '../../Icon/types'; | ||
import type { ISwitchProps } from '../types'; | ||
import useSwitchStateProps from '../useSwitchStateProps'; | ||
import useSwitchAccessibilityProps from './useSwitchAccessibilityProps'; | ||
import useSwitchIcon from './useSwitchIcon'; | ||
|
||
interface IUseSwitchPropsResolverProps extends Omit<ISwitchProps, 'initialValue' >{ | ||
selected: boolean; | ||
disabled?: boolean; | ||
} | ||
|
||
interface IUseSwitchPropsResolverReturnType { | ||
icon: JSX.Element, | ||
iconProps?: Omit<IIconProps, 'name'>, | ||
iconContainerProps?: Omit<IBoxProps, 'children'>, | ||
switchContainerProps?: Omit<IBoxProps, 'children'>, | ||
containerProps: Omit<IBoxProps, 'children'> | ||
} | ||
|
||
const useSwitchPropsResolver = ( | ||
props: IUseSwitchPropsResolverProps | ||
): IUseSwitchPropsResolverReturnType => { | ||
const { onIcon, offIcon } = props; | ||
const { state, stateProps } = useSwitchStateProps( props ); | ||
const { | ||
__switchContainer: switchContainerProps, | ||
__iconContainer: iconContainerProps, | ||
__icon: iconProps, | ||
...containerProps | ||
} = useComponentPropsResolver( 'Switch', props, state ); | ||
|
||
const icon = useSwitchIcon( { | ||
on: state.isSelected, | ||
onIcon, | ||
offIcon | ||
} ); | ||
|
||
const accessibilityProps = useSwitchAccessibilityProps( { | ||
disabled: state.isDisabled, | ||
selected: state.isSelected | ||
} ); | ||
|
||
Object.assign( containerProps, stateProps, accessibilityProps ); | ||
|
||
return { | ||
icon, | ||
containerProps, | ||
iconContainerProps, | ||
iconProps, | ||
switchContainerProps | ||
}; | ||
}; | ||
|
||
export default useSwitchPropsResolver; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import React, { | ||
cloneElement, useCallback, useState | ||
} from 'react'; | ||
import Pressable from '../Pressable'; | ||
import { useSwitchPropsResolver, useSwitchAnimation } from './hooks'; | ||
import type { ISwitchProps } from './types'; | ||
import Box from '../Box'; | ||
|
||
const Switch = ( { | ||
initialValue = false, | ||
withIcon = true, | ||
animationDuration, | ||
onChange, | ||
testID, | ||
...props | ||
}: ISwitchProps ) => { | ||
const [ isOn, setIsOn ] = useState( initialValue ); | ||
|
||
const { | ||
icon, iconProps, iconContainerProps, containerProps, switchContainerProps | ||
} = useSwitchPropsResolver( { | ||
selected: isOn, | ||
...props | ||
} ); | ||
|
||
const { position, startAnimation, reverseAnimation } = useSwitchAnimation( { | ||
containerProps, | ||
iconContainerProps: iconContainerProps || {}, | ||
animationDuration, | ||
animatedValue: initialValue ? 1 : 0 | ||
} ); | ||
|
||
const toggle = useCallback( () => { | ||
setIsOn( ( wasOn ) => { | ||
if ( wasOn ) { | ||
reverseAnimation(); | ||
} else { | ||
startAnimation(); | ||
} | ||
onChange?.( !wasOn ); | ||
return !wasOn; | ||
} ); | ||
}, [ startAnimation, reverseAnimation, onChange ] ); | ||
|
||
return ( | ||
<Box {...switchContainerProps}> | ||
<Pressable {...containerProps} onPress={toggle} testID={testID}> | ||
<Box.Animated {...iconContainerProps} style={{ marginLeft: position }}> | ||
{withIcon && cloneElement( icon, { | ||
...iconProps, | ||
testID: testID && `${testID}-icon` | ||
} )} | ||
</Box.Animated> | ||
</Pressable> | ||
</Box> | ||
|
||
); | ||
}; | ||
|
||
export default Switch; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import type { IPressableProps } from '../Pressable/types'; | ||
|
||
export interface ISwitchProps extends Omit<IPressableProps, 'children'>{ | ||
initialValue?: boolean, | ||
disabled?: boolean, | ||
|
||
onIcon?: JSX.Element, | ||
offIcon?: JSX.Element, | ||
|
||
animationDuration?: number, | ||
|
||
withIcon?: boolean, | ||
|
||
onChange?: ( value: boolean ) => void, | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { useMemo } from 'react'; | ||
import type { PressableProps } from 'react-native'; | ||
import { useIsFocused, useIsHovered, useIsPressed } from '../../hooks'; | ||
import type { IHoverableComponent } from '../../../core/components/types'; | ||
|
||
interface IUseSwitchStateProps { | ||
value?: string, | ||
disabled?: boolean, | ||
selected?: boolean, | ||
onPress?: PressableProps[ 'onPress' ], | ||
onPressIn?: PressableProps[ 'onPressIn' ], | ||
onPressOut?: PressableProps[ 'onPressOut' ], | ||
onHoverIn?: IHoverableComponent[ 'onHoverIn' ], | ||
onHoverOut?: IHoverableComponent[ 'onHoverOut' ], | ||
onFocus?: PressableProps[ 'onFocus' ], | ||
onBlur?: PressableProps[ 'onBlur' ] | ||
} | ||
|
||
const useSwitchStateProps = ( { | ||
disabled: isDisabled, | ||
selected: isSelected, | ||
onPress: onPressProp, | ||
...props | ||
}: IUseSwitchStateProps ) => { | ||
const { isPressed, onPressIn, onPressOut } = useIsPressed( props ); | ||
const { isHovered, onHoverIn, onHoverOut } = useIsHovered( props ); | ||
const { isFocused, onFocus, onBlur } = useIsFocused( props ); | ||
|
||
return useMemo( | ||
() => { | ||
const onPress = onPressProp; | ||
|
||
const state = { | ||
isSelected: !!isSelected, | ||
isDisabled: !!isDisabled, | ||
isPressed, | ||
isHovered, | ||
isFocused | ||
}; | ||
|
||
const stateProps = { | ||
selected: !!isSelected, | ||
disabled: !!isDisabled, | ||
onPress, | ||
onPressIn, | ||
onPressOut, | ||
onHoverIn, | ||
onHoverOut, | ||
onFocus, | ||
onBlur | ||
}; | ||
|
||
return { state, stateProps }; | ||
}, | ||
[ | ||
isSelected, isDisabled, isPressed, isHovered, isFocused, | ||
onPressProp, onPressIn, onPressOut, onHoverIn, onHoverOut, onFocus, onBlur | ||
] | ||
); | ||
}; | ||
|
||
export default useSwitchStateProps; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
export default { | ||
defaultProps: { | ||
width: 52, | ||
height: 32, | ||
padding: '0.5', | ||
borderWidth: 'sm', | ||
borderColor: 'neutral.600', | ||
backgroundColor: 'secondary.50', | ||
rounded: '4xl', | ||
justifyContent: 'center', | ||
alignItems: 'flex-start', | ||
__switchContainer: { | ||
width: 60, | ||
height: 40, | ||
rounded: '4xl', | ||
justifyContent: 'center', | ||
alignItems: 'center' | ||
}, | ||
__iconContainer: { | ||
width: 24, | ||
height: 24, | ||
rounded: 'full', | ||
backgroundColor: 'neutral.600', | ||
justifyContent: 'center', | ||
alignItems: 'center' | ||
}, | ||
__icon: { | ||
size: '3xs', | ||
color: 'white' | ||
}, | ||
__selected: { | ||
backgroundColor: 'secondary.900', | ||
borderColor: 'transparent', | ||
__iconContainer: { | ||
backgroundColor: 'white' | ||
}, | ||
__icon: { | ||
color: 'secondary.900' | ||
} | ||
}, | ||
__disabled: { | ||
__icon: { | ||
color: 'white' | ||
}, | ||
backgroundColor: 'neutral.200', | ||
borderColor: 'neutral.600', | ||
opacity: 0.5, | ||
__iconContainer: { | ||
backgroundColor: 'neutral.600' | ||
} | ||
}, | ||
__hovered: { | ||
__switchContainer: { | ||
backgroundColor: 'secondary.50' | ||
} | ||
}, | ||
__pressed: { | ||
__switchContainer: { | ||
backgroundColor: 'secondary.50' | ||
} | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ export { | |
Radio, | ||
FormControl, | ||
TextInput, | ||
Chip | ||
Chip, | ||
Switch | ||
} from './components'; | ||
export { UIKitIcon }; |
Oops, something went wrong.