react-ui-icheck
Version:
iCheck customizable checkboxes and radios built with React.
346 lines (295 loc) • 15.6 kB
JavaScript
;
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 _classnames = _interopRequireDefault(require("classnames"));
var _excluded = ["activeClassName", "aria", "checkboxClassName", "checkedClassName", "className", "disabled", "disabledClassName", "focusClassName", "hoverClassName", "id", "increaseArea", "indeterminateClassName", "inheritClassName", "inheritID", "inputType", "insert", "isMobile", "name", "radioClassName", "value", "hovered", "indeterminate"];
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 _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(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 iCheck = 'iCheck';
var iCheckHelper = "".concat(iCheck, "-helper");
var Input = /*#__PURE__*/function (_Component) {
_inherits(Input, _Component);
var _super = _createSuper(Input);
function Input(props) {
var _this;
_classCallCheck(this, Input);
_this = _super.call(this, props);
_this.handleBlur = _this.handleBlur.bind(_assertThisInitialized(_this));
_this.handleChange = _this.handleChange.bind(_assertThisInitialized(_this));
_this.handleFocus = _this.handleFocus.bind(_assertThisInitialized(_this));
_this.handleHelperClick = _this.handleHelperClick.bind(_assertThisInitialized(_this));
var checked = props.checked,
hovered = props.hovered,
indeterminate = props.indeterminate;
_this.state = {
active: false,
focused: false,
prevPropChecked: props.checked,
checked: checked,
hovered: hovered,
indeterminate: indeterminate
};
return _this;
}
_createClass(Input, [{
key: "handleBlur",
value: function handleBlur(event) {
var onBlur = this.props.onBlur;
this.setState({
focused: false
});
if (onBlur) onBlur(event);
}
}, {
key: "handleChange",
value: function handleChange(event) {
var onChange = this.props.onChange;
var checked = event.target.checked;
this.setState(function (state) {
return {
checked: !state.checked
};
});
if (onChange) onChange(event, checked);
}
}, {
key: "handleFocus",
value: function handleFocus(event) {
var onFocus = this.props.onFocus;
this.setState({
focused: true
});
if (onFocus) onFocus(event);
}
}, {
key: "handleHelperClick",
value: function handleHelperClick(event) {
var _this$props = this.props,
disabled = _this$props.disabled,
inputType = _this$props.inputType,
insert = _this$props.insert,
label = _this$props.label,
onChange = _this$props.onChange;
var checked = this.state.checked;
if (!label && !insert || disabled) return;
if (inputType.toString() === 'checkbox') this.setState(function (state) {
return {
checked: !state.checked
};
});
event.preventDefault();
event.stopPropagation();
if (onChange) {
// make sure <ins /> element is not target
event.target = this.checkbox; // eslint-disable-line no-param-reassign
onChange(event, checked);
}
}
}, {
key: "render",
value: function render() {
var _classNames,
_this2 = this;
var _this$props2 = this.props,
activeClassName = _this$props2.activeClassName,
aria = _this$props2.aria,
checkboxClassName = _this$props2.checkboxClassName,
checkedClassName = _this$props2.checkedClassName,
className = _this$props2.className,
disabled = _this$props2.disabled,
disabledClassName = _this$props2.disabledClassName,
focusClassName = _this$props2.focusClassName,
hoverClassName = _this$props2.hoverClassName,
id = _this$props2.id,
increaseArea = _this$props2.increaseArea,
indeterminateClassName = _this$props2.indeterminateClassName,
inheritClassName = _this$props2.inheritClassName,
inheritID = _this$props2.inheritID,
inputType = _this$props2.inputType,
insert = _this$props2.insert,
isMobile = _this$props2.isMobile,
name = _this$props2.name,
radioClassName = _this$props2.radioClassName,
value = _this$props2.value,
hovered = _this$props2.hovered,
indeterminate = _this$props2.indeterminate,
other = _objectWithoutProperties(_this$props2, _excluded);
var _this$state = this.state,
active = _this$state.active,
checked = _this$state.checked,
focused = _this$state.focused,
stateHovered = _this$state.hovered,
stateIndeterminate = _this$state.indeterminate;
var wrapProps = {
className: (0, _classnames.default)((_classNames = {}, _defineProperty(_classNames, checkboxClassName, inputType.toString() === 'checkbox'), _defineProperty(_classNames, radioClassName, inputType.toString() === 'radio'), _defineProperty(_classNames, indeterminateClassName, stateIndeterminate), _defineProperty(_classNames, checkedClassName, checked), _defineProperty(_classNames, disabledClassName, disabled), _defineProperty(_classNames, hoverClassName, stateHovered), _defineProperty(_classNames, focusClassName, focused), _defineProperty(_classNames, activeClassName, active), _classNames))
};
if (inheritClassName) wrapProps.className = (0, _classnames.default)(wrapProps.className, className);
if (inheritID && id) wrapProps.id = "".concat(iCheck, "-").concat(id);
if (aria) {
wrapProps.role = inputType; // Set ARIA "labelledby"
wrapProps['aria-labelledby'] = wrapProps.id;
wrapProps['aria-checked'] = checked;
} // Clickable area
var area = "".concat(increaseArea).replace('%', '') || 0;
if (area < -50) area = -50; // Layer styles
var offset = "".concat(-area, "%");
var size = "".concat(100 + area * 2, "%");
var layer = {
position: 'absolute',
top: offset,
left: offset,
display: 'block',
width: size,
height: size,
margin: 0,
padding: 0,
background: '#fff',
border: 0,
opacity: 0,
cursor: disabled ? 'default' : 'pointer'
}; // Layer addition
var helper = /*#__PURE__*/_react.default.createElement("ins", {
className: iCheckHelper,
role: "presentation",
style: layer,
onClick: this.handleHelperClick
}); // Choose how to hide input
var hide;
if (isMobile) {
hide = {
position: 'absolute',
visibility: 'hidden'
};
} else if (area) {
hide = layer;
} else {
hide = {
opacity: 0,
position: 'absolute'
};
}
var inputElement = /*#__PURE__*/_react.default.createElement("input", _extends({}, other, {
checked: checked,
disabled: disabled,
name: name,
ref: function ref(c) {
_this2.checkbox = c;
},
style: hide,
type: inputType,
value: value,
onBlur: this.handleBlur,
onChange: this.handleChange,
onFocus: this.handleFocus
}));
var insertElement = null;
if (insert) {
insertElement = /*#__PURE__*/_react.default.isValidElement(insert) ? insert :
/*#__PURE__*/
// eslint-disable-next-line react/no-danger
_react.default.createElement("div", {
dangerouslySetInnerHTML: {
__html: insert
}
});
}
return /*#__PURE__*/_react.default.createElement("div", wrapProps, inputElement, insertElement, helper);
}
}], [{
key: "getDerivedStateFromProps",
value: function getDerivedStateFromProps(props, state) {
if (state.prevPropChecked !== props.checked) {
return {
checked: props.checked,
prevPropChecked: props.checked
};
}
if (props.hovered !== state.hovered) {
return {
hovered: props.hovered
};
}
return null;
}
}]);
return Input;
}(_react.Component);
Input.propTypes = {
inputType: _propTypes.default.string.isRequired,
activeClassName: _propTypes.default.string,
aria: _propTypes.default.bool,
checkboxClassName: _propTypes.default.string,
checked: _propTypes.default.bool,
checkedClassName: _propTypes.default.string,
className: _propTypes.default.string,
disabled: _propTypes.default.bool,
disabledClassName: _propTypes.default.string,
focusClassName: _propTypes.default.string,
hoverClassName: _propTypes.default.string,
hovered: _propTypes.default.bool,
id: _propTypes.default.string,
increaseArea: _propTypes.default.string,
indeterminate: _propTypes.default.bool,
indeterminateClassName: _propTypes.default.string,
inheritClassName: _propTypes.default.bool,
inheritID: _propTypes.default.bool,
insert: _propTypes.default.node,
isMobile: _propTypes.default.bool,
label: _propTypes.default.string,
name: _propTypes.default.string,
radioClassName: _propTypes.default.string,
value: _propTypes.default.string,
onBlur: _propTypes.default.func,
onChange: _propTypes.default.func,
onFocus: _propTypes.default.func
};
Input.defaultProps = {
activeClassName: null,
aria: false,
checkboxClassName: 'icheckbox',
checked: false,
checkedClassName: 'checked',
className: null,
disabled: false,
disabledClassName: 'disabled',
focusClassName: 'focus',
hoverClassName: 'hover',
hovered: false,
id: null,
increaseArea: '',
indeterminate: false,
indeterminateClassName: 'indeterminate',
inheritClassName: false,
inheritID: false,
insert: null,
isMobile: false,
label: null,
name: null,
radioClassName: 'iradio',
value: '',
onBlur: null,
onChange: null,
onFocus: null
};
var _default = Input;
exports.default = _default;