import React, { FunctionComponent, useState, useEffect } from 'react' import bem from '@/utils/bem' export interface SwitchProps { isAsync: boolean checked: boolean disable: boolean activeColor: string inactiveColor: string activeText: string inactiveText: string className: string style: React.CSSProperties change: (val: boolean, event: React.MouseEvent) => void } const defaultProps = { isAsync: false, checked: false, disable: false, activeColor: '', inactiveColor: '', activeText: '', inactiveText: '', className: '', } as SwitchProps export const Switch: FunctionComponent> = (props) => { const { isAsync, checked, disable, activeColor, inactiveColor, activeText, inactiveText, change, className, style, } = { ...defaultProps, ...props, } const [value, setValue] = useState(false) const b = bem('switch') useEffect(() => { setValue(checked) }, [checked]) const classes = () => { return `${b()} ${value ? 'switch-open' : 'switch-close'} ${ disable ? `${b()}-disable` : '' } ${`${b()}-base`} ${className}` } const styles = () => { const myStyle = Object.assign( {}, { backgroundColor: value ? activeColor : inactiveColor }, style || {} ) return myStyle } const onClick = (event: React.MouseEvent) => { if (disable) return if (!isAsync) { setValue(!value) } change && change(!value, event) } return (
onClick(e)} style={styles()}>
{!value &&
} {activeText && ( <> {value ? (
{activeText}
) : (
{inactiveText}
)} )}
) } Switch.defaultProps = defaultProps Switch.displayName = 'NutSwitch'