UNPKG

d2-ui

Version:
164 lines (130 loc) 6.93 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.MakeSelectable = undefined; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; 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 _simpleAssign = require('simple-assign'); var _simpleAssign2 = _interopRequireDefault(_simpleAssign); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _colorManipulator = require('../utils/colorManipulator'); var _deprecatedPropType = require('../utils/deprecatedPropType'); var _deprecatedPropType2 = _interopRequireDefault(_deprecatedPropType); 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; } var MakeSelectable = exports.MakeSelectable = function MakeSelectable(Component) { var _class, _temp2; return _temp2 = _class = function (_Component) { _inherits(_class, _Component); function _class() { var _Object$getPrototypeO; var _temp, _this, _ret; _classCallCheck(this, _class); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_Object$getPrototypeO = Object.getPrototypeOf(_class)).call.apply(_Object$getPrototypeO, [this].concat(args))), _this), _this.hasSelectedDescendant = function (previousValue, child) { if (_react2.default.isValidElement(child) && child.props.nestedItems && child.props.nestedItems.length > 0) { return child.props.nestedItems.reduce(_this.hasSelectedDescendant, previousValue); } return previousValue || _this.isChildSelected(child, _this.props); }, _this.handleItemTouchTap = function (event, item) { var valueLink = _this.getValueLink(_this.props); var itemValue = item.props.value; if (itemValue !== valueLink.value) { valueLink.requestChange(event, itemValue); } }, _temp), _possibleConstructorReturn(_this, _ret); } _createClass(_class, [{ key: 'getValueLink', value: function getValueLink(props) { return props.valueLink || { value: props.value, requestChange: props.onChange }; } }, { key: 'extendChild', value: function extendChild(child, styles, selectedItemStyle) { var _this2 = this; if (child && child.type && child.type.muiName === 'ListItem') { var selected = this.isChildSelected(child, this.props); var selectedChildrenStyles = void 0; if (selected) { selectedChildrenStyles = (0, _simpleAssign2.default)({}, styles, selectedItemStyle); } var mergedChildrenStyles = (0, _simpleAssign2.default)({}, child.props.style, selectedChildrenStyles); this.keyIndex += 1; return _react2.default.cloneElement(child, { onTouchTap: function onTouchTap(event) { _this2.handleItemTouchTap(event, child); if (child.props.onTouchTap) { child.props.onTouchTap(event); } }, key: this.keyIndex, style: mergedChildrenStyles, nestedItems: child.props.nestedItems.map(function (child) { return _this2.extendChild(child, styles, selectedItemStyle); }), initiallyOpen: this.isInitiallyOpen(child) }); } else { return child; } } }, { key: 'isInitiallyOpen', value: function isInitiallyOpen(child) { if (child.props.initiallyOpen) { return child.props.initiallyOpen; } return this.hasSelectedDescendant(false, child); } }, { key: 'isChildSelected', value: function isChildSelected(child, props) { return this.getValueLink(props).value === child.props.value; } }, { key: 'render', value: function render() { var _this3 = this; var _props = this.props; var children = _props.children; var selectedItemStyle = _props.selectedItemStyle; this.keyIndex = 0; var styles = {}; if (!selectedItemStyle) { var textColor = this.context.muiTheme.baseTheme.palette.textColor; styles.backgroundColor = (0, _colorManipulator.fade)(textColor, 0.2); } return _react2.default.createElement( Component, _extends({}, this.props, this.state), _react2.default.Children.map(children, function (child) { return _this3.extendChild(child, styles, selectedItemStyle); }) ); } }]); return _class; }(Component), _class.propTypes = { children: _react.PropTypes.node, onChange: _react.PropTypes.func, selectedItemStyle: _react.PropTypes.object, value: _react.PropTypes.any, valueLink: (0, _deprecatedPropType2.default)(_react.PropTypes.shape({ value: _react.PropTypes.any, requestChange: _react.PropTypes.func }), 'This property is deprecated due to his low popularity. Use the value and onChange property.') }, _class.contextTypes = { muiTheme: _react.PropTypes.object.isRequired }, _temp2; }; exports.default = MakeSelectable;