import React, { useState } from 'react'; import { StyleSheet, ViewStyle, Switch as RNSwitch } from 'react-native'; import styled from 'styled-components/native'; import { isNil } from 'lodash'; import { useToken, usePropsConfig } from '../../../theme'; import { border, color, flexbox, layout, space } from 'styled-system'; import { customBorder, customBackground, customOutline, customLayout, customExtra, customShadow, } from '../../../utils/customProps'; import type { ISwitchProps } from './props'; const StyledNBSwitch = styled(RNSwitch)( color, space, layout, flexbox, border, customBorder, customBackground, customOutline, customShadow, customExtra, customLayout ); const Switch = ( { style, onToggle, isDisabled, isInvalid, iosBgColor, isChecked, defaultIsChecked, ariaLabel, onColor, offColor, ...props }: ISwitchProps, ref: any ) => { const [isActive, setIsActive] = useState( !isNil(defaultIsChecked) ? defaultIsChecked : false ); const borderColorInvalid = useToken('colors', 'danger.2'); const checked = !isNil(isChecked) ? isChecked : isActive; const newProps = usePropsConfig('Switch', { ...props, checked, onColor, offColor, }); const inValidPropFactors = { borderWidth: 1, borderRadius: 16, borderColor: borderColorInvalid, }; let computedStyle: ViewStyle = StyleSheet.flatten([ style, isInvalid ? inValidPropFactors : {}, ]); return ( setIsActive(!isActive)} value={checked} style={computedStyle} accessibilityLabel={ariaLabel} accessibilityRole="switch" ref={ref} /> ); }; export default React.forwardRef(Switch); export { ISwitchProps } from './props';