import Form from 'react-bootstrap/Form';

function FormCheckbox(props) {
  const {
    input,
    required,
    label,
    disabled,
    meta: { touched, error }
  } = props;
  const id = props.id || input.name;
  const checked = typeof input.value === 'boolean' ? input.value : input.value === 'true';

  return (
    <Form.Group controlId={id}>
      <Form.Check type='checkbox'>
        <Form.Check.Input type='checkbox' disabled={disabled} checked={checked} {...input} />
        <Form.Check.Label>
          {label}
          {required && <span className='text-danger'> * </span>}
        </Form.Check.Label>
      </Form.Check>
      {touched && error != null && <Form.Text className='text-danger'>{error}</Form.Text>}
    </Form.Group>
  );
}

export default FormCheckbox;
