adds error state to checkbox and requires onClick prop

This commit is contained in:
Jarrod Flesch
2020-02-27 12:15:25 -05:00
parent 5804d98b7d
commit cd716f2dff
4 changed files with 25 additions and 4 deletions

View File

@@ -6,18 +6,19 @@ import './index.scss';
const baseClass = 'styled-checkbox';
const StyledCheckbox = ({
onClick, isChecked, label, name, isDisabled,
onClick, isChecked, label, name, isDisabled, hasError,
}) => {
const classes = [
baseClass,
isChecked && `${baseClass}--is-checked`,
isDisabled && `${baseClass}--is-disabled`,
hasError && `${baseClass}--has-error`,
].filter(Boolean).join(' ');
return (
<button
className={classes}
onClick={() => (onClick && !isDisabled) && onClick(!isChecked)}
onClick={() => !isDisabled && onClick(!isChecked)}
type="button"
title={label}
role="checkbox"
@@ -31,18 +32,19 @@ const StyledCheckbox = ({
};
StyledCheckbox.defaultProps = {
onClick: null,
isChecked: false,
label: 'Checkbox',
isDisabled: false,
hasError: false,
};
StyledCheckbox.propTypes = {
onClick: PropTypes.func,
onClick: PropTypes.func.isRequired,
isChecked: PropTypes.bool,
label: PropTypes.string,
name: PropTypes.string.isRequired,
isDisabled: PropTypes.bool,
hasError: PropTypes.bool,
};
export default StyledCheckbox;

View File

@@ -20,4 +20,8 @@
opacity: 1;
}
}
&--has-error {
background-color: lighten($error, 20%);
}
}

View File

@@ -5,6 +5,8 @@ import Label from '../../Label';
import Error from '../../Error';
import StyledCheckbox from './StyledCheckbox';
import './index.scss';
const defaultError = 'Checkbox is required';
const defaultValidate = value => Boolean(value);
@@ -63,6 +65,7 @@ const Checkbox = (props) => {
name={name}
label={label}
isDisabled={formProcessing}
hasError={showError}
/>
</div>
);

View File

@@ -0,0 +1,12 @@
@import '../shared';
.field-type.checkbox {
.tooltip {
right: auto;
span {
left: calc(#{base(1.25)} / 2);
transform: none;
}
}
}