import * as React from 'react'; import {isEmpty} from 'lodash'; import classNames from 'classnames'; import {getId} from '../function'; import RenderHtml from '../renderFunction'; import PropsTypes from './types'; import FormComponent from '../FormComponent'; interface State extends PropsTypes { uuid: string, doValidate: boolean } export default class Button extends FormComponent { public state: State; private input = React.createRef(); constructor(props: PropsTypes, {}) { super(props); const uuid = getId(props.id, this.defaultId); this.state = { uuid: uuid, checked: props.checked, doValidate: !props.disabled || false, type: props.type ? props.type : 'checkbox' }; } componentDidMount() { this.validate(this.state.checked ? 'true' : '') } componentWillReceiveProps(nextProps: PropsTypes) { if (nextProps.checked !== this.props.checked) { this.setState({ checked: nextProps.checked }) } } handleCheck = () => { if (this.props.disabled) return; this.setState({ checked: !this.state.checked, }, () => { this.validate(this.state.checked ? 'true' : ''); this.props.onChange && this.props.onChange(this.state.checked) }) }; public render(): JSX.Element { const {checked, uuid, type} = this.state; const {size, customStyle, color, disabled, position, caption} = this.props; const uiClass = classNames(`jump-ui-container jump-ui-${type} ${size || 'medium'}-ui ${customStyle} check-${color || 'default'}`, { 'disabled': disabled || this.readOnly(), 'color': color, 'error': !isEmpty(this.error()), 'checkmark-right': position === 'right' }), checkmarkError = classNames('checkmark', { 'error-checkmark': this.error() && !isEmpty(this.error()) }); return (
{RenderHtml.renderError(this.error())}
); } }