react-toolbox/components/radio/RadioButton.js

61 lines
1.6 KiB
JavaScript
Raw Normal View History

import React from 'react';
2015-11-28 21:54:45 +03:00
import ClassNames from 'classnames';
2015-12-07 03:40:59 +03:00
import Radio from './Radio';
2015-10-06 20:40:47 +03:00
import style from './style';
2015-10-22 02:31:17 +03:00
class RadioButton extends React.Component {
static propTypes = {
2015-10-06 20:40:47 +03:00
checked: React.PropTypes.bool,
className: React.PropTypes.string,
disabled: React.PropTypes.bool,
label: React.PropTypes.string,
name: React.PropTypes.string,
onBlur: React.PropTypes.func,
onChange: React.PropTypes.func,
onFocus: React.PropTypes.func,
value: React.PropTypes.any
};
2015-10-06 20:40:47 +03:00
static defaultProps = {
checked: false,
className: '',
disabled: false
};
2015-10-06 20:40:47 +03:00
handleClick = (event) => {
2015-12-07 03:40:59 +03:00
const {checked, disabled, onChange} = this.props;
if (event.pageX !== 0 && event.pageY !== 0) this.blur();
if (!disabled && !checked && onChange) onChange(event, this);
};
blur () {
this.refs.input.blur();
}
focus () {
this.refs.input.focus();
}
2015-10-06 20:40:47 +03:00
render () {
2015-12-07 03:40:59 +03:00
const className = ClassNames(style[this.props.disabled ? 'disabled' : 'field'], this.props.className);
2016-04-09 21:34:34 +03:00
const { onChange, ...others } = this.props; //eslint-disable-line no-unused-vars
2015-10-06 20:40:47 +03:00
return (
2016-02-08 21:43:14 +03:00
<label data-react-toolbox='radio-button' className={className}>
2015-10-06 20:40:47 +03:00
<input
2015-12-07 03:40:59 +03:00
{...others}
2015-10-06 20:40:47 +03:00
className={style.input}
2015-12-07 03:40:59 +03:00
onClick={this.handleClick}
2015-12-07 04:14:10 +03:00
readOnly
2015-12-07 03:40:59 +03:00
ref='input'
type='radio'
2015-10-06 20:40:47 +03:00
/>
2015-12-07 03:40:59 +03:00
<Radio checked={this.props.checked} disabled={this.props.disabled}/>
{this.props.label ? <span className={style.text}>{this.props.label}</span> : null}
2015-10-06 20:40:47 +03:00
</label>
);
}
2015-10-21 13:25:07 +03:00
}
2015-10-22 02:31:17 +03:00
export default RadioButton;