UNPKG

@yncoder/element-react

Version:
216 lines (176 loc) 6.22 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _createClass2 = require('babel-runtime/helpers/createClass'); var _createClass3 = _interopRequireDefault(_createClass2); var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); var _inherits2 = require('babel-runtime/helpers/inherits'); var _inherits3 = _interopRequireDefault(_inherits2); var _react = require('react'); var React = _interopRequireWildcard(_react); var _libs = require('../../libs'); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } (function () { var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined; enterModule && enterModule(module); })(); var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) { return a; }; var Checkbox = function (_Component) { (0, _inherits3.default)(Checkbox, _Component); function Checkbox(props) { (0, _classCallCheck3.default)(this, Checkbox); var _this = (0, _possibleConstructorReturn3.default)(this, (Checkbox.__proto__ || Object.getPrototypeOf(Checkbox)).call(this, props)); _this.state = { checked: props.checked, focus: props.focus, label: _this.getLabel(props) }; return _this; } (0, _createClass3.default)(Checkbox, [{ key: 'componentWillReceiveProps', value: function componentWillReceiveProps(nextProps) { this.setState({ checked: nextProps.checked, focus: nextProps.focus, label: this.getLabel(nextProps) }); } }, { key: 'onFocus', value: function onFocus() { this.setState({ focus: true }); } }, { key: 'onBlur', value: function onBlur() { this.setState({ focus: false }); } }, { key: 'onChange', value: function onChange(e) { var _this2 = this; if (e.target instanceof HTMLInputElement) { var _label = this.state.label; var _props = this.props, trueLabel = _props.trueLabel, falseLabel = _props.falseLabel; var _checked = e.target.checked; var group = this.context.ElCheckboxGroup; if (group) { var length = group.state.options.length + (_checked ? 1 : -1); if (group.props.min !== undefined && length < group.props.min) { return; } if (group.props.max !== undefined && length > group.props.max) { return; } } var newLabel = _label; if (this.props.trueLabel || this.props.falseLabel) { newLabel = _checked ? trueLabel : falseLabel; } this.setState({ checked: _checked, label: newLabel }, function () { if (_this2.props.onChange) { _this2.props.onChange(_checked); } }); } } }, { key: 'getLabel', value: function getLabel(props) { if (props.trueLabel || props.falseLabel) { return props.checked ? props.trueLabel : props.falseLabel; } else { return props.label; } } }, { key: 'render', value: function render() { return React.createElement( 'label', { style: this.style(), className: this.className('el-checkbox') }, React.createElement( 'span', { className: this.classNames('el-checkbox__input', { 'is-disabled': this.props.disabled, 'is-checked': this.state.checked, 'is-indeterminate': this.props.indeterminate, 'is-focus': this.state.focus }) }, React.createElement('span', { className: 'el-checkbox__inner' }), React.createElement('input', { className: 'el-checkbox__original', type: 'checkbox', checked: this.state.checked, disabled: this.props.disabled, onFocus: this.onFocus.bind(this), onBlur: this.onBlur.bind(this), onChange: this.onChange.bind(this) }) ), React.createElement( 'span', { className: 'el-checkbox__label' }, this.props.children || this.state.label ) ); } }, { key: '__reactstandin__regenerateByEval', // @ts-ignore value: function __reactstandin__regenerateByEval(key, code) { // @ts-ignore this[key] = eval(code); } }]); return Checkbox; }(_libs.Component); Checkbox.elementType = 'Checkbox'; var _default = Checkbox; exports.default = _default; Checkbox.contextTypes = { ElCheckboxGroup: _libs.PropTypes.any }; Checkbox.propTypes = { label: _libs.PropTypes.string, trueLabel: _libs.PropTypes.oneOfType([_libs.PropTypes.string, _libs.PropTypes.number]), falseLabel: _libs.PropTypes.oneOfType([_libs.PropTypes.string, _libs.PropTypes.number]), disabled: _libs.PropTypes.bool, checked: _libs.PropTypes.bool, indeterminate: _libs.PropTypes.bool, focus: _libs.PropTypes.bool, onChange: _libs.PropTypes.func }; Checkbox.defaultProps = { checked: false, focus: false }; ; (function () { var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined; if (!reactHotLoader) { return; } reactHotLoader.register(Checkbox, 'Checkbox', 'src/checkbox/CheckBox.jsx'); reactHotLoader.register(_default, 'default', 'src/checkbox/CheckBox.jsx'); })(); ; (function () { var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined; leaveModule && leaveModule(module); })();