hy-checkbox
Version:
checkbox & radio ui components for react
128 lines (99 loc) • 6.36 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
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 _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _classnames = require('classnames');
var _classnames2 = _interopRequireDefault(_classnames);
var _CheckBox = require('..//CheckBox');
var _CheckBox2 = _interopRequireDefault(_CheckBox);
var _formsyReact = require('formsy-react');
var _formsyReact2 = _interopRequireDefault(_formsyReact);
var _Label = require('..//Label');
var _Label2 = _interopRequireDefault(_Label);
require('./form.less');
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; } /*
* @Author: SiMeiyu
* @Date: 2017-07-04 10:01:13
*/
var HOC = _formsyReact2.default.HOC;
var CheckboxGroup = _CheckBox2.default.CheckboxGroup;
var CheckboxGroupForm = function (_React$Component) {
_inherits(CheckboxGroupForm, _React$Component);
function CheckboxGroupForm(props) {
_classCallCheck(this, CheckboxGroupForm);
var _this = _possibleConstructorReturn(this, (CheckboxGroupForm.__proto__ || Object.getPrototypeOf(CheckboxGroupForm)).call(this, props));
_this.state = {
value: _this.props.defaultValue || []
};
return _this;
}
_createClass(CheckboxGroupForm, [{
key: 'componentDidMount',
value: function componentDidMount() {
this.props.setValue(this.props.defaultValue || []);
}
}, {
key: 'changeValue',
value: function changeValue(value) {
this.setState({
value: value
});
this.props.setValue(value);
}
}, {
key: 'render',
value: function render() {
var _props = this.props,
className = _props.className,
prefixCls = _props.prefixCls,
label = _props.label,
name = _props.name,
items = _props.items,
defaultValue = _props.defaultValue,
inline = _props.inline,
isRequired = _props.isRequired;
var validCls = this.props.showRequired() ? 'required' : this.props.showError() ? 'error' : '';
var classes = (0, _classnames2.default)(prefixCls + '-form-group', className, validCls);
var errorMessage = this.props.getErrorMessage();
return _react2.default.createElement(
'div',
{ className: classes },
_react2.default.createElement(_Label2.default, {
name: label,
require: isRequired()
}),
_react2.default.createElement(
'div',
{ className: prefixCls + '-form-checkbox' },
_react2.default.createElement(CheckboxGroup, { name: name, items: items, defaultValue: this.state.value, onChange: this.changeValue.bind(this), inline: inline })
),
_react2.default.createElement(
'span',
{ className: 'validation-error' },
errorMessage
)
);
}
}]);
return CheckboxGroupForm;
}(_react2.default.Component);
CheckboxGroupForm.propTypes = {
items: _react2.default.PropTypes.array.isRequired,
className: _react2.default.PropTypes.string,
defaultValue: _react2.default.PropTypes.array,
label: _react2.default.PropTypes.string,
name: _react2.default.PropTypes.string,
prefixCls: _react2.default.PropTypes.string,
inline: _react2.default.PropTypes.bool
};
CheckboxGroupForm.defaultProps = {
prefixCls: "ult",
inline: false
};
exports.default = HOC(CheckboxGroupForm);