/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ import * as React from 'react' import { capitalize } from '../helpers' import { StyledSwitchButtonWrapper, StyledSwitchButtonLabel, StyledSwitchButton } from './style' export interface SwitchButtonTheme { labelColor?: string, userSelect?: string } export interface SwitchButtonProps { checked: boolean, disabled?: boolean, onChange?: (e: any) => void, id: string, readOnly?: boolean, autoFocus?: boolean, leftText?: string, rightText?: string, size?: 'large' | 'medium' | 'small', theme?: SwitchButtonTheme } export interface SwitchButtonState { checked?: boolean } class SwitchButton extends React.PureComponent { constructor (props: SwitchButtonProps) { super(props) this.state = { checked: props.checked } this.handleChange = this.handleChange.bind(this) } componentWillReceiveProps (nextProps: SwitchButtonProps) { if ('checked' in nextProps) { this.setState({ checked: nextProps.checked }) } } handleChange (e: any) { const { props } = this if (props.disabled) { return } if (!('checked' in props)) { this.setState({ checked: e.target.checked }) } // add non-null assertion operator // in case onChange is not defined i.e. used in a prototype props.onChange!({ target: { checked: e.target.checked, id: e.target.id } }) } getLabel (side: 'left' | 'right') { return ( { side === 'left' ? this.props.leftText : this.props.rightText } ) } render () { const { id, readOnly, disabled, autoFocus, size = 'medium', rightText, leftText } = this.props const { checked } = this.state return ( { leftText && this.getLabel('left') } { rightText && this.getLabel('right') } ) } } export default SwitchButton