Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

464 строки
15 KiB

  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 _reactEventListener = require('react-event-listener');
  26. var _reactEventListener2 = _interopRequireDefault(_reactEventListener);
  27. var _keycode = require('keycode');
  28. var _keycode2 = _interopRequireDefault(_keycode);
  29. var _transitions = require('../styles/transitions');
  30. var _transitions2 = _interopRequireDefault(_transitions);
  31. var _FocusRipple = require('./FocusRipple');
  32. var _FocusRipple2 = _interopRequireDefault(_FocusRipple);
  33. var _TouchRipple = require('./TouchRipple');
  34. var _TouchRipple2 = _interopRequireDefault(_TouchRipple);
  35. var _Paper = require('./../Paper');
  36. var _Paper2 = _interopRequireDefault(_Paper);
  37. var _warning = require('warning');
  38. var _warning2 = _interopRequireDefault(_warning);
  39. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  40. function getStyles(props, context) {
  41. var baseTheme = context.muiTheme.baseTheme;
  42. return {
  43. root: {
  44. cursor: props.disabled ? 'not-allowed' : 'pointer',
  45. position: 'relative',
  46. overflow: 'visible',
  47. display: 'table',
  48. height: 'auto',
  49. width: '100%'
  50. },
  51. input: {
  52. position: 'absolute',
  53. cursor: 'inherit',
  54. pointerEvents: 'all',
  55. opacity: 0,
  56. width: '100%',
  57. height: '100%',
  58. zIndex: 2,
  59. left: 0,
  60. boxSizing: 'border-box',
  61. padding: 0,
  62. margin: 0
  63. },
  64. controls: {
  65. display: 'flex',
  66. width: '100%',
  67. height: '100%'
  68. },
  69. label: {
  70. float: 'left',
  71. position: 'relative',
  72. display: 'block',
  73. width: 'calc(100% - 60px)',
  74. lineHeight: '24px',
  75. color: baseTheme.palette.textColor,
  76. fontFamily: baseTheme.fontFamily
  77. },
  78. wrap: {
  79. transition: _transitions2.default.easeOut(),
  80. float: 'left',
  81. position: 'relative',
  82. display: 'block',
  83. flexShrink: 0,
  84. width: 60 - baseTheme.spacing.desktopGutterLess,
  85. marginRight: props.labelPosition === 'right' ? baseTheme.spacing.desktopGutterLess : 0,
  86. marginLeft: props.labelPosition === 'left' ? baseTheme.spacing.desktopGutterLess : 0
  87. },
  88. ripple: {
  89. color: props.rippleColor || baseTheme.palette.primary1Color,
  90. height: '200%',
  91. width: '200%',
  92. top: -12,
  93. left: -12
  94. }
  95. };
  96. }
  97. var EnhancedSwitch = function (_Component) {
  98. (0, _inherits3.default)(EnhancedSwitch, _Component);
  99. function EnhancedSwitch() {
  100. var _ref;
  101. var _temp, _this, _ret;
  102. (0, _classCallCheck3.default)(this, EnhancedSwitch);
  103. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  104. args[_key] = arguments[_key];
  105. }
  106. return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = EnhancedSwitch.__proto__ || (0, _getPrototypeOf2.default)(EnhancedSwitch)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
  107. isKeyboardFocused: false
  108. }, _this.handleChange = function (event) {
  109. _this.tabPressed = false;
  110. _this.setState({
  111. isKeyboardFocused: false
  112. });
  113. var isInputChecked = _this.refs.checkbox.checked;
  114. if (!_this.props.hasOwnProperty('checked') && _this.props.onParentShouldUpdate) {
  115. _this.props.onParentShouldUpdate(isInputChecked);
  116. }
  117. if (_this.props.onSwitch) {
  118. _this.props.onSwitch(event, isInputChecked);
  119. }
  120. }, _this.handleKeyDown = function (event) {
  121. var code = (0, _keycode2.default)(event);
  122. if (code === 'tab') {
  123. _this.tabPressed = true;
  124. }
  125. if (_this.state.isKeyboardFocused && code === 'space') {
  126. _this.handleChange(event);
  127. }
  128. }, _this.handleKeyUp = function (event) {
  129. if (_this.state.isKeyboardFocused && (0, _keycode2.default)(event) === 'space') {
  130. _this.handleChange(event);
  131. }
  132. }, _this.handleMouseDown = function (event) {
  133. // only listen to left clicks
  134. if (event.button === 0) {
  135. _this.refs.touchRipple.start(event);
  136. }
  137. }, _this.handleMouseUp = function () {
  138. _this.refs.touchRipple.end();
  139. }, _this.handleMouseLeave = function () {
  140. _this.refs.touchRipple.end();
  141. }, _this.handleTouchStart = function (event) {
  142. _this.refs.touchRipple.start(event);
  143. }, _this.handleTouchEnd = function () {
  144. _this.refs.touchRipple.end();
  145. }, _this.handleBlur = function (event) {
  146. _this.setState({
  147. isKeyboardFocused: false
  148. });
  149. if (_this.props.onBlur) {
  150. _this.props.onBlur(event);
  151. }
  152. }, _this.handleFocus = function (event) {
  153. // setTimeout is needed becuase the focus event fires first
  154. // Wait so that we can capture if this was a keyboard focus
  155. // or touch focus
  156. setTimeout(function () {
  157. if (_this.tabPressed) {
  158. _this.setState({
  159. isKeyboardFocused: true
  160. });
  161. }
  162. }, 150);
  163. if (_this.props.onFocus) {
  164. _this.props.onFocus(event);
  165. }
  166. }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  167. }
  168. (0, _createClass3.default)(EnhancedSwitch, [{
  169. key: 'componentWillMount',
  170. value: function componentWillMount() {
  171. this.componentWillReceiveProps(this.props);
  172. }
  173. }, {
  174. key: 'componentDidMount',
  175. value: function componentDidMount() {
  176. var inputNode = this.refs.checkbox;
  177. if ((!this.props.switched || inputNode.checked !== this.props.switched) && this.props.onParentShouldUpdate) {
  178. this.props.onParentShouldUpdate(inputNode.checked);
  179. }
  180. }
  181. }, {
  182. key: 'componentWillReceiveProps',
  183. value: function componentWillReceiveProps(nextProps) {
  184. var hasCheckedProp = nextProps.hasOwnProperty('checked');
  185. var hasNewDefaultProp = nextProps.hasOwnProperty('defaultChecked') && nextProps.defaultChecked !== this.props.defaultChecked;
  186. if (hasCheckedProp || hasNewDefaultProp) {
  187. var switched = nextProps.checked || nextProps.defaultChecked || false;
  188. this.setState({
  189. switched: switched
  190. });
  191. if (this.props.onParentShouldUpdate && switched !== this.props.switched) {
  192. this.props.onParentShouldUpdate(switched);
  193. }
  194. }
  195. }
  196. }, {
  197. key: 'isSwitched',
  198. value: function isSwitched() {
  199. return this.refs.checkbox.checked;
  200. }
  201. // no callback here because there is no event
  202. }, {
  203. key: 'setSwitched',
  204. value: function setSwitched(newSwitchedValue) {
  205. if (!this.props.hasOwnProperty('checked') || this.props.checked === false) {
  206. if (this.props.onParentShouldUpdate) {
  207. this.props.onParentShouldUpdate(newSwitchedValue);
  208. }
  209. this.refs.checkbox.checked = newSwitchedValue;
  210. } else {
  211. process.env.NODE_ENV !== "production" ? (0, _warning2.default)(false, 'Material-UI: Cannot call set method while checked is defined as a property.') : void 0;
  212. }
  213. }
  214. }, {
  215. key: 'getValue',
  216. value: function getValue() {
  217. return this.refs.checkbox.value;
  218. }
  219. // Checkbox inputs only use SPACE to change their state. Using ENTER will
  220. // update the ui but not the input.
  221. /**
  222. * Because both the ripples and the checkbox input cannot share pointer
  223. * events, the checkbox input takes control of pointer events and calls
  224. * ripple animations manually.
  225. */
  226. }, {
  227. key: 'render',
  228. value: function render() {
  229. var _props = this.props,
  230. name = _props.name,
  231. value = _props.value,
  232. checked = _props.checked,
  233. iconStyle = _props.iconStyle,
  234. inputStyle = _props.inputStyle,
  235. inputType = _props.inputType,
  236. label = _props.label,
  237. labelStyle = _props.labelStyle,
  238. labelPosition = _props.labelPosition,
  239. onSwitch = _props.onSwitch,
  240. onBlur = _props.onBlur,
  241. onFocus = _props.onFocus,
  242. onMouseUp = _props.onMouseUp,
  243. onMouseDown = _props.onMouseDown,
  244. onMouseLeave = _props.onMouseLeave,
  245. onTouchStart = _props.onTouchStart,
  246. onTouchEnd = _props.onTouchEnd,
  247. onParentShouldUpdate = _props.onParentShouldUpdate,
  248. disabled = _props.disabled,
  249. disableTouchRipple = _props.disableTouchRipple,
  250. disableFocusRipple = _props.disableFocusRipple,
  251. className = _props.className,
  252. rippleColor = _props.rippleColor,
  253. rippleStyle = _props.rippleStyle,
  254. style = _props.style,
  255. switched = _props.switched,
  256. switchElement = _props.switchElement,
  257. thumbStyle = _props.thumbStyle,
  258. trackStyle = _props.trackStyle,
  259. other = (0, _objectWithoutProperties3.default)(_props, ['name', 'value', 'checked', 'iconStyle', 'inputStyle', 'inputType', 'label', 'labelStyle', 'labelPosition', 'onSwitch', 'onBlur', 'onFocus', 'onMouseUp', 'onMouseDown', 'onMouseLeave', 'onTouchStart', 'onTouchEnd', 'onParentShouldUpdate', 'disabled', 'disableTouchRipple', 'disableFocusRipple', 'className', 'rippleColor', 'rippleStyle', 'style', 'switched', 'switchElement', 'thumbStyle', 'trackStyle']);
  260. var prepareStyles = this.context.muiTheme.prepareStyles;
  261. var styles = getStyles(this.props, this.context);
  262. var wrapStyles = (0, _simpleAssign2.default)(styles.wrap, iconStyle);
  263. var mergedRippleStyle = (0, _simpleAssign2.default)(styles.ripple, rippleStyle);
  264. if (thumbStyle) {
  265. wrapStyles.marginLeft /= 2;
  266. wrapStyles.marginRight /= 2;
  267. }
  268. var labelElement = label && _react2.default.createElement(
  269. 'label',
  270. { style: prepareStyles((0, _simpleAssign2.default)(styles.label, labelStyle)) },
  271. label
  272. );
  273. var showTouchRipple = !disabled && !disableTouchRipple;
  274. var showFocusRipple = !disabled && !disableFocusRipple;
  275. var touchRipple = _react2.default.createElement(_TouchRipple2.default, {
  276. ref: 'touchRipple',
  277. key: 'touchRipple',
  278. style: mergedRippleStyle,
  279. color: mergedRippleStyle.color,
  280. muiTheme: this.context.muiTheme,
  281. centerRipple: true
  282. });
  283. var focusRipple = _react2.default.createElement(_FocusRipple2.default, {
  284. key: 'focusRipple',
  285. innerStyle: mergedRippleStyle,
  286. color: mergedRippleStyle.color,
  287. muiTheme: this.context.muiTheme,
  288. show: this.state.isKeyboardFocused
  289. });
  290. var ripples = [showTouchRipple ? touchRipple : null, showFocusRipple ? focusRipple : null];
  291. var touchHandlers = showTouchRipple ? {
  292. onMouseUp: this.handleMouseUp,
  293. onMouseDown: this.handleMouseDown,
  294. onMouseLeave: this.handleMouseLeave,
  295. onTouchStart: this.handleTouchStart,
  296. onTouchEnd: this.handleTouchEnd
  297. } : {};
  298. var inputElement = _react2.default.createElement('input', (0, _extends3.default)({}, other, {
  299. ref: 'checkbox',
  300. type: inputType,
  301. style: prepareStyles((0, _simpleAssign2.default)(styles.input, inputStyle)),
  302. name: name,
  303. value: value,
  304. checked: this.state.switched,
  305. disabled: disabled,
  306. onBlur: this.handleBlur,
  307. onFocus: this.handleFocus,
  308. onChange: this.handleChange
  309. }, touchHandlers));
  310. // If toggle component (indicated by whether the style includes thumb) manually lay out
  311. // elements in order to nest ripple elements
  312. var switchOrThumbElement = !thumbStyle ? _react2.default.createElement(
  313. 'div',
  314. { style: prepareStyles(wrapStyles) },
  315. switchElement,
  316. ripples
  317. ) : _react2.default.createElement(
  318. 'div',
  319. { style: prepareStyles(wrapStyles) },
  320. _react2.default.createElement('div', { style: prepareStyles((0, _simpleAssign2.default)({}, trackStyle)) }),
  321. _react2.default.createElement(
  322. _Paper2.default,
  323. { style: thumbStyle, zDepth: 1, circle: true },
  324. ' ',
  325. ripples,
  326. ' '
  327. )
  328. );
  329. var elementsInOrder = labelPosition === 'right' ? _react2.default.createElement(
  330. 'div',
  331. { style: styles.controls },
  332. switchOrThumbElement,
  333. labelElement
  334. ) : _react2.default.createElement(
  335. 'div',
  336. { style: styles.controls },
  337. labelElement,
  338. switchOrThumbElement
  339. );
  340. return _react2.default.createElement(
  341. 'div',
  342. { ref: 'root', className: className, style: prepareStyles((0, _simpleAssign2.default)(styles.root, style)) },
  343. _react2.default.createElement(_reactEventListener2.default, {
  344. target: 'window',
  345. onKeyDown: this.handleKeyDown,
  346. onKeyUp: this.handleKeyUp
  347. }),
  348. inputElement,
  349. elementsInOrder
  350. );
  351. }
  352. }]);
  353. return EnhancedSwitch;
  354. }(_react.Component);
  355. EnhancedSwitch.contextTypes = {
  356. muiTheme: _propTypes2.default.object.isRequired
  357. };
  358. EnhancedSwitch.propTypes = process.env.NODE_ENV !== "production" ? {
  359. checked: _propTypes2.default.bool,
  360. className: _propTypes2.default.string,
  361. defaultChecked: _propTypes2.default.bool,
  362. disableFocusRipple: _propTypes2.default.bool,
  363. disableTouchRipple: _propTypes2.default.bool,
  364. disabled: _propTypes2.default.bool,
  365. iconStyle: _propTypes2.default.object,
  366. inputStyle: _propTypes2.default.object,
  367. inputType: _propTypes2.default.string.isRequired,
  368. label: _propTypes2.default.node,
  369. labelPosition: _propTypes2.default.oneOf(['left', 'right']),
  370. labelStyle: _propTypes2.default.object,
  371. name: _propTypes2.default.string,
  372. onBlur: _propTypes2.default.func,
  373. onFocus: _propTypes2.default.func,
  374. onMouseDown: _propTypes2.default.func,
  375. onMouseLeave: _propTypes2.default.func,
  376. onMouseUp: _propTypes2.default.func,
  377. onParentShouldUpdate: _propTypes2.default.func,
  378. onSwitch: _propTypes2.default.func,
  379. onTouchEnd: _propTypes2.default.func,
  380. onTouchStart: _propTypes2.default.func,
  381. rippleColor: _propTypes2.default.string,
  382. rippleStyle: _propTypes2.default.object,
  383. style: _propTypes2.default.object,
  384. switchElement: _propTypes2.default.element.isRequired,
  385. switched: _propTypes2.default.bool.isRequired,
  386. thumbStyle: _propTypes2.default.object,
  387. trackStyle: _propTypes2.default.object,
  388. value: _propTypes2.default.any
  389. } : {};
  390. exports.default = EnhancedSwitch;