Skip to content

Commit 56dba2c

Browse files
author
Antonio Contreras LEMONCODE
committed
Add checkbox common component
1 parent 75bc71e commit 56dba2c

4 files changed

Lines changed: 63 additions & 0 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import React from 'react';
2+
import { useField } from 'formik';
3+
import Checkbox, { CheckboxProps } from '@material-ui/core/Checkbox';
4+
import FormControlLabel from '@material-ui/core/FormControlLabel';
5+
import FormControl from '@material-ui/core/FormControl';
6+
import FormHelperText from '@material-ui/core/FormHelperText';
7+
import * as classes from './checkbox.styles';
8+
9+
interface Props extends CheckboxProps {
10+
label?: string;
11+
labelPlacement?: 'end' | 'start' | 'top' | 'bottom';
12+
error?: boolean;
13+
helperText?: string;
14+
}
15+
16+
export const CheckboxComponent: React.FunctionComponent<Props> = props => {
17+
const {
18+
label,
19+
labelPlacement,
20+
error,
21+
name,
22+
checked,
23+
onChange,
24+
onBlur,
25+
...checkboxProps
26+
} = props;
27+
const [field, meta] = Boolean(name) ? useField(name) : [];
28+
const hasError = error || Boolean(meta && meta.touched && meta.error);
29+
const helperText = Boolean(field) ? meta?.error : props.helperText;
30+
31+
return (
32+
<FormControl
33+
className={classes.root}
34+
error={hasError}
35+
fullWidth={true}
36+
margin="normal"
37+
>
38+
<FormControlLabel
39+
control={
40+
<Checkbox
41+
{...checkboxProps}
42+
name={name}
43+
checked={checked || field?.value}
44+
onChange={onChange || field?.onChange}
45+
onBlur={onBlur || field?.onBlur}
46+
/>
47+
}
48+
label={label}
49+
labelPlacement={labelPlacement}
50+
/>
51+
52+
{hasError && <FormHelperText>{helperText}</FormHelperText>}
53+
</FormControl>
54+
);
55+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { css } from 'emotion';
2+
import { theme } from 'core/theme';
3+
4+
export const root = css`
5+
justify-content: flex-end;
6+
`;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './checkbox.component';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './form.component';
22
export * from './text-field.component';
3+
export * from './checkbox';

0 commit comments

Comments
 (0)