UNPKG

7.26 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _react = require('react');
8
9var _react2 = _interopRequireDefault(_react);
10
11var _propTypes = require('prop-types');
12
13var _propTypes2 = _interopRequireDefault(_propTypes);
14
15var _style = require('./style.scss');
16
17var _style2 = _interopRequireDefault(_style);
18
19var _bind = require('classnames/bind');
20
21var _bind2 = _interopRequireDefault(_bind);
22
23var _Icon = require('../Icon');
24
25var _Icon2 = _interopRequireDefault(_Icon);
26
27function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
28
29function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
30
31function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
32
33function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
34
35function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
36
37var Checkbox = function (_React$Component) {
38 _inherits(Checkbox, _React$Component);
39
40 function Checkbox(props) {
41 _classCallCheck(this, Checkbox);
42
43 var _this = _possibleConstructorReturn(this, (Checkbox.__proto__ || Object.getPrototypeOf(Checkbox)).call(this, props));
44
45 _this.state = {
46 value: _this.props.value,
47 iconName: _this.props.iconName
48 };
49
50 _this.handleChange = function (event) {
51 event.persist();
52
53 // Allow user to interact with locked checkboxes only if the value is false (unchecked)
54 if (!_this.props.locked || !_this.props.value) {
55 _this.setState({ value: event.target.checked }, function () {
56 if (typeof _this.props.changeCallback === 'function') {
57 _this.props.changeCallback(event);
58 }
59 });
60 }
61 };
62
63 _this.UNSAFE_componentWillReceiveProps = function (nextProps) {
64 var newState = {};
65
66 if (nextProps.value !== _this.props.value) {
67 newState.value = nextProps.value;
68 }
69
70 if (nextProps.iconName !== _this.state.iconName) {
71 newState.iconName = nextProps.iconName;
72 }
73
74 _this.setState(newState);
75 };
76
77 _this.getLabel = function () {
78 var title = typeof _this.props.label === 'string' ? _this.props.label : '';
79
80 if (_this.props.label && _this.props.description) {
81 return _react2.default.createElement(
82 'div',
83 { className: _style2.default['label-wrapper'] },
84 _react2.default.createElement(
85 'label',
86 { htmlFor: _this.props.forLabelAttr },
87 _react2.default.createElement(
88 'span',
89 { className: _style2.default['label-title'], title: title },
90 _this.props.label
91 ),
92 _react2.default.createElement(
93 'span',
94 { className: _style2.default['label-description'] },
95 _this.props.description
96 )
97 )
98 );
99 }
100
101 if (_this.props.label) {
102 return _react2.default.createElement(
103 'label',
104 { htmlFor: _this.props.forLabelAttr, title: title },
105 _this.props.label
106 );
107 }
108
109 return null;
110 };
111
112 _this.render = function () {
113 var _this$props = _this.props,
114 optClass = _this$props.optClass,
115 changeCallback = _this$props.changeCallback,
116 value = _this$props.value,
117 disabled = _this$props.disabled,
118 name = _this$props.name,
119 label = _this$props.label,
120 forLabelAttr = _this$props.forLabelAttr,
121 other = _objectWithoutProperties(_this$props, ['optClass', 'changeCallback', 'value', 'disabled', 'name', 'label', 'forLabelAttr']);
122
123 var cx = _bind2.default.bind(_style2.default);
124 var disabledClass = _this.props.disabled ? _style2.default['checkbox-disabled'] : '';
125 var allowNativeClass = _this.props.allowNative ? _style2.default['checkbox-native'] : null;
126 var descriptionClass = _this.props.description ? _style2.default['has-description'] : null;
127 var checkboxClass = cx(_style2.default['checkbox-component'], allowNativeClass, descriptionClass, disabledClass, optClass);
128 var inputFillColor = _this.props.disabled ? '#9198A0' : '#3C97D3';
129 var labelWrapperClass = _this.props.description ? _style2.default['label-group'] : null;
130 var title = typeof label === 'string' ? label : '';
131
132 return _react2.default.createElement(
133 'div',
134 { className: checkboxClass },
135 _react2.default.createElement('input', { type: 'checkbox', id: _this.props.forLabelAttr, checked: _this.state.value, onChange: _this.handleChange, value: value, disabled: disabled, name: name, label: label, title: title }),
136 _react2.default.createElement(
137 'div',
138 { className: labelWrapperClass },
139 _react2.default.createElement(
140 'div',
141 { className: _style2.default['checkbox-input'] },
142 _react2.default.createElement(_Icon2.default, { name: _this.state.iconName, fill: inputFillColor })
143 ),
144 _this.getLabel()
145 )
146 );
147 };
148
149 return _this;
150 }
151
152 return Checkbox;
153}(_react2.default.Component);
154
155Checkbox.defaultProps = {
156 disabled: false,
157 iconName: 'md-check',
158 locked: false
159};
160Checkbox.propTypes = {
161 /**
162 * Displays a native checkbox, instead of the custom implementation.
163 */
164 allowNative: _propTypes2.default.bool,
165 /**
166 * Whether the checkbox is disabled.
167 */
168 disabled: _propTypes2.default.bool,
169 /**
170 * Value of the input. Sets whether the component is checked or not.
171 */
172 value: _propTypes2.default.bool,
173 /**
174 * Unique string to be passed to the label 'for' attrbute and the native checkbox 'id',
175 * to allow using label to check/uncheck
176 */
177 forLabelAttr: _propTypes2.default.string,
178 /**
179 * Node displayed with the checkbox (can be a string).
180 */
181 label: _propTypes2.default.node,
182 /**
183 * Optional styles to add to the checkbox.
184 */
185 optClass: _propTypes2.default.string,
186 /**
187 * A callback function to be called when the checkbox changes.
188 */
189 changeCallback: _propTypes2.default.func,
190 /**
191 * Icon to be used in the checkbox.
192 */
193 iconName: _propTypes2.default.string,
194 /**
195 * Whether the checkbox is locked from change outside of receiving props.
196 */
197 locked: _propTypes2.default.bool,
198 /**
199 * Optional description that appears below the label.
200 */
201 description: _propTypes2.default.string
202};
203exports.default = Checkbox;
\No newline at end of file