react-toolbox/components/radio/RadioButton.js

76 lines
2.2 KiB
JavaScript
Raw Normal View History

2016-05-29 23:09:02 +03:00
import React, { Component, PropTypes } from 'react';
2016-05-22 23:18:41 +03:00
import classnames from 'classnames';
import { themr } from 'react-css-themr';
2016-05-29 23:09:02 +03:00
import { RADIO } from '../identifiers.js';
import rippleFactory from '../ripple/Ripple.js';
import radioFactory from './Radio.js';
2015-10-06 20:40:47 +03:00
2016-05-29 23:09:02 +03:00
const factory = (Radio) => {
class RadioButton extends Component {
static propTypes = {
checked: PropTypes.bool,
className: PropTypes.string,
disabled: PropTypes.bool,
label: PropTypes.string,
name: PropTypes.string,
onBlur: PropTypes.func,
onChange: PropTypes.func,
onFocus: PropTypes.func,
theme: PropTypes.shape({
disabled: PropTypes.string.isRequired,
field: PropTypes.string.isRequired,
input: PropTypes.string.isRequired,
text: PropTypes.string.isRequired
}),
value: PropTypes.any
};
2015-10-06 20:40:47 +03:00
2016-05-29 23:09:02 +03:00
static defaultProps = {
checked: false,
className: '',
disabled: false
};
2015-10-06 20:40:47 +03:00
2016-05-29 23:09:02 +03:00
handleClick = (event) => {
const {checked, disabled, onChange} = this.props;
if (event.pageX !== 0 && event.pageY !== 0) this.blur();
if (!disabled && !checked && onChange) onChange(event, this);
};
2016-05-29 23:09:02 +03:00
blur () {
this.refs.input.blur();
}
2016-05-29 23:09:02 +03:00
focus () {
this.refs.input.focus();
}
2016-05-29 23:09:02 +03:00
render () {
const { className, checked, disabled, label, theme, onChange, ...others } = this.props; // eslint-disable-line
const _className = classnames(theme[this.props.disabled ? 'disabled' : 'field'], className);
return (
<label data-react-toolbox='radio-button' className={_className}>
<input
{...others}
className={theme.input}
onClick={this.handleClick}
readOnly
ref='input'
type='radio'
/>
<Radio checked={checked} disabled={disabled} theme={theme} />
{label ? <span className={theme.text}>{label}</span> : null}
</label>
);
}
}
2015-10-22 02:31:17 +03:00
2016-05-29 23:09:02 +03:00
return RadioButton;
};
const Radio = radioFactory(rippleFactory({ centered: true, spread: 2.6 }));
const RadioButton = factory(Radio);
export default themr(RADIO)(RadioButton);
export { factory as radioButtonFactory };
export { RadioButton };