react-kube
Version:
Kube CSS in React Components
122 lines (99 loc) • 5.42 kB
JavaScript
"use strict";
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 _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; desc = parent = getter = undefined; _again = false; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; continue _function; } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
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 _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) subClass.__proto__ = superClass; }
var _react = require("react");
var _react2 = _interopRequireDefault(_react);
var _CheckBox = require("./CheckBox");
var _CheckBox2 = _interopRequireDefault(_CheckBox);
var _classnames = require("classnames");
var _classnames2 = _interopRequireDefault(_classnames);
var CheckAll = (function (_React$Component) {
function CheckAll(props) {
_classCallCheck(this, CheckAll);
_get(Object.getPrototypeOf(CheckAll.prototype), "constructor", this).call(this, props);
this.state = { all: false, checked: null };
}
_inherits(CheckAll, _React$Component);
_createClass(CheckAll, [{
key: "componentWillMount",
value: function componentWillMount() {
this.setState({
checked: this.props.children.map(function (child) {
return {
checked: child.props.checked ? child.props.checked : false
};
})
});
}
}, {
key: "handleSingleCheck",
value: function handleSingleCheck(index, value) {
if (!value) {
this.setState({ all: value });
}
var currentState = this.state.checked;
currentState[index].checked = value;
this.setState({
checked: currentState
});
}
}, {
key: "handleChange",
value: function handleChange(value) {
this.setState({
all: !this.state.all,
checked: this.props.children.map(function () {
return {
checked: value
};
})
});
this.props.onChange ? this.props.onChange(value) : null; //eslint-disable-line
}
}, {
key: "render",
value: function render() {
var _this = this;
var children = [];
_react2["default"].Children.forEach(this.props.children, function (child, i) {
var childrenClasses = (0, _classnames2["default"])({
highlight: _this.state.checked[i].checked && _this.props.highlight
});
child.props.checked = "true";
children.push(_react2["default"].cloneElement(child, { checked: _this.state.checked[i].checked, inline: _this.props.inline, key: i, labelClassName: (0, _classnames2["default"])(_this.props.className, childrenClasses), onChange: _this.handleSingleCheck.bind(_this, i) }));
});
var checkAllBox = _react2["default"].createElement(
_CheckBox2["default"],
{ checked: this.state.all, className: this.props.className, disabled: this.props.disabled, id: "all", inline: this.props.inline, onChange: this.handleChange.bind(this), ref: "checkAll", style: this.props.style, value: this.props.value },
this.props.label2 ? this.state.all ? this.props.label2 : this.props.label : this.props.label
);
return _react2["default"].createElement(
"span",
null,
!this.props.bottom ? { checkAllBox: checkAllBox } : null,
children,
this.props.bottom ? { checkAllBox: checkAllBox } : null
);
}
}]);
return CheckAll;
})(_react2["default"].Component);
CheckAll.propTypes = {
bottom: _react2["default"].PropTypes.bool,
children: _react2["default"].PropTypes.node.isRequired,
className: _react2["default"].PropTypes.string,
disabled: _react2["default"].PropTypes.bool,
highlight: _react2["default"].PropTypes.bool,
id: _react2["default"].PropTypes.string,
inline: _react2["default"].PropTypes.bool,
label: _react2["default"].PropTypes.string,
label2: _react2["default"].PropTypes.string,
onChange: _react2["default"].PropTypes.func,
style: _react2["default"].PropTypes.object,
value: _react2["default"].PropTypes.oneOfType([_react2["default"].PropTypes.string, _react2["default"].PropTypes.number])
};
CheckAll.defaultProps = { bottom: false, checked: false, disabled: false, value: "all" };
module.exports = CheckAll;