@trendmicro/react-checkbox
Version:
React Checkbox component
462 lines (365 loc) • 18.7 kB
JavaScript
/*! react-checkbox v3.4.1 | (c) 2018 Trend Micro Inc. | MIT | https://github.com/trendmicro-frontend/react-checkbox */
module.exports =
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 4);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _class, _temp2;
var _classnames = __webpack_require__(5);
var _classnames2 = _interopRequireDefault(_classnames);
var _react = __webpack_require__(1);
var _react2 = _interopRequireDefault(_react);
var _propTypes = __webpack_require__(2);
var _propTypes2 = _interopRequireDefault(_propTypes);
var _chainedFunction = __webpack_require__(3);
var _chainedFunction2 = _interopRequireDefault(_chainedFunction);
var _index = __webpack_require__(6);
var _index2 = _interopRequireDefault(_index);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _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; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _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; }
function _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; }
var noop = function noop() {};
var Checkbox = (_temp2 = _class = function (_PureComponent) {
_inherits(Checkbox, _PureComponent);
function Checkbox() {
var _ref;
var _temp, _this, _ret;
_classCallCheck(this, Checkbox);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Checkbox.__proto__ || Object.getPrototypeOf(Checkbox)).call.apply(_ref, [this].concat(args))), _this), _this.checkbox = null, _this.updateIndeterminateState = function () {
if (typeof _this.props.indeterminate !== 'undefined') {
_this.checkbox.indeterminate = !!_this.props.indeterminate;
}
}, _temp), _possibleConstructorReturn(_this, _ret);
}
_createClass(Checkbox, [{
key: 'render',
value: function render() {
var _this2 = this;
var _props = this.props,
label = _props.label,
inputClassName = _props.inputClassName,
inputStyle = _props.inputStyle,
disabled = _props.disabled,
defaultIndeterminate = _props.defaultIndeterminate,
_props$onChange = _props.onChange,
onChange = _props$onChange === undefined ? noop : _props$onChange,
_props$onClick = _props.onClick,
onClick = _props$onClick === undefined ? noop : _props$onClick,
className = _props.className,
style = _props.style,
children = _props.children,
props = _objectWithoutProperties(_props, ['label', 'inputClassName', 'inputStyle', 'disabled', 'defaultIndeterminate', 'onChange', 'onClick', 'className', 'style', 'children']);
delete props.indeterminate;
return _react2.default.createElement(
'label',
{
className: (0, _classnames2.default)(className, _index2.default.controlCheckbox, _defineProperty({}, _index2.default.disabled, disabled)),
style: style
},
_react2.default.createElement('input', _extends({}, props, {
ref: function ref(node) {
_this2.checkbox = node;
var indeterminate = typeof _this2.props.indeterminate !== 'undefined' ? _this2.props.indeterminate : defaultIndeterminate;
node && (_this2.checkbox.indeterminate = indeterminate);
},
type: 'checkbox',
disabled: disabled,
className: (0, _classnames2.default)(inputClassName, _index2.default.inputCheckbox),
style: inputStyle,
onChange: onChange,
onClick: (0, _chainedFunction2.default)(this.updateIndeterminateState, onClick)
})),
_react2.default.createElement('i', { className: _index2.default.controlIndicator }),
label ? _react2.default.createElement(
'span',
{ className: _index2.default.textLabel },
label
) : null,
children
);
}
}, {
key: 'checked',
get: function get() {
if (!this.checkbox) {
return null;
}
return this.checkbox.checked;
}
}, {
key: 'indeterminate',
get: function get() {
if (!this.checkbox) {
return null;
}
return this.checkbox.indeterminate;
}
}]);
return Checkbox;
}(_react.PureComponent), _class.propTypes = {
label: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.node]),
inputClassName: _propTypes2.default.object,
inputStyle: _propTypes2.default.object,
disabled: _propTypes2.default.bool,
checked: _propTypes2.default.bool,
defaultChecked: _propTypes2.default.bool,
indeterminate: _propTypes2.default.bool,
defaultIndeterminate: _propTypes2.default.bool,
onChange: _propTypes2.default.func,
onClick: _propTypes2.default.func
}, _class.defaultProps = {
disabled: false,
defaultIndeterminate: false
}, _temp2);
exports.default = Checkbox;
/***/ }),
/* 1 */
/***/ (function(module, exports) {
module.exports = require("react");
/***/ }),
/* 2 */
/***/ (function(module, exports) {
module.exports = require("prop-types");
/***/ }),
/* 3 */
/***/ (function(module, exports) {
module.exports = require("chained-function");
/***/ }),
/* 4 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.CheckboxGroup = exports.Checkbox = undefined;
var _Checkbox2 = __webpack_require__(0);
Object.defineProperty(exports, 'default', {
enumerable: true,
get: function get() {
return _interopRequireDefault(_Checkbox2).default;
}
});
var _Checkbox3 = _interopRequireDefault(_Checkbox2);
var _CheckboxGroup2 = __webpack_require__(7);
var _CheckboxGroup3 = _interopRequireDefault(_CheckboxGroup2);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
exports.Checkbox = _Checkbox3.default;
exports.CheckboxGroup = _CheckboxGroup3.default;
/***/ }),
/* 5 */
/***/ (function(module, exports) {
module.exports = require("classnames");
/***/ }),
/* 6 */
/***/ (function(module, exports) {
// removed by extract-text-webpack-plugin
module.exports = {"control-checkbox":"checkbox---control-checkbox---2Mo4k","controlCheckbox":"checkbox---control-checkbox---2Mo4k","input-checkbox":"checkbox---input-checkbox---3hbfZ","inputCheckbox":"checkbox---input-checkbox---3hbfZ","control-indicator":"checkbox---control-indicator---dcwXW","controlIndicator":"checkbox---control-indicator---dcwXW","text-label":"checkbox---text-label---2D19D","textLabel":"checkbox---text-label---2D19D","disabled":"checkbox---disabled---eCY9b"};
/***/ }),
/* 7 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _class, _temp2;
var _chainedFunction = __webpack_require__(3);
var _chainedFunction2 = _interopRequireDefault(_chainedFunction);
var _ensureArray = __webpack_require__(8);
var _ensureArray2 = _interopRequireDefault(_ensureArray);
var _propTypes = __webpack_require__(2);
var _propTypes2 = _interopRequireDefault(_propTypes);
var _react = __webpack_require__(1);
var _react2 = _interopRequireDefault(_react);
var _Checkbox = __webpack_require__(0);
var _Checkbox2 = _interopRequireDefault(_Checkbox);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _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; }
function _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; }
var getComponentType = function getComponentType(Component) {
return Component ? _react2.default.createElement(Component, null).type : undefined;
};
var CheckboxGroup = (_temp2 = _class = function (_PureComponent) {
_inherits(CheckboxGroup, _PureComponent);
function CheckboxGroup() {
var _ref;
var _temp, _this, _ret;
_classCallCheck(this, CheckboxGroup);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = CheckboxGroup.__proto__ || Object.getPrototypeOf(CheckboxGroup)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
value: (0, _ensureArray2.default)(_this.props.value || _this.props.defaultValue)
}, _this.handleChange = function (value, event) {
var newValue = void 0;
if (event.target.checked) {
newValue = _this.state.value.concat(value);
} else {
newValue = _this.state.value.filter(function (v) {
return v !== value;
});
}
if (_this.props.value !== undefined) {
// Controlled component
_this.setState({ value: (0, _ensureArray2.default)(_this.props.value) });
} else {
// Uncontrolled component
_this.setState({ value: newValue });
}
if (typeof _this.props.onChange === 'function') {
_this.props.onChange(newValue, event);
}
}, _this.renderChildren = function (children) {
var depth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
if (depth > _this.props.depth) {
return children;
}
var mapChild = function mapChild(child) {
if (!_react2.default.isValidElement(child) || !child.props) {
return child;
}
if (child.type === getComponentType(CheckboxGroup)) {
// No nested checkbox groups
return child;
}
if (child.type === getComponentType(_Checkbox2.default)) {
return (0, _react.cloneElement)(child, {
checked: _this.state.value.indexOf(child.props.value) >= 0,
disabled: _this.props.disabled || child.props.disabled,
onChange: (0, _chainedFunction2.default)(child.props.onChange, function (event) {
_this.handleChange(child.props.value, event);
})
});
}
if (child.props.children && _typeof(child.props.children) === 'object') {
return (0, _react.cloneElement)(child, {
children: _this.renderChildren(child.props.children, depth + 1)
});
}
return child;
};
if (Array.isArray(children)) {
return _react2.default.Children.map(children, mapChild);
} else {
return mapChild(children);
}
}, _temp), _possibleConstructorReturn(_this, _ret);
}
_createClass(CheckboxGroup, [{
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
if (nextProps.value !== undefined) {
this.setState({
value: (0, _ensureArray2.default)(nextProps.value)
});
}
}
}, {
key: 'render',
value: function render() {
return this.renderChildren(this.props.children);
}
}, {
key: 'value',
get: function get() {
return this.state.value;
}
}]);
return CheckboxGroup;
}(_react.PureComponent), _class.propTypes = {
disabled: _propTypes2.default.bool,
name: _propTypes2.default.string,
value: _propTypes2.default.arrayOf(_propTypes2.default.any),
defaultValue: _propTypes2.default.arrayOf(_propTypes2.default.any),
onChange: _propTypes2.default.func,
depth: _propTypes2.default.number
}, _class.defaultProps = {
disabled: false,
depth: 1
}, _temp2);
exports.default = CheckboxGroup;
/***/ }),
/* 8 */
/***/ (function(module, exports) {
module.exports = require("ensure-array");
/***/ })
/******/ ]);
//# sourceMappingURL=index.js.map