UNPKG

backpack-ui

Version:
138 lines (103 loc) 3.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var _getPrototypeOf = require("babel-runtime/core-js/object/get-prototype-of"); var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); 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 _react2 = _interopRequireDefault(_react); var _propTypes = require("prop-types"); var _propTypes2 = _interopRequireDefault(_propTypes); var _radium = require("radium"); var _radium2 = _interopRequireDefault(_radium); var _tag = require("../tag"); var _tag2 = _interopRequireDefault(_tag); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var rowHeight = 40; var styles = { container: { overflow: "hidden" }, tag: { marginBottom: "8px", marginRight: "8px" } }; var TagList = function (_React$Component) { (0, _inherits3.default)(TagList, _React$Component); function TagList(props) { (0, _classCallCheck3.default)(this, TagList); var _this = (0, _possibleConstructorReturn3.default)(this, (TagList.__proto__ || (0, _getPrototypeOf2.default)(TagList)).call(this, props)); _this.state = { expanded: false }; _this.expand = _this.expand.bind(_this); _this.shouldShowExpand = _this.shouldShowExpand.bind(_this); return _this; } (0, _createClass3.default)(TagList, [{ key: "expand", value: function expand() { this.setState({ expanded: true }); } }, { key: "shouldShowExpand", value: function shouldShowExpand() { return this.props.children.length - this.props.limit > 0 && !this.state.expanded; } }, { key: "render", value: function render() { var _props = this.props, children = _props.children, rows = _props.rows, style = _props.style; return _react2.default.createElement( "div", { className: "TagList", style: [styles.container, { maxHeight: rowHeight * rows + "px" }, style] }, _react2.default.Children.map(children, function (child, index) { return _react2.default.cloneElement(child, { key: index, style: [styles.tag, child.props.style] }); }).slice(0, this.state.expanded ? this.props.children.length : this.props.limit), this.shouldShowExpand() && _react2.default.createElement( _tag2.default, { onClick: this.expand }, "+" + (this.props.children.length - this.props.limit) ) ); } }]); return TagList; }(_react2.default.Component); TagList.propTypes = { children: function children(props, propName, componentName) { var prop = props[propName]; var error = null; _react2.default.Children.forEach(prop, function (child) { if (child.type !== _tag2.default) { error = new Error(componentName + " children should be of type \"Tag\"."); } }); return error; }, rows: _propTypes2.default.number, limit: _propTypes2.default.number, style: _propTypes2.default.objectOf(_propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number, _propTypes2.default.object])) }; TagList.defaultProps = { rows: 3 }; exports.default = (0, _radium2.default)(TagList);