UNPKG

3.48 kBJavaScriptView Raw
1import _extends from 'babel-runtime/helpers/extends';
2import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
3import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
4import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
5import _inherits from 'babel-runtime/helpers/inherits';
6/* eslint-disable jsx-a11y/label-has-for */
7
8import classNames from 'classnames';
9import React from 'react';
10import PropTypes from 'prop-types';
11import warning from 'warning';
12
13import { bsClass, getClassSet, prefix, splitBsProps } from './utils/bootstrapUtils';
14
15var propTypes = {
16 inline: PropTypes.bool,
17 disabled: PropTypes.bool,
18 title: PropTypes.string,
19 /**
20 * Only valid if `inline` is not set.
21 */
22 validationState: PropTypes.oneOf(['success', 'warning', 'error', null]),
23 /**
24 * Attaches a ref to the `<input>` element. Only functions can be used here.
25 *
26 * ```js
27 * <Checkbox inputRef={ref => { this.input = ref; }} />
28 * ```
29 */
30 inputRef: PropTypes.func
31};
32
33var defaultProps = {
34 inline: false,
35 disabled: false,
36 title: ''
37};
38
39var Checkbox = function (_React$Component) {
40 _inherits(Checkbox, _React$Component);
41
42 function Checkbox() {
43 _classCallCheck(this, Checkbox);
44
45 return _possibleConstructorReturn(this, _React$Component.apply(this, arguments));
46 }
47
48 Checkbox.prototype.render = function render() {
49 var _props = this.props,
50 inline = _props.inline,
51 disabled = _props.disabled,
52 validationState = _props.validationState,
53 inputRef = _props.inputRef,
54 className = _props.className,
55 style = _props.style,
56 title = _props.title,
57 children = _props.children,
58 props = _objectWithoutProperties(_props, ['inline', 'disabled', 'validationState', 'inputRef', 'className', 'style', 'title', 'children']);
59
60 var _splitBsProps = splitBsProps(props),
61 bsProps = _splitBsProps[0],
62 elementProps = _splitBsProps[1];
63
64 var input = React.createElement('input', _extends({}, elementProps, {
65 ref: inputRef,
66 type: 'checkbox',
67 disabled: disabled
68 }));
69
70 if (inline) {
71 var _classes2;
72
73 var _classes = (_classes2 = {}, _classes2[prefix(bsProps, 'inline')] = true, _classes2.disabled = disabled, _classes2);
74
75 // Use a warning here instead of in propTypes to get better-looking
76 // generated documentation.
77 process.env.NODE_ENV !== 'production' ? warning(!validationState, '`validationState` is ignored on `<Checkbox inline>`. To display ' + 'validation state on an inline checkbox, set `validationState` on a ' + 'parent `<FormGroup>` or other element instead.') : void 0;
78
79 return React.createElement(
80 'label',
81 {
82 className: classNames(className, _classes),
83 style: style,
84 title: title
85 },
86 input,
87 children
88 );
89 }
90
91 var classes = _extends({}, getClassSet(bsProps), {
92 disabled: disabled
93 });
94 if (validationState) {
95 classes['has-' + validationState] = true;
96 }
97
98 return React.createElement(
99 'div',
100 { className: classNames(className, classes), style: style },
101 React.createElement(
102 'label',
103 { title: title },
104 input,
105 children
106 )
107 );
108 };
109
110 return Checkbox;
111}(React.Component);
112
113Checkbox.propTypes = propTypes;
114Checkbox.defaultProps = defaultProps;
115
116export default bsClass('checkbox', Checkbox);
\No newline at end of file