-
Notifications
You must be signed in to change notification settings - Fork 26
/
checkbox.js
60 lines (52 loc) · 1.52 KB
/
checkbox.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import React from 'react';
import PropTypes from 'prop-types';
import styled from '@emotion/styled';
import { customProperties as vars } from '@commercetools-uikit/design-system';
import accessibleHiddenInputStyles from '../../internals/accessible-hidden-input.styles';
// accessible input :)
const Input = styled.input`
&:focus + div > svg [id$='borderAndContent'] > [id$='border'] {
stroke: ${vars.borderColorForInputWhenFocused};
}
`;
const Checkbox = props => {
const ref = React.useRef();
React.useEffect(() => {
if (props.isIndeterminate) {
ref.current.indeterminate = true;
}
}, [props.isIndeterminate]);
const { onChange } = props;
const handleChange = React.useCallback(
event => !props.isReadOnly && onChange && onChange(event),
[props.isReadOnly, onChange]
);
return (
<Input
css={accessibleHiddenInputStyles}
id={props.id}
name={props.name}
value={props.value}
disabled={props.isDisabled || props.isReadOnly}
readOnly={props.isReadOnly}
checked={props.isChecked && !props.isIndeterminate}
onChange={handleChange}
ref={ref}
/* ARIA */
aria-readonly={props.isReadOnly}
{...props}
/>
);
};
Checkbox.displayName = 'Checkbox';
Checkbox.propTypes = {
id: PropTypes.string,
name: PropTypes.string,
value: PropTypes.string,
isChecked: PropTypes.bool,
isIndeterminate: PropTypes.bool,
onChange: PropTypes.func.isRequired,
isDisabled: PropTypes.bool,
isReadOnly: PropTypes.bool,
};
export default Checkbox;