Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

Checkbox.js 9.3 KiB

3 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 _EnhancedSwitch = require('../internal/EnhancedSwitch');
  26. var _EnhancedSwitch2 = _interopRequireDefault(_EnhancedSwitch);
  27. var _transitions = require('../styles/transitions');
  28. var _transitions2 = _interopRequireDefault(_transitions);
  29. var _checkBoxOutlineBlank = require('../svg-icons/toggle/check-box-outline-blank');
  30. var _checkBoxOutlineBlank2 = _interopRequireDefault(_checkBoxOutlineBlank);
  31. var _checkBox = require('../svg-icons/toggle/check-box');
  32. var _checkBox2 = _interopRequireDefault(_checkBox);
  33. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  34. function getStyles(props, context) {
  35. var checkbox = context.muiTheme.checkbox;
  36. var checkboxSize = 24;
  37. return {
  38. icon: {
  39. height: checkboxSize,
  40. width: checkboxSize
  41. },
  42. check: {
  43. position: 'absolute',
  44. opacity: 0,
  45. transform: 'scale(0)',
  46. transitionOrigin: '50% 50%',
  47. transition: _transitions2.default.easeOut('450ms', 'opacity', '0ms') + ', ' + _transitions2.default.easeOut('0ms', 'transform', '450ms'),
  48. fill: checkbox.checkedColor
  49. },
  50. checkWhenSwitched: {
  51. opacity: 1,
  52. transform: 'scale(1)',
  53. transition: _transitions2.default.easeOut('0ms', 'opacity', '0ms') + ', ' + _transitions2.default.easeOut('800ms', 'transform', '0ms')
  54. },
  55. checkWhenDisabled: {
  56. fill: checkbox.disabledColor
  57. },
  58. box: {
  59. position: 'absolute',
  60. opacity: 1,
  61. fill: checkbox.boxColor,
  62. transition: _transitions2.default.easeOut('1000ms', 'opacity', '200ms')
  63. },
  64. boxWhenSwitched: {
  65. opacity: 0,
  66. transition: _transitions2.default.easeOut('650ms', 'opacity', '150ms'),
  67. fill: checkbox.checkedColor
  68. },
  69. boxWhenDisabled: {
  70. fill: props.checked ? 'transparent' : checkbox.disabledColor
  71. },
  72. label: {
  73. color: props.disabled ? checkbox.labelDisabledColor : checkbox.labelColor
  74. }
  75. };
  76. }
  77. var Checkbox = function (_Component) {
  78. (0, _inherits3.default)(Checkbox, _Component);
  79. function Checkbox() {
  80. var _ref;
  81. var _temp, _this, _ret;
  82. (0, _classCallCheck3.default)(this, Checkbox);
  83. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  84. args[_key] = arguments[_key];
  85. }
  86. return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = Checkbox.__proto__ || (0, _getPrototypeOf2.default)(Checkbox)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
  87. switched: false
  88. }, _this.handleStateChange = function (newSwitched) {
  89. _this.setState({
  90. switched: newSwitched
  91. });
  92. }, _this.handleCheck = function (event, isInputChecked) {
  93. if (_this.props.onCheck) {
  94. _this.props.onCheck(event, isInputChecked);
  95. }
  96. }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  97. }
  98. (0, _createClass3.default)(Checkbox, [{
  99. key: 'componentWillMount',
  100. value: function componentWillMount() {
  101. var _props = this.props,
  102. checked = _props.checked,
  103. defaultChecked = _props.defaultChecked,
  104. valueLink = _props.valueLink;
  105. if (checked || defaultChecked || valueLink && valueLink.value) {
  106. this.setState({
  107. switched: true
  108. });
  109. }
  110. }
  111. }, {
  112. key: 'componentWillReceiveProps',
  113. value: function componentWillReceiveProps(nextProps) {
  114. if (this.props.checked !== nextProps.checked) {
  115. this.setState({
  116. switched: nextProps.checked
  117. });
  118. }
  119. }
  120. }, {
  121. key: 'isChecked',
  122. value: function isChecked() {
  123. return this.refs.enhancedSwitch.isSwitched();
  124. }
  125. }, {
  126. key: 'setChecked',
  127. value: function setChecked(newCheckedValue) {
  128. this.refs.enhancedSwitch.setSwitched(newCheckedValue);
  129. }
  130. }, {
  131. key: 'render',
  132. value: function render() {
  133. var _props2 = this.props,
  134. iconStyle = _props2.iconStyle,
  135. onCheck = _props2.onCheck,
  136. checkedIcon = _props2.checkedIcon,
  137. uncheckedIcon = _props2.uncheckedIcon,
  138. other = (0, _objectWithoutProperties3.default)(_props2, ['iconStyle', 'onCheck', 'checkedIcon', 'uncheckedIcon']);
  139. var styles = getStyles(this.props, this.context);
  140. var boxStyles = (0, _simpleAssign2.default)(styles.box, this.state.switched && styles.boxWhenSwitched, iconStyle, this.props.disabled && styles.boxWhenDisabled);
  141. var checkStyles = (0, _simpleAssign2.default)(styles.check, this.state.switched && styles.checkWhenSwitched, iconStyle, this.props.disabled && styles.checkWhenDisabled);
  142. var checkedElement = checkedIcon ? _react2.default.cloneElement(checkedIcon, {
  143. style: (0, _simpleAssign2.default)(checkStyles, checkedIcon.props.style)
  144. }) : _react2.default.createElement(_checkBox2.default, {
  145. style: checkStyles
  146. });
  147. var unCheckedElement = uncheckedIcon ? _react2.default.cloneElement(uncheckedIcon, {
  148. style: (0, _simpleAssign2.default)(boxStyles, uncheckedIcon.props.style)
  149. }) : _react2.default.createElement(_checkBoxOutlineBlank2.default, {
  150. style: boxStyles
  151. });
  152. var checkboxElement = _react2.default.createElement(
  153. 'div',
  154. null,
  155. unCheckedElement,
  156. checkedElement
  157. );
  158. var rippleColor = this.state.switched ? checkStyles.fill : boxStyles.fill;
  159. var mergedIconStyle = (0, _simpleAssign2.default)(styles.icon, iconStyle);
  160. var labelStyle = (0, _simpleAssign2.default)(styles.label, this.props.labelStyle);
  161. var enhancedSwitchProps = {
  162. ref: 'enhancedSwitch',
  163. inputType: 'checkbox',
  164. switched: this.state.switched,
  165. switchElement: checkboxElement,
  166. rippleColor: rippleColor,
  167. iconStyle: mergedIconStyle,
  168. onSwitch: this.handleCheck,
  169. labelStyle: labelStyle,
  170. onParentShouldUpdate: this.handleStateChange,
  171. labelPosition: this.props.labelPosition
  172. };
  173. return _react2.default.createElement(_EnhancedSwitch2.default, (0, _extends3.default)({}, other, enhancedSwitchProps));
  174. }
  175. }]);
  176. return Checkbox;
  177. }(_react.Component);
  178. Checkbox.defaultProps = {
  179. labelPosition: 'right',
  180. disabled: false
  181. };
  182. Checkbox.contextTypes = {
  183. muiTheme: _propTypes2.default.object.isRequired
  184. };
  185. Checkbox.propTypes = process.env.NODE_ENV !== "production" ? {
  186. /**
  187. * Checkbox is checked if true.
  188. */
  189. checked: _propTypes2.default.bool,
  190. /**
  191. * The SvgIcon to use for the checked state.
  192. * This is useful to create icon toggles.
  193. */
  194. checkedIcon: _propTypes2.default.element,
  195. /**
  196. * The default state of our checkbox component.
  197. * **Warning:** This cannot be used in conjunction with `checked`.
  198. * Decide between using a controlled or uncontrolled input element and remove one of these props.
  199. * More info: https://fb.me/react-controlled-components
  200. */
  201. defaultChecked: _propTypes2.default.bool,
  202. /**
  203. * Disabled if true.
  204. */
  205. disabled: _propTypes2.default.bool,
  206. /**
  207. * Overrides the inline-styles of the icon element.
  208. */
  209. iconStyle: _propTypes2.default.object,
  210. /**
  211. * Overrides the inline-styles of the input element.
  212. */
  213. inputStyle: _propTypes2.default.object,
  214. /**
  215. * Where the label will be placed next to the checkbox.
  216. */
  217. labelPosition: _propTypes2.default.oneOf(['left', 'right']),
  218. /**
  219. * Overrides the inline-styles of the Checkbox element label.
  220. */
  221. labelStyle: _propTypes2.default.object,
  222. /**
  223. * Callback function that is fired when the checkbox is checked.
  224. *
  225. * @param {object} event `change` event targeting the underlying checkbox `input`.
  226. * @param {boolean} isInputChecked The `checked` value of the underlying checkbox `input`.
  227. */
  228. onCheck: _propTypes2.default.func,
  229. /**
  230. * Override the inline-styles of the root element.
  231. */
  232. style: _propTypes2.default.object,
  233. /**
  234. * The SvgIcon to use for the unchecked state.
  235. * This is useful to create icon toggles.
  236. */
  237. uncheckedIcon: _propTypes2.default.element,
  238. /**
  239. * ValueLink for when using controlled checkbox.
  240. */
  241. valueLink: _propTypes2.default.object
  242. } : {};
  243. exports.default = Checkbox;