UNPKG

react-ui-icheck

Version:

iCheck customizable checkboxes and radios built with React.

306 lines (268 loc) 15.3 kB
"use strict"; function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireWildcard(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _isMobile = _interopRequireDefault(require("is-mobile")); var _Input = _interopRequireDefault(require("./components/Input")); var _Label = _interopRequireDefault(require("./components/Label")); var _deprecated = _interopRequireDefault(require("./helpers/deprecated")); var _excluded = ["activeClass", "activeClassName", "aria", "checkedClass", "checkedClassName", "className", "defaultChecked", "disabled", "disabledClass", "disabledClassName", "focusClass", "focusClassName", "hoverClass", "hoverClassName", "id", "increaseArea", "indeterminate", "indeterminateClass", "indeterminateClassName", "inheritClass", "inheritClassName", "inheritID", "insert", "name", "onBlur", "onChange", "onFocus", "value", "checkboxClass", "checked", "children", "label", "labelClassName", "labelHover", "labelHoverClass", "labelTag", "labelTagClassName", "onMouseDown", "onMouseLeave", "onMouseUp", "onTouchEnd", "onTouchStart", "radioClass", "type"]; function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _extends() { _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; }; return _extends.apply(this, arguments); } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } var BaseInput = /*#__PURE__*/function (_Component) { _inherits(BaseInput, _Component); var _super = _createSuper(BaseInput); function BaseInput(props) { var _this; _classCallCheck(this, BaseInput); _this = _super.call(this, props); _this.handleHover = _this.handleHover.bind(_assertThisInitialized(_this)); var checked = props.checked, defaultChecked = props.defaultChecked; _this.state = { checked: defaultChecked || checked, hovered: false, isMobile: (0, _isMobile.default)(), prevPropChecked: props.checked }; return _this; } _createClass(BaseInput, [{ key: "handleHover", value: function handleHover(event, hovered) { this.setState({ hovered: hovered }); } }, { key: "renderInput", value: function renderInput(props) { var _this2 = this; var _this$props = this.props, activeClass = _this$props.activeClass, activeClassName = _this$props.activeClassName, aria = _this$props.aria, checkedClass = _this$props.checkedClass, checkedClassName = _this$props.checkedClassName, className = _this$props.className, defaultChecked = _this$props.defaultChecked, disabled = _this$props.disabled, disabledClass = _this$props.disabledClass, disabledClassName = _this$props.disabledClassName, focusClass = _this$props.focusClass, focusClassName = _this$props.focusClassName, hoverClass = _this$props.hoverClass, hoverClassName = _this$props.hoverClassName, id = _this$props.id, increaseArea = _this$props.increaseArea, indeterminate = _this$props.indeterminate, indeterminateClass = _this$props.indeterminateClass, indeterminateClassName = _this$props.indeterminateClassName, inheritClass = _this$props.inheritClass, inheritClassName = _this$props.inheritClassName, inheritID = _this$props.inheritID, insert = _this$props.insert, name = _this$props.name, onBlur = _this$props.onBlur, onChange = _this$props.onChange, onFocus = _this$props.onFocus, value = _this$props.value, checkboxClass = _this$props.checkboxClass, checked = _this$props.checked, children = _this$props.children, label = _this$props.label, labelClassName = _this$props.labelClassName, labelHover = _this$props.labelHover, labelHoverClass = _this$props.labelHoverClass, labelTag = _this$props.labelTag, labelTagClassName = _this$props.labelTagClassName, onMouseDown = _this$props.onMouseDown, onMouseLeave = _this$props.onMouseLeave, onMouseUp = _this$props.onMouseUp, onTouchEnd = _this$props.onTouchEnd, onTouchStart = _this$props.onTouchStart, radioClass = _this$props.radioClass, type = _this$props.type, other = _objectWithoutProperties(_this$props, _excluded); var _this$state = this.state, stateChecked = _this$state.checked, hovered = _this$state.hovered, isMobile = _this$state.isMobile; return /*#__PURE__*/_react.default.createElement(_Input.default, _extends({}, other, props, { activeClassName: activeClass || activeClassName, aria: aria, checked: stateChecked, checkedClassName: checkedClass || checkedClassName, className: className, disabled: disabled, disabledClassName: disabledClass || disabledClassName, focusClassName: focusClass || focusClassName, hoverClassName: hoverClass || hoverClassName, hovered: hovered, id: id, increaseArea: increaseArea, indeterminate: indeterminate, indeterminateClassName: indeterminateClass || indeterminateClassName, inheritClassName: inheritClass || inheritClassName, inheritID: inheritID, insert: insert, isMobile: isMobile, name: name, ref: function ref(c) { _this2.input = c; }, value: value, onBlur: onBlur, onChange: onChange, onFocus: onFocus })); } }, { key: "render", value: function render() { var _this$props2 = this.props, children = _this$props2.children, disabled = _this$props2.disabled, label = _this$props2.label, labelClassName = _this$props2.labelClassName, labelHover = _this$props2.labelHover, labelTag = _this$props2.labelTag, labelTagClassName = _this$props2.labelTagClassName; var isMobile = this.state.isMobile; if (!label && !children) { return this.renderInput(); } return /*#__PURE__*/_react.default.createElement(_Label.default, { disabled: disabled, handleHover: this.handleHover, isMobile: isMobile, label: label, labelChildren: children, labelClassName: labelClassName, labelHover: labelHover, labelTag: labelTag, labelTagClassName: labelTagClassName }, this.renderInput()); } }], [{ key: "getDerivedStateFromProps", value: function getDerivedStateFromProps(props, state) { if (state.prevPropChecked !== props.checked) { return { checked: props.checked, prevPropChecked: props.checked }; } return null; } }]); return BaseInput; }(_react.Component); BaseInput.propTypes = { activeClass: (0, _deprecated.default)(_propTypes.default.string, 'activeClassName'), activeClassName: _propTypes.default.string, aria: _propTypes.default.bool, checked: _propTypes.default.bool, checkedClass: (0, _deprecated.default)(_propTypes.default.string, 'checkedClassName'), checkedClassName: _propTypes.default.string, children: _propTypes.default.node, className: _propTypes.default.string, defaultChecked: (0, _deprecated.default)(_propTypes.default.bool, 'checked'), disabled: _propTypes.default.bool, disabledClass: (0, _deprecated.default)(_propTypes.default.string, 'disabledClassName'), disabledClassName: _propTypes.default.string, focusClass: (0, _deprecated.default)(_propTypes.default.string, 'focusClassName'), focusClassName: _propTypes.default.string, hoverClass: (0, _deprecated.default)(_propTypes.default.string, 'hoverClassName'), hoverClassName: _propTypes.default.string, id: _propTypes.default.string, increaseArea: _propTypes.default.string, indeterminate: _propTypes.default.bool, indeterminateClass: (0, _deprecated.default)(_propTypes.default.string, 'indeterminateClassName'), indeterminateClassName: _propTypes.default.string, inheritClass: (0, _deprecated.default)(_propTypes.default.bool, 'inheritClassName'), inheritClassName: _propTypes.default.bool, inheritID: _propTypes.default.bool, insert: _propTypes.default.node, label: _propTypes.default.node, labelClassName: _propTypes.default.string, labelHover: _propTypes.default.bool, labelHoverClass: _propTypes.default.string, labelTag: _propTypes.default.string, labelTagClassName: _propTypes.default.string, name: _propTypes.default.string, type: _propTypes.default.string, value: _propTypes.default.string, onBlur: _propTypes.default.func, onChange: _propTypes.default.func, onFocus: _propTypes.default.func, onMouseDown: _propTypes.default.func, onMouseLeave: _propTypes.default.func, onMouseUp: _propTypes.default.func, onTouchEnd: _propTypes.default.func, onTouchStart: _propTypes.default.func }; BaseInput.defaultProps = { activeClass: null, activeClassName: 'active', aria: false, checked: false, checkedClass: null, checkedClassName: 'checked', children: null, className: null, defaultChecked: null, disabled: false, disabledClass: null, disabledClassName: 'disabled', focusClass: null, focusClassName: 'focus', hoverClass: null, hoverClassName: 'hover', id: null, increaseArea: '', indeterminate: true, indeterminateClass: null, indeterminateClassName: 'indeterminate', inheritClass: null, inheritClassName: false, inheritID: false, insert: null, label: null, labelClassName: null, labelHover: true, labelHoverClass: 'hover', labelTag: 'span', labelTagClassName: null, name: null, type: null, value: '', onBlur: null, onChange: null, onFocus: null, onMouseDown: null, onMouseLeave: null, onMouseUp: null, onTouchEnd: null, onTouchStart: null }; var _default = BaseInput; exports.default = _default;