UNPKG

@fitch-digital/fitch-ui

Version:

UI Library for Fitch Digital Projects

80 lines (69 loc) 2.15 kB
import React, { PureComponent } from 'react'; import PropTypes from 'prop-types'; import classNames from 'classnames'; import IconCheckmark from '../../svg/icon-checkmark'; import { excludeFactoryProps } from '../../utils'; import './style.scss'; export class Checkbox extends PureComponent { static propTypes = { checked: PropTypes.bool, disabled: PropTypes.bool, focus: PropTypes.bool, label: PropTypes.string, onChange: PropTypes.func, error: PropTypes.string, } state = { checked: !!this.props.checked, } handleChange = (e) => { this.setState(prevState => ({ checked: !prevState.checked, })); if (this.props.onChange) { this.props.onChange(e); } } render() { const classes = classNames({ checkbox: true, focus: !!this.props.focus, disabled: !!this.props.disabled, checked: this.state.checked, }); const props = excludeFactoryProps([ 'checked', 'disabled', 'focus', 'label', 'onChange', 'type', ], this.props); return ( <div className={classes}> <label> <div className="element"> <input type="checkbox" checked={this.state.checked} onChange={this.handleChange} disabled={!!this.props.disabled} {...props} /> <IconCheckmark /> </div> { this.props.label && <div className="label"> {this.props.label} </div> } </label> { this.props.error && <div className="error"> {this.props.error} </div> } </div> ); } }