Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

RaisedButton.js 16 KiB

il y a 3 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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 _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
  8. var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
  9. var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
  10. var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
  11. var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
  12. var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
  13. var _createClass2 = require('babel-runtime/helpers/createClass');
  14. var _createClass3 = _interopRequireDefault(_createClass2);
  15. var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
  16. var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
  17. var _inherits2 = require('babel-runtime/helpers/inherits');
  18. var _inherits3 = _interopRequireDefault(_inherits2);
  19. var _simpleAssign = require('simple-assign');
  20. var _simpleAssign2 = _interopRequireDefault(_simpleAssign);
  21. var _react = require('react');
  22. var _react2 = _interopRequireDefault(_react);
  23. var _propTypes = require('prop-types');
  24. var _propTypes2 = _interopRequireDefault(_propTypes);
  25. var _transitions = require('../styles/transitions');
  26. var _transitions2 = _interopRequireDefault(_transitions);
  27. var _colorManipulator = require('../utils/colorManipulator');
  28. var _EnhancedButton = require('../internal/EnhancedButton');
  29. var _EnhancedButton2 = _interopRequireDefault(_EnhancedButton);
  30. var _Paper = require('../Paper');
  31. var _Paper2 = _interopRequireDefault(_Paper);
  32. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  33. function validateLabel(props, propName, componentName) {
  34. if (process.env.NODE_ENV !== 'production') {
  35. if (!props.children && props.label !== 0 && !props.label && !props.icon) {
  36. return new Error('Required prop label or children or icon was not specified in ' + componentName + '.');
  37. }
  38. }
  39. }
  40. function getStyles(props, context, state) {
  41. var _context$muiTheme = context.muiTheme,
  42. baseTheme = _context$muiTheme.baseTheme,
  43. button = _context$muiTheme.button,
  44. raisedButton = _context$muiTheme.raisedButton,
  45. borderRadius = _context$muiTheme.borderRadius;
  46. var disabled = props.disabled,
  47. disabledBackgroundColor = props.disabledBackgroundColor,
  48. disabledLabelColor = props.disabledLabelColor,
  49. fullWidth = props.fullWidth,
  50. icon = props.icon,
  51. label = props.label,
  52. labelPosition = props.labelPosition,
  53. primary = props.primary,
  54. secondary = props.secondary,
  55. style = props.style;
  56. var amount = primary || secondary ? 0.4 : 0.08;
  57. var backgroundColor = raisedButton.color;
  58. var labelColor = raisedButton.textColor;
  59. if (disabled) {
  60. backgroundColor = disabledBackgroundColor || raisedButton.disabledColor;
  61. labelColor = disabledLabelColor || raisedButton.disabledTextColor;
  62. } else if (primary) {
  63. backgroundColor = raisedButton.primaryColor;
  64. labelColor = raisedButton.primaryTextColor;
  65. } else if (secondary) {
  66. backgroundColor = raisedButton.secondaryColor;
  67. labelColor = raisedButton.secondaryTextColor;
  68. } else {
  69. if (props.backgroundColor) {
  70. backgroundColor = props.backgroundColor;
  71. }
  72. if (props.labelColor) {
  73. labelColor = props.labelColor;
  74. }
  75. }
  76. var buttonHeight = style && style.height || button.height;
  77. return {
  78. root: {
  79. display: 'inline-block',
  80. transition: _transitions2.default.easeOut(),
  81. minWidth: fullWidth ? '100%' : button.minWidth
  82. },
  83. button: {
  84. height: buttonHeight,
  85. lineHeight: buttonHeight + 'px',
  86. width: '100%',
  87. padding: 0,
  88. borderRadius: borderRadius,
  89. transition: _transitions2.default.easeOut(),
  90. backgroundColor: backgroundColor,
  91. // That's the default value for a button but not a link
  92. textAlign: 'center'
  93. },
  94. label: {
  95. position: 'relative',
  96. opacity: 1,
  97. fontSize: raisedButton.fontSize,
  98. letterSpacing: 0,
  99. textTransform: raisedButton.textTransform || button.textTransform || 'uppercase',
  100. fontWeight: raisedButton.fontWeight,
  101. margin: 0,
  102. userSelect: 'none',
  103. paddingLeft: icon && labelPosition !== 'before' ? 8 : baseTheme.spacing.desktopGutterLess,
  104. paddingRight: icon && labelPosition === 'before' ? 8 : baseTheme.spacing.desktopGutterLess,
  105. color: labelColor
  106. },
  107. icon: {
  108. verticalAlign: 'middle',
  109. marginLeft: label && labelPosition !== 'before' ? 12 : 0,
  110. marginRight: label && labelPosition === 'before' ? 12 : 0
  111. },
  112. overlay: {
  113. height: buttonHeight,
  114. borderRadius: borderRadius,
  115. backgroundColor: (state.keyboardFocused || state.hovered) && !disabled && (0, _colorManipulator.fade)(labelColor, amount),
  116. transition: _transitions2.default.easeOut(),
  117. top: 0
  118. },
  119. ripple: {
  120. color: labelColor,
  121. opacity: !(primary || secondary) ? 0.1 : 0.16
  122. }
  123. };
  124. }
  125. var RaisedButton = function (_Component) {
  126. (0, _inherits3.default)(RaisedButton, _Component);
  127. function RaisedButton() {
  128. var _ref;
  129. var _temp, _this, _ret;
  130. (0, _classCallCheck3.default)(this, RaisedButton);
  131. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  132. args[_key] = arguments[_key];
  133. }
  134. return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = RaisedButton.__proto__ || (0, _getPrototypeOf2.default)(RaisedButton)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
  135. hovered: false,
  136. keyboardFocused: false,
  137. touched: false,
  138. initialZDepth: 0,
  139. zDepth: 0
  140. }, _this.handleMouseDown = function (event) {
  141. // only listen to left clicks
  142. if (event.button === 0) {
  143. _this.setState({
  144. zDepth: _this.state.initialZDepth + 1
  145. });
  146. }
  147. if (_this.props.onMouseDown) {
  148. _this.props.onMouseDown(event);
  149. }
  150. }, _this.handleMouseUp = function (event) {
  151. _this.setState({
  152. zDepth: _this.state.initialZDepth
  153. });
  154. if (_this.props.onMouseUp) {
  155. _this.props.onMouseUp(event);
  156. }
  157. }, _this.handleMouseLeave = function (event) {
  158. if (!_this.state.keyboardFocused) {
  159. _this.setState({
  160. zDepth: _this.state.initialZDepth,
  161. hovered: false
  162. });
  163. }
  164. if (_this.props.onMouseLeave) {
  165. _this.props.onMouseLeave(event);
  166. }
  167. }, _this.handleMouseEnter = function (event) {
  168. if (!_this.state.keyboardFocused && !_this.state.touched) {
  169. _this.setState({
  170. hovered: true
  171. });
  172. }
  173. if (_this.props.onMouseEnter) {
  174. _this.props.onMouseEnter(event);
  175. }
  176. }, _this.handleTouchStart = function (event) {
  177. _this.setState({
  178. touched: true,
  179. zDepth: _this.state.initialZDepth + 1
  180. });
  181. if (_this.props.onTouchStart) {
  182. _this.props.onTouchStart(event);
  183. }
  184. }, _this.handleTouchEnd = function (event) {
  185. _this.setState({
  186. touched: true,
  187. zDepth: _this.state.initialZDepth
  188. });
  189. if (_this.props.onTouchEnd) {
  190. _this.props.onTouchEnd(event);
  191. }
  192. }, _this.handleKeyboardFocus = function (event, keyboardFocused) {
  193. var zDepth = keyboardFocused && !_this.props.disabled ? _this.state.initialZDepth + 1 : _this.state.initialZDepth;
  194. _this.setState({
  195. zDepth: zDepth,
  196. keyboardFocused: keyboardFocused
  197. });
  198. }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  199. }
  200. (0, _createClass3.default)(RaisedButton, [{
  201. key: 'componentWillMount',
  202. value: function componentWillMount() {
  203. var zDepth = this.props.disabled ? 0 : 1;
  204. this.setState({
  205. zDepth: zDepth,
  206. initialZDepth: zDepth
  207. });
  208. }
  209. }, {
  210. key: 'componentWillReceiveProps',
  211. value: function componentWillReceiveProps(nextProps) {
  212. var zDepth = nextProps.disabled ? 0 : 1;
  213. var nextState = {
  214. zDepth: zDepth,
  215. initialZDepth: zDepth
  216. };
  217. if (nextProps.disabled) {
  218. nextState.hovered = false;
  219. }
  220. this.setState(nextState);
  221. }
  222. }, {
  223. key: 'render',
  224. value: function render() {
  225. var _props = this.props,
  226. backgroundColor = _props.backgroundColor,
  227. buttonStyle = _props.buttonStyle,
  228. children = _props.children,
  229. className = _props.className,
  230. disabled = _props.disabled,
  231. disabledBackgroundColor = _props.disabledBackgroundColor,
  232. disabledLabelColor = _props.disabledLabelColor,
  233. fullWidth = _props.fullWidth,
  234. icon = _props.icon,
  235. label = _props.label,
  236. labelColor = _props.labelColor,
  237. labelPosition = _props.labelPosition,
  238. labelStyle = _props.labelStyle,
  239. overlayStyle = _props.overlayStyle,
  240. primary = _props.primary,
  241. rippleStyle = _props.rippleStyle,
  242. secondary = _props.secondary,
  243. style = _props.style,
  244. other = (0, _objectWithoutProperties3.default)(_props, ['backgroundColor', 'buttonStyle', 'children', 'className', 'disabled', 'disabledBackgroundColor', 'disabledLabelColor', 'fullWidth', 'icon', 'label', 'labelColor', 'labelPosition', 'labelStyle', 'overlayStyle', 'primary', 'rippleStyle', 'secondary', 'style']);
  245. var prepareStyles = this.context.muiTheme.prepareStyles;
  246. var styles = getStyles(this.props, this.context, this.state);
  247. var mergedRippleStyles = (0, _simpleAssign2.default)({}, styles.ripple, rippleStyle);
  248. var buttonEventHandlers = disabled ? {} : {
  249. onMouseDown: this.handleMouseDown,
  250. onMouseUp: this.handleMouseUp,
  251. onMouseLeave: this.handleMouseLeave,
  252. onMouseEnter: this.handleMouseEnter,
  253. onTouchStart: this.handleTouchStart,
  254. onTouchEnd: this.handleTouchEnd,
  255. onKeyboardFocus: this.handleKeyboardFocus
  256. };
  257. var labelElement = label && _react2.default.createElement(
  258. 'span',
  259. { style: prepareStyles((0, _simpleAssign2.default)(styles.label, labelStyle)), key: 'labelElement' },
  260. label
  261. );
  262. var iconCloned = icon && (0, _react.cloneElement)(icon, {
  263. color: icon.props.color || styles.label.color,
  264. style: (0, _simpleAssign2.default)(styles.icon, icon.props.style),
  265. key: 'iconCloned'
  266. });
  267. var overlayBackgroundProxy = {
  268. backgroundColor: overlayStyle && styles.overlay.backgroundColor && overlayStyle.backgroundColor || styles.overlay.backgroundColor
  269. };
  270. // Place label before or after children.
  271. var enhancedButtonChildren = labelPosition === 'before' ? [labelElement, iconCloned, children] : [children, iconCloned, labelElement];
  272. return _react2.default.createElement(
  273. _Paper2.default,
  274. {
  275. className: className,
  276. style: (0, _simpleAssign2.default)(styles.root, style),
  277. zDepth: this.state.zDepth
  278. },
  279. _react2.default.createElement(
  280. _EnhancedButton2.default,
  281. (0, _extends3.default)({}, other, buttonEventHandlers, {
  282. ref: 'container',
  283. disabled: disabled,
  284. style: (0, _simpleAssign2.default)(styles.button, buttonStyle),
  285. focusRippleColor: mergedRippleStyles.color,
  286. touchRippleColor: mergedRippleStyles.color,
  287. focusRippleOpacity: mergedRippleStyles.opacity,
  288. touchRippleOpacity: mergedRippleStyles.opacity
  289. }),
  290. _react2.default.createElement(
  291. 'div',
  292. {
  293. ref: 'overlay',
  294. style: prepareStyles((0, _simpleAssign2.default)(styles.overlay, overlayStyle, overlayBackgroundProxy))
  295. },
  296. enhancedButtonChildren
  297. )
  298. )
  299. );
  300. }
  301. }]);
  302. return RaisedButton;
  303. }(_react.Component);
  304. RaisedButton.muiName = 'RaisedButton';
  305. RaisedButton.defaultProps = {
  306. disabled: false,
  307. labelPosition: 'after',
  308. fullWidth: false,
  309. primary: false,
  310. secondary: false
  311. };
  312. RaisedButton.contextTypes = {
  313. muiTheme: _propTypes2.default.object.isRequired
  314. };
  315. RaisedButton.propTypes = process.env.NODE_ENV !== "production" ? {
  316. /**
  317. * Override the default background color for the button,
  318. * but not the default disabled background color
  319. * (use `disabledBackgroundColor` for this).
  320. */
  321. backgroundColor: _propTypes2.default.string,
  322. /**
  323. * Override the inline-styles of the button element.
  324. */
  325. buttonStyle: _propTypes2.default.object,
  326. /**
  327. * The content of the button.
  328. * If a label is provided via the `label` prop, the text within the label
  329. * will be displayed in addition to the content provided here.
  330. */
  331. children: _propTypes2.default.node,
  332. /**
  333. * The CSS class name of the root element.
  334. */
  335. className: _propTypes2.default.string,
  336. /**
  337. * The element to use as the container for the RaisedButton. Either a string to
  338. * use a DOM element or a ReactElement. This is useful for wrapping the
  339. * RaisedButton in a custom Link component. If a ReactElement is given, ensure
  340. * that it passes all of its given props through to the underlying DOM
  341. * element and renders its children prop for proper integration.
  342. */
  343. containerElement: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.element]),
  344. /**
  345. * If true, the element's ripple effect will be disabled.
  346. */
  347. disableTouchRipple: _propTypes2.default.bool,
  348. /**
  349. * If true, the button will be disabled.
  350. */
  351. disabled: _propTypes2.default.bool,
  352. /**
  353. * Override the default background color for the button
  354. * when it is disabled.
  355. */
  356. disabledBackgroundColor: _propTypes2.default.string,
  357. /**
  358. * The color of the button's label when the button is disabled.
  359. */
  360. disabledLabelColor: _propTypes2.default.string,
  361. /**
  362. * If true, the button will take up the full width of its container.
  363. */
  364. fullWidth: _propTypes2.default.bool,
  365. /**
  366. * The URL to link to when the button is clicked.
  367. */
  368. href: _propTypes2.default.string,
  369. /**
  370. * An icon to be displayed within the button.
  371. */
  372. icon: _propTypes2.default.node,
  373. /**
  374. * The label to be displayed within the button.
  375. * If content is provided via the `children` prop, that content will be
  376. * displayed in addition to the label provided here.
  377. */
  378. label: validateLabel,
  379. /**
  380. * The color of the button's label.
  381. */
  382. labelColor: _propTypes2.default.string,
  383. /**
  384. * The position of the button's label relative to the button's `children`.
  385. */
  386. labelPosition: _propTypes2.default.oneOf(['before', 'after']),
  387. /**
  388. * Override the inline-styles of the button's label element.
  389. */
  390. labelStyle: _propTypes2.default.object,
  391. /**
  392. * Callback function fired when the button is clicked.
  393. *
  394. * @param {object} event Click event targeting the button.
  395. */
  396. onClick: _propTypes2.default.func,
  397. /** @ignore */
  398. onMouseDown: _propTypes2.default.func,
  399. /** @ignore */
  400. onMouseEnter: _propTypes2.default.func,
  401. /** @ignore */
  402. onMouseLeave: _propTypes2.default.func,
  403. /** @ignore */
  404. onMouseUp: _propTypes2.default.func,
  405. /** @ignore */
  406. onTouchEnd: _propTypes2.default.func,
  407. /** @ignore */
  408. onTouchStart: _propTypes2.default.func,
  409. /**
  410. * Override the inline style of the button overlay.
  411. */
  412. overlayStyle: _propTypes2.default.object,
  413. /**
  414. * If true, the button will use the theme's primary color.
  415. */
  416. primary: _propTypes2.default.bool,
  417. /**
  418. * Override the inline style of the ripple element.
  419. */
  420. rippleStyle: _propTypes2.default.object,
  421. /**
  422. * If true, the button will use the theme's secondary color.
  423. * If both `secondary` and `primary` are true, the button will use
  424. * the theme's primary color.
  425. */
  426. secondary: _propTypes2.default.bool,
  427. /**
  428. * Override the inline-styles of the root element.
  429. */
  430. style: _propTypes2.default.object
  431. } : {};
  432. exports.default = RaisedButton;