/** * empty comment line Ivan Marshalkin * * @author: Ivan Marshalkin * @date: 2019-04-19 */ import * as React from 'react'; import {Field, IFieldProps} from './Field'; import * as styles from './field.m.css'; import {Checkbox, ICheckboxProps, joinClassNames} from '../..'; import {FieldLabel} from './FieldLabel'; export type IFieldCheckboxProps = IFieldProps & { name?: string; defaultChecked?: boolean; disabled?: boolean; readOnly?: boolean; onChange: (name: string, value: boolean) => void; inputLabel?: string; items?: any[]; checkboxPosition?: 'left' | 'right'; } const defaultProps = { disabled: false as boolean, checkboxPosition: 'right' }; export class FieldCheckbox extends React.PureComponent { static defaultProps = defaultProps; get checkboxChildren () { if (this.props.items) { return (
{this.props.items.map((item) => { return (
{item.inputLabel}
); })}
); } return (
{this.props.inputLabel}
); } inputProps = ({ name, defaultChecked, disabled, readOnly }: IFieldCheckboxProps) => { let props: ICheckboxProps = { name, checked: (defaultChecked === undefined) ? false : defaultChecked, onChange: this.onChange, disabled, readOnly }; return props; } onChange = ({target}: {target: any}) => { this.props.onChange(target.name, target.checked); } override render () { const props = this.props; const {label, sublabel, error, checkboxPosition, isRequired, isWide} = props; if (checkboxPosition === 'right') { return ( {this.checkboxChildren} ); } else { const inputClass = joinClassNames( styles.fieldInputContainer, styles.checkboxInputWidth ); const labelClass = joinClassNames( styles.fieldLabelContainer, styles.checkboxLabelWidth ); const fieldStyles = joinClassNames( styles.field, styles.columnsMode, [styles.wide, isWide === true] ); return (
{this.checkboxChildren}
{error && (
{error}
)}
); } } }