umami/components/common/Checkbox.js

40 lines
958 B
JavaScript
Raw Normal View History

2020-08-15 10:17:15 +02:00
import React, { useRef } from 'react';
import PropTypes from 'prop-types';
2020-08-15 10:17:15 +02:00
import Icon from 'components/common/Icon';
import Check from 'assets/check.svg';
import styles from './Checkbox.module.css';
function Checkbox({ name, value, label, onChange }) {
2020-08-15 10:17:15 +02:00
const ref = useRef();
const onClick = () => ref.current.click();
2020-08-15 10:17:15 +02:00
return (
<div className={styles.container}>
<div className={styles.checkbox} onClick={onClick}>
2020-08-15 10:17:15 +02:00
{value && <Icon icon={<Check />} size="small" />}
</div>
<label className={styles.label} htmlFor={name} onClick={onClick}>
2020-08-15 10:17:15 +02:00
{label}
</label>
<input
ref={ref}
className={styles.input}
type="checkbox"
name={name}
defaultChecked={value}
2020-08-15 10:17:15 +02:00
onChange={onChange}
/>
</div>
);
}
Checkbox.propTypes = {
name: PropTypes.string,
value: PropTypes.any,
label: PropTypes.node,
onChange: PropTypes.func,
};
export default Checkbox;