UNPKG

d2-ui

Version:
191 lines (163 loc) 6.81 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); 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 _Avatar = require('../Avatar'); var _Avatar2 = _interopRequireDefault(_Avatar); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } 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; } function getStyles(props, context) { var card = context.muiTheme.card; return { root: { padding: 16, fontWeight: card.fontWeight, boxSizing: 'border-box', position: 'relative', whiteSpace: 'nowrap' }, text: { display: 'inline-block', verticalAlign: 'top', whiteSpace: 'normal', paddingRight: '90px' }, avatar: { marginRight: 16 }, title: { color: props.titleColor || card.titleColor, display: 'block', fontSize: 15 }, subtitle: { color: props.subtitleColor || card.subtitleColor, display: 'block', fontSize: 14 } }; } var CardHeader = function (_Component) { _inherits(CardHeader, _Component); function CardHeader() { _classCallCheck(this, CardHeader); return _possibleConstructorReturn(this, Object.getPrototypeOf(CardHeader).apply(this, arguments)); } _createClass(CardHeader, [{ key: 'render', value: function render() { var prepareStyles = this.context.muiTheme.prepareStyles; var styles = getStyles(this.props, this.context); var rootStyle = (0, _simpleAssign2.default)(styles.root, this.props.style); var textStyle = (0, _simpleAssign2.default)(styles.text, this.props.textStyle); var titleStyle = (0, _simpleAssign2.default)(styles.title, this.props.titleStyle); var subtitleStyle = (0, _simpleAssign2.default)(styles.subtitle, this.props.subtitleStyle); var avatar = this.props.avatar; if (_react2.default.isValidElement(this.props.avatar)) { avatar = _react2.default.cloneElement(avatar, { style: (0, _simpleAssign2.default)(styles.avatar, avatar.props.style) }); } else if (avatar !== null) { avatar = _react2.default.createElement(_Avatar2.default, { src: this.props.avatar, style: styles.avatar }); } var _props = this.props; var title = _props.title; var subtitle = _props.subtitle; var other = _objectWithoutProperties(_props, ['title', 'subtitle']); return _react2.default.createElement( 'div', _extends({}, other, { style: prepareStyles(rootStyle) }), avatar, _react2.default.createElement( 'div', { style: prepareStyles(textStyle) }, _react2.default.createElement( 'span', { style: prepareStyles(titleStyle) }, title ), _react2.default.createElement( 'span', { style: prepareStyles(subtitleStyle) }, subtitle ) ), this.props.children ); } }]); return CardHeader; }(_react.Component); CardHeader.muiName = 'CardHeader'; CardHeader.propTypes = { /** * If true, a click on this card component expands the card. */ actAsExpander: _react.PropTypes.bool, /** * This is the [Avatar](/#/components/avatar) element to be displayed on the Card Header. * If `avatar` is an `Avatar` or other element, it will be rendered. * If `avatar` is a string, it will be used as the image `src` for an `Avatar`. */ avatar: _react.PropTypes.node, /** * Can be used to render elements inside the Card Header. */ children: _react.PropTypes.node, /** * If true, this card component is expandable. */ expandable: _react.PropTypes.bool, /** * If true, this card component will include a button to expand the card. */ showExpandableButton: _react.PropTypes.bool, /** * Override the inline-styles of the root element. */ style: _react.PropTypes.object, /** * Can be used to render a subtitle in Card Header. */ subtitle: _react.PropTypes.node, /** * Override the subtitle color. */ subtitleColor: _react.PropTypes.string, /** * Override the inline-styles of the subtitle. */ subtitleStyle: _react.PropTypes.object, /** * Override the inline-styles of the text. */ textStyle: _react.PropTypes.object, /** * Can be used to render a title in Card Header. */ title: _react.PropTypes.node, /** * Override the title color. */ titleColor: _react.PropTypes.string, /** * Override the inline-styles of the title. */ titleStyle: _react.PropTypes.object }; CardHeader.defaultProps = { avatar: null }; CardHeader.contextTypes = { muiTheme: _react.PropTypes.object.isRequired }; exports.default = CardHeader;