Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

AppBar.js 13 KiB

3 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _extends2 = require('babel-runtime/helpers/extends');
  6. var _extends3 = _interopRequireDefault(_extends2);
  7. var _keys = require('babel-runtime/core-js/object/keys');
  8. var _keys2 = _interopRequireDefault(_keys);
  9. var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
  10. var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
  11. var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
  12. var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
  13. var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
  14. var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
  15. var _createClass2 = require('babel-runtime/helpers/createClass');
  16. var _createClass3 = _interopRequireDefault(_createClass2);
  17. var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
  18. var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
  19. var _inherits2 = require('babel-runtime/helpers/inherits');
  20. var _inherits3 = _interopRequireDefault(_inherits2);
  21. exports.getStyles = getStyles;
  22. var _simpleAssign = require('simple-assign');
  23. var _simpleAssign2 = _interopRequireDefault(_simpleAssign);
  24. var _react = require('react');
  25. var _react2 = _interopRequireDefault(_react);
  26. var _propTypes = require('prop-types');
  27. var _propTypes2 = _interopRequireDefault(_propTypes);
  28. var _IconButton = require('../IconButton');
  29. var _IconButton2 = _interopRequireDefault(_IconButton);
  30. var _menu = require('../svg-icons/navigation/menu');
  31. var _menu2 = _interopRequireDefault(_menu);
  32. var _Paper = require('../Paper');
  33. var _Paper2 = _interopRequireDefault(_Paper);
  34. var _propTypes3 = require('../utils/propTypes');
  35. var _propTypes4 = _interopRequireDefault(_propTypes3);
  36. var _warning = require('warning');
  37. var _warning2 = _interopRequireDefault(_warning);
  38. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  39. function getStyles(props, context) {
  40. var _context$muiTheme = context.muiTheme,
  41. appBar = _context$muiTheme.appBar,
  42. iconButtonSize = _context$muiTheme.button.iconButtonSize,
  43. zIndex = _context$muiTheme.zIndex;
  44. var flatButtonSize = 36;
  45. var styles = {
  46. root: {
  47. position: 'relative',
  48. zIndex: zIndex.appBar,
  49. width: '100%',
  50. display: 'flex',
  51. backgroundColor: appBar.color,
  52. paddingLeft: appBar.padding,
  53. paddingRight: appBar.padding
  54. },
  55. title: {
  56. whiteSpace: 'nowrap',
  57. overflow: 'hidden',
  58. textOverflow: 'ellipsis',
  59. margin: 0,
  60. paddingTop: 0,
  61. letterSpacing: 0,
  62. fontSize: 24,
  63. fontWeight: appBar.titleFontWeight,
  64. color: appBar.textColor,
  65. height: appBar.height,
  66. lineHeight: appBar.height + 'px'
  67. },
  68. mainElement: {
  69. boxFlex: 1,
  70. flex: '1'
  71. },
  72. iconButtonStyle: {
  73. marginTop: (appBar.height - iconButtonSize) / 2,
  74. marginRight: 8,
  75. marginLeft: -16
  76. },
  77. iconButtonIconStyle: {
  78. fill: appBar.textColor,
  79. color: appBar.textColor
  80. },
  81. flatButton: {
  82. color: appBar.textColor,
  83. marginTop: (iconButtonSize - flatButtonSize) / 2 + 1
  84. }
  85. };
  86. return styles;
  87. }
  88. var AppBar = function (_Component) {
  89. (0, _inherits3.default)(AppBar, _Component);
  90. function AppBar() {
  91. var _ref;
  92. var _temp, _this, _ret;
  93. (0, _classCallCheck3.default)(this, AppBar);
  94. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  95. args[_key] = arguments[_key];
  96. }
  97. return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = AppBar.__proto__ || (0, _getPrototypeOf2.default)(AppBar)).call.apply(_ref, [this].concat(args))), _this), _this.handleClickLeftIconButton = function (event) {
  98. if (_this.props.onLeftIconButtonClick) {
  99. _this.props.onLeftIconButtonClick(event);
  100. }
  101. }, _this.handleClickRightIconButton = function (event) {
  102. if (_this.props.onRightIconButtonClick) {
  103. _this.props.onRightIconButtonClick(event);
  104. }
  105. }, _this.handleTitleClick = function (event) {
  106. if (_this.props.onTitleClick) {
  107. _this.props.onTitleClick(event);
  108. }
  109. }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  110. }
  111. (0, _createClass3.default)(AppBar, [{
  112. key: 'componentDidMount',
  113. value: function componentDidMount() {
  114. process.env.NODE_ENV !== "production" ? (0, _warning2.default)(!this.props.iconElementLeft || !this.props.iconClassNameLeft, 'Material-UI: Properties iconElementLeft\n and iconClassNameLeft cannot be simultaneously defined. Please use one or the other.') : void 0;
  115. process.env.NODE_ENV !== "production" ? (0, _warning2.default)(!this.props.iconElementRight || !this.props.iconClassNameRight, 'Material-UI: Properties iconElementRight\n and iconClassNameRight cannot be simultaneously defined. Please use one or the other.') : void 0;
  116. }
  117. }, {
  118. key: 'render',
  119. value: function render() {
  120. var _props = this.props,
  121. title = _props.title,
  122. titleStyle = _props.titleStyle,
  123. iconStyleLeft = _props.iconStyleLeft,
  124. iconStyleRight = _props.iconStyleRight,
  125. onTitleClick = _props.onTitleClick,
  126. showMenuIconButton = _props.showMenuIconButton,
  127. iconElementLeft = _props.iconElementLeft,
  128. iconElementRight = _props.iconElementRight,
  129. iconClassNameLeft = _props.iconClassNameLeft,
  130. iconClassNameRight = _props.iconClassNameRight,
  131. onLeftIconButtonClick = _props.onLeftIconButtonClick,
  132. onRightIconButtonClick = _props.onRightIconButtonClick,
  133. className = _props.className,
  134. style = _props.style,
  135. zDepth = _props.zDepth,
  136. children = _props.children,
  137. other = (0, _objectWithoutProperties3.default)(_props, ['title', 'titleStyle', 'iconStyleLeft', 'iconStyleRight', 'onTitleClick', 'showMenuIconButton', 'iconElementLeft', 'iconElementRight', 'iconClassNameLeft', 'iconClassNameRight', 'onLeftIconButtonClick', 'onRightIconButtonClick', 'className', 'style', 'zDepth', 'children']);
  138. var prepareStyles = this.context.muiTheme.prepareStyles;
  139. var styles = getStyles(this.props, this.context);
  140. var menuElementLeft = void 0;
  141. var menuElementRight = void 0;
  142. // If the title is a string, wrap in an h1 tag.
  143. // If not, wrap in a div tag.
  144. var titleComponent = typeof title === 'string' || title instanceof String ? 'h1' : 'div';
  145. var titleElement = _react2.default.createElement(titleComponent, {
  146. onClick: this.handleTitleClick,
  147. style: prepareStyles((0, _simpleAssign2.default)(styles.title, styles.mainElement, titleStyle))
  148. }, title);
  149. var iconLeftStyle = (0, _simpleAssign2.default)({}, styles.iconButtonStyle, iconStyleLeft);
  150. if (showMenuIconButton) {
  151. if (iconElementLeft) {
  152. var iconElementLeftProps = {};
  153. if (iconElementLeft.type.muiName === 'IconButton') {
  154. var iconElemLeftChildren = iconElementLeft.props.children;
  155. var iconButtonIconStyle = !(iconElemLeftChildren && iconElemLeftChildren.props && iconElemLeftChildren.props.color) ? styles.iconButtonIconStyle : null;
  156. iconElementLeftProps.iconStyle = (0, _simpleAssign2.default)({}, iconButtonIconStyle, iconElementLeft.props.iconStyle);
  157. }
  158. if (!iconElementLeft.props.onClick && this.props.onLeftIconButtonClick) {
  159. iconElementLeftProps.onClick = this.handleClickLeftIconButton;
  160. }
  161. menuElementLeft = _react2.default.createElement(
  162. 'div',
  163. { style: prepareStyles(iconLeftStyle) },
  164. (0, _keys2.default)(iconElementLeftProps).length > 0 ? (0, _react.cloneElement)(iconElementLeft, iconElementLeftProps) : iconElementLeft
  165. );
  166. } else {
  167. menuElementLeft = _react2.default.createElement(
  168. _IconButton2.default,
  169. {
  170. style: iconLeftStyle,
  171. iconStyle: styles.iconButtonIconStyle,
  172. iconClassName: iconClassNameLeft,
  173. onClick: this.handleClickLeftIconButton
  174. },
  175. iconClassNameLeft ? '' : _react2.default.createElement(_menu2.default, { style: (0, _simpleAssign2.default)({}, styles.iconButtonIconStyle) })
  176. );
  177. }
  178. }
  179. var iconRightStyle = (0, _simpleAssign2.default)({}, styles.iconButtonStyle, {
  180. marginRight: -16,
  181. marginLeft: 'auto'
  182. }, iconStyleRight);
  183. if (iconElementRight) {
  184. var iconElementRightProps = {};
  185. switch (iconElementRight.type.muiName) {
  186. case 'IconMenu':
  187. case 'IconButton':
  188. var iconElemRightChildren = iconElementRight.props.children;
  189. var _iconButtonIconStyle = !(iconElemRightChildren && iconElemRightChildren.props && iconElemRightChildren.props.color) ? styles.iconButtonIconStyle : null;
  190. iconElementRightProps.iconStyle = (0, _simpleAssign2.default)({}, _iconButtonIconStyle, iconElementRight.props.iconStyle);
  191. break;
  192. case 'FlatButton':
  193. iconElementRightProps.style = (0, _simpleAssign2.default)({}, styles.flatButton, iconElementRight.props.style);
  194. break;
  195. default:
  196. }
  197. if (!iconElementRight.props.onClick && this.props.onRightIconButtonClick) {
  198. iconElementRightProps.onClick = this.handleClickRightIconButton;
  199. }
  200. menuElementRight = _react2.default.createElement(
  201. 'div',
  202. { style: prepareStyles(iconRightStyle) },
  203. (0, _keys2.default)(iconElementRightProps).length > 0 ? (0, _react.cloneElement)(iconElementRight, iconElementRightProps) : iconElementRight
  204. );
  205. } else if (iconClassNameRight) {
  206. menuElementRight = _react2.default.createElement(_IconButton2.default, {
  207. style: iconRightStyle,
  208. iconStyle: styles.iconButtonIconStyle,
  209. iconClassName: iconClassNameRight,
  210. onClick: this.handleClickRightIconButton
  211. });
  212. }
  213. return _react2.default.createElement(
  214. _Paper2.default,
  215. (0, _extends3.default)({}, other, {
  216. rounded: false,
  217. className: className,
  218. style: (0, _simpleAssign2.default)({}, styles.root, style),
  219. zDepth: zDepth
  220. }),
  221. menuElementLeft,
  222. titleElement,
  223. menuElementRight,
  224. children
  225. );
  226. }
  227. }]);
  228. return AppBar;
  229. }(_react.Component);
  230. AppBar.muiName = 'AppBar';
  231. AppBar.defaultProps = {
  232. showMenuIconButton: true,
  233. title: '',
  234. zDepth: 1
  235. };
  236. AppBar.contextTypes = {
  237. muiTheme: _propTypes2.default.object.isRequired
  238. };
  239. AppBar.propTypes = process.env.NODE_ENV !== "production" ? {
  240. /**
  241. * Can be used to render a tab inside an app bar for instance.
  242. */
  243. children: _propTypes2.default.node,
  244. /**
  245. * Applied to the app bar's root element.
  246. */
  247. className: _propTypes2.default.string,
  248. /**
  249. * The classname of the icon on the left of the app bar.
  250. * If you are using a stylesheet for your icons, enter the class name for the icon to be used here.
  251. */
  252. iconClassNameLeft: _propTypes2.default.string,
  253. /**
  254. * Similiar to the iconClassNameLeft prop except that
  255. * it applies to the icon displayed on the right of the app bar.
  256. */
  257. iconClassNameRight: _propTypes2.default.string,
  258. /**
  259. * The custom element to be displayed on the left side of the
  260. * app bar such as an SvgIcon.
  261. */
  262. iconElementLeft: _propTypes2.default.element,
  263. /**
  264. * Similiar to the iconElementLeft prop except that this element is displayed on the right of the app bar.
  265. */
  266. iconElementRight: _propTypes2.default.element,
  267. /**
  268. * Override the inline-styles of the element displayed on the left side of the app bar.
  269. */
  270. iconStyleLeft: _propTypes2.default.object,
  271. /**
  272. * Override the inline-styles of the element displayed on the right side of the app bar.
  273. */
  274. iconStyleRight: _propTypes2.default.object,
  275. /**
  276. * Callback function for when the left icon is selected via a click.
  277. *
  278. * @param {object} event Click event targeting the left `IconButton`.
  279. */
  280. onLeftIconButtonClick: _propTypes2.default.func,
  281. /**
  282. * Callback function for when the right icon is selected via a click.
  283. *
  284. * @param {object} event Click event targeting the right `IconButton`.
  285. */
  286. onRightIconButtonClick: _propTypes2.default.func,
  287. /**
  288. * Callback function for when the title text is selected via a click.
  289. *
  290. * @param {object} event Click event targeting the `title` node.
  291. */
  292. onTitleClick: _propTypes2.default.func,
  293. /**
  294. * Determines whether or not to display the Menu icon next to the title.
  295. * Setting this prop to false will hide the icon.
  296. */
  297. showMenuIconButton: _propTypes2.default.bool,
  298. /**
  299. * Override the inline-styles of the root element.
  300. */
  301. style: _propTypes2.default.object,
  302. /**
  303. * The title to display on the app bar.
  304. */
  305. title: _propTypes2.default.node,
  306. /**
  307. * Override the inline-styles of the app bar's title element.
  308. */
  309. titleStyle: _propTypes2.default.object,
  310. /**
  311. * The zDepth of the component.
  312. * The shadow of the app bar is also dependent on this property.
  313. */
  314. zDepth: _propTypes4.default.zDepth
  315. } : {};
  316. exports.default = AppBar;