UNPKG

rsuite

Version:

A suite of react components

164 lines (143 loc) 5.48 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose"; import _inheritsLoose from "@babel/runtime/helpers/esm/inheritsLoose"; import _isUndefined from "lodash/isUndefined"; import * as React from 'react'; import PropTypes from 'prop-types'; import classNames from 'classnames'; import setDisplayName from 'recompose/setDisplayName'; import { prefix, defaultProps, getUnhandledProps, partitionHTMLProps } from '../utils'; import { CheckboxContext } from '../CheckboxGroup/CheckboxGroup'; var Checkbox = /*#__PURE__*/ function (_React$Component) { _inheritsLoose(Checkbox, _React$Component); function Checkbox(props) { var _this; _this = _React$Component.call(this, props) || this; _this.context = {}; _this.handleChange = function (event) { var _this$props = _this.props, onChange = _this$props.onChange, disabled = _this$props.disabled, value = _this$props.value; var onContextChange = _this.context.onChange; var checked = !_this.isChecked(); if (disabled) { return; } _this.setState({ checked: checked }, function () { onChange && onChange(value, checked, event); onContextChange && onContextChange(value, checked, event); }); }; _this.state = { checked: props.defaultChecked }; return _this; } var _proto = Checkbox.prototype; _proto.getCheckedByValue = function getCheckedByValue() { var _this2 = this; var value = this.context.value; if (!_isUndefined(value) && !_isUndefined(this.props.value)) { return value.some(function (i) { return i === _this2.props.value; }); } return this.props.checked; }; _proto.isChecked = function isChecked() { var checked = this.getCheckedByValue(); return _isUndefined(checked) ? this.state.checked : checked; }; _proto.render = function render() { var _classNames; var _this$props2 = this.props, disabled = _this$props2.disabled, className = _this$props2.className, children = _this$props2.children, title = _this$props2.title, inputRef = _this$props2.inputRef, indeterminate = _this$props2.indeterminate, tabIndex = _this$props2.tabIndex, classPrefix = _this$props2.classPrefix, onClick = _this$props2.onClick, onCheckboxClick = _this$props2.onCheckboxClick, checkable = _this$props2.checkable, props = _objectWithoutPropertiesLoose(_this$props2, ["disabled", "className", "children", "title", "inputRef", "indeterminate", "tabIndex", "classPrefix", "onClick", "onCheckboxClick", "checkable"]); var checked = this.isChecked(); var _this$context = this.context, _this$context$inline = _this$context.inline, inline = _this$context$inline === void 0 ? this.props.inline : _this$context$inline, _this$context$name = _this$context.name, name = _this$context$name === void 0 ? this.props.name : _this$context$name, controlled = _this$context.controlled; var addPrefix = prefix(classPrefix); var classes = classNames(classPrefix, className, (_classNames = {}, _classNames[addPrefix('inline')] = inline, _classNames[addPrefix('indeterminate')] = indeterminate, _classNames[addPrefix('disabled')] = disabled, _classNames[addPrefix('checked')] = checked, _classNames)); var unhandled = getUnhandledProps(Checkbox, props); var _partitionHTMLProps = partitionHTMLProps(unhandled), htmlInputProps = _partitionHTMLProps[0], rest = _partitionHTMLProps[1]; if (!_isUndefined(controlled)) { htmlInputProps[controlled ? 'checked' : 'defaultChecked'] = checked; } var input = React.createElement("span", { className: addPrefix('wrapper'), onClick: onCheckboxClick, "aria-disabled": disabled }, React.createElement("input", _extends({}, htmlInputProps, { name: name, type: "checkbox", ref: inputRef, tabIndex: tabIndex, onClick: function onClick(event) { return event.stopPropagation(); }, disabled: disabled, onChange: this.handleChange })), React.createElement("span", { className: addPrefix('inner'), "aria-hidden": true, role: "presentation" })); return React.createElement("div", _extends({}, rest, { onClick: onClick, className: classes }), React.createElement("div", { className: addPrefix('checker') }, React.createElement("label", { title: title }, checkable ? input : null, children))); }; return Checkbox; }(React.Component); Checkbox.contextType = CheckboxContext; Checkbox.propTypes = { title: PropTypes.string, className: PropTypes.string, inline: PropTypes.bool, disabled: PropTypes.bool, checked: PropTypes.bool, defaultChecked: PropTypes.bool, indeterminate: PropTypes.bool, onChange: PropTypes.func, onClick: PropTypes.func, inputRef: PropTypes.func, value: PropTypes.any, children: PropTypes.node, classPrefix: PropTypes.string, tabIndex: PropTypes.number, checkable: PropTypes.bool, onCheckboxClick: PropTypes.func }; Checkbox.defaultProps = { checkable: true, tabIndex: 0 }; var EnhancedCheckBox = defaultProps({ classPrefix: 'checkbox' })(Checkbox); export default setDisplayName('Checkbox')(EnhancedCheckBox);