2017-06-04 10:40:14 +08:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2017-10-02 22:12:58 +08:00
|
|
|
import cx from 'classnames';
|
2017-01-27 23:41:11 +08:00
|
|
|
import Icon from '../icon/component';
|
|
|
|
import styles from './styles';
|
2017-10-02 22:12:58 +08:00
|
|
|
|
|
|
|
const propTypes = {
|
|
|
|
disabled: PropTypes.bool,
|
|
|
|
checked: PropTypes.bool,
|
|
|
|
onChange: PropTypes.func.isRequired,
|
|
|
|
className: PropTypes.string,
|
|
|
|
ariaLabelledBy: PropTypes.string,
|
|
|
|
ariaLabel: PropTypes.string,
|
|
|
|
ariaDescribedBy: PropTypes.string,
|
|
|
|
ariaDesc: PropTypes.string,
|
|
|
|
};
|
|
|
|
|
|
|
|
const defaultProps = {
|
|
|
|
disabled: false,
|
|
|
|
checked: false,
|
|
|
|
className: null,
|
|
|
|
ariaLabelledBy: null,
|
|
|
|
ariaLabel: null,
|
|
|
|
ariaDescribedBy: null,
|
|
|
|
ariaDesc: null,
|
|
|
|
};
|
2017-01-27 23:41:11 +08:00
|
|
|
|
|
|
|
export default class Checkbox extends Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
2017-02-16 02:49:40 +08:00
|
|
|
this.onChange = props.onChange;
|
|
|
|
this.handleChange = this.handleChange.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleChange() {
|
2017-10-02 22:12:58 +08:00
|
|
|
if (this.props.disabled) return;
|
2017-02-16 02:49:40 +08:00
|
|
|
this.onChange();
|
2017-01-27 23:41:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2017-10-02 22:12:58 +08:00
|
|
|
const {
|
|
|
|
ariaLabel, ariaLabelledBy, ariaDesc, ariaDescribedBy,
|
|
|
|
className, checked, disabled,
|
|
|
|
} = this.props;
|
2017-04-25 10:08:18 +08:00
|
|
|
|
2017-01-27 23:41:11 +08:00
|
|
|
return (
|
2017-10-02 22:12:58 +08:00
|
|
|
<div className={cx({
|
|
|
|
[styles.disabled]: !!disabled,
|
|
|
|
}, styles.container, className)}
|
|
|
|
>
|
2017-01-27 23:41:11 +08:00
|
|
|
<input
|
2017-06-03 03:25:02 +08:00
|
|
|
type="checkbox"
|
2017-01-27 23:41:11 +08:00
|
|
|
onChange={this.handleChange}
|
2017-10-02 22:12:58 +08:00
|
|
|
checked={checked}
|
2017-04-25 10:08:18 +08:00
|
|
|
className={styles.input}
|
|
|
|
aria-labelledby={ariaLabelledBy}
|
2017-05-04 04:51:17 +08:00
|
|
|
aria-describedby={ariaDescribedBy}
|
2017-10-02 22:12:58 +08:00
|
|
|
disabled={disabled}
|
2017-06-03 03:25:02 +08:00
|
|
|
/>
|
2017-10-02 22:12:58 +08:00
|
|
|
<div role="presentation" onClick={this.handleChange}>
|
|
|
|
{ checked ?
|
2017-06-03 03:25:02 +08:00
|
|
|
<Icon iconName="check" className={cx(styles.icon, styles.checked)} /> :
|
|
|
|
<Icon iconName="circle" className={styles.icon} />
|
2017-02-16 02:49:40 +08:00
|
|
|
}
|
2017-01-27 23:41:11 +08:00
|
|
|
</div>
|
2017-04-25 10:08:18 +08:00
|
|
|
<div id={ariaLabelledBy} hidden>{ariaLabel}</div>
|
|
|
|
<div id={ariaDescribedBy} hidden>{ariaDesc}</div>
|
2017-01-27 23:41:11 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2017-10-02 22:12:58 +08:00
|
|
|
|
|
|
|
Checkbox.propTypes = propTypes;
|
|
|
|
Checkbox.defaultProps = defaultProps;
|