You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

275 lines
8.6 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 _transitions = require('../styles/transitions');
  26. var _transitions2 = _interopRequireDefault(_transitions);
  27. var _EnhancedSwitch = require('../internal/EnhancedSwitch');
  28. var _EnhancedSwitch2 = _interopRequireDefault(_EnhancedSwitch);
  29. var _radioButtonUnchecked = require('../svg-icons/toggle/radio-button-unchecked');
  30. var _radioButtonUnchecked2 = _interopRequireDefault(_radioButtonUnchecked);
  31. var _radioButtonChecked = require('../svg-icons/toggle/radio-button-checked');
  32. var _radioButtonChecked2 = _interopRequireDefault(_radioButtonChecked);
  33. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  34. function getStyles(props, context) {
  35. var radioButton = context.muiTheme.radioButton;
  36. return {
  37. icon: {
  38. height: radioButton.size,
  39. width: radioButton.size
  40. },
  41. target: {
  42. transition: _transitions2.default.easeOut(),
  43. position: 'absolute',
  44. opacity: 1,
  45. transform: 'scale(1)',
  46. fill: radioButton.borderColor
  47. },
  48. fill: {
  49. position: 'absolute',
  50. opacity: 1,
  51. transform: 'scale(0)',
  52. transformOrigin: '50% 50%',
  53. transition: _transitions2.default.easeOut(),
  54. fill: radioButton.checkedColor
  55. },
  56. targetWhenChecked: {
  57. opacity: 0,
  58. transform: 'scale(0)'
  59. },
  60. fillWhenChecked: {
  61. opacity: 1,
  62. transform: 'scale(1)'
  63. },
  64. targetWhenDisabled: {
  65. fill: radioButton.disabledColor
  66. },
  67. fillWhenDisabled: {
  68. fill: radioButton.disabledColor
  69. },
  70. label: {
  71. color: props.disabled ? radioButton.labelDisabledColor : radioButton.labelColor
  72. },
  73. ripple: {
  74. color: props.checked ? radioButton.checkedColor : radioButton.borderColor
  75. }
  76. };
  77. }
  78. var RadioButton = function (_Component) {
  79. (0, _inherits3.default)(RadioButton, _Component);
  80. function RadioButton() {
  81. var _ref;
  82. var _temp, _this, _ret;
  83. (0, _classCallCheck3.default)(this, RadioButton);
  84. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  85. args[_key] = arguments[_key];
  86. }
  87. return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = RadioButton.__proto__ || (0, _getPrototypeOf2.default)(RadioButton)).call.apply(_ref, [this].concat(args))), _this), _this.handleSwitch = function (event) {
  88. if (_this.props.onCheck) {
  89. _this.props.onCheck(event, _this.props.value);
  90. }
  91. }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  92. }
  93. // Only called when selected, not when unselected.
  94. (0, _createClass3.default)(RadioButton, [{
  95. key: 'isChecked',
  96. value: function isChecked() {
  97. return this.refs.enhancedSwitch.isSwitched();
  98. }
  99. // Use RadioButtonGroup.setSelectedValue(newSelectionValue) to set a
  100. // RadioButton's checked value.
  101. }, {
  102. key: 'setChecked',
  103. value: function setChecked(newCheckedValue) {
  104. this.refs.enhancedSwitch.setSwitched(newCheckedValue);
  105. }
  106. }, {
  107. key: 'getValue',
  108. value: function getValue() {
  109. return this.refs.enhancedSwitch.getValue();
  110. }
  111. }, {
  112. key: 'render',
  113. value: function render() {
  114. var _props = this.props,
  115. checkedIcon = _props.checkedIcon,
  116. checked = _props.checked,
  117. iconStyle = _props.iconStyle,
  118. labelStyle = _props.labelStyle,
  119. labelPosition = _props.labelPosition,
  120. onCheck = _props.onCheck,
  121. uncheckedIcon = _props.uncheckedIcon,
  122. disabled = _props.disabled,
  123. other = (0, _objectWithoutProperties3.default)(_props, ['checkedIcon', 'checked', 'iconStyle', 'labelStyle', 'labelPosition', 'onCheck', 'uncheckedIcon', 'disabled']);
  124. var styles = getStyles(this.props, this.context);
  125. var uncheckedStyles = (0, _simpleAssign2.default)(styles.target, checked && styles.targetWhenChecked, iconStyle, disabled && styles.targetWhenDisabled);
  126. var checkedStyles = (0, _simpleAssign2.default)(styles.fill, checked && styles.fillWhenChecked, iconStyle, disabled && styles.fillWhenDisabled);
  127. var uncheckedElement = _react2.default.isValidElement(uncheckedIcon) ? _react2.default.cloneElement(uncheckedIcon, {
  128. style: (0, _simpleAssign2.default)(uncheckedStyles, uncheckedIcon.props.style)
  129. }) : _react2.default.createElement(_radioButtonUnchecked2.default, { style: uncheckedStyles });
  130. var checkedElement = _react2.default.isValidElement(checkedIcon) ? _react2.default.cloneElement(checkedIcon, {
  131. style: (0, _simpleAssign2.default)(checkedStyles, checkedIcon.props.style)
  132. }) : _react2.default.createElement(_radioButtonChecked2.default, { style: checkedStyles });
  133. var mergedIconStyle = (0, _simpleAssign2.default)(styles.icon, iconStyle);
  134. var mergedLabelStyle = (0, _simpleAssign2.default)(styles.label, labelStyle);
  135. return _react2.default.createElement(_EnhancedSwitch2.default, (0, _extends3.default)({}, other, {
  136. ref: 'enhancedSwitch',
  137. inputType: 'radio',
  138. checked: checked,
  139. switched: checked,
  140. disabled: disabled,
  141. rippleColor: styles.ripple.color,
  142. iconStyle: mergedIconStyle,
  143. labelStyle: mergedLabelStyle,
  144. labelPosition: labelPosition,
  145. onSwitch: this.handleSwitch,
  146. switchElement: _react2.default.createElement(
  147. 'div',
  148. null,
  149. uncheckedElement,
  150. checkedElement
  151. )
  152. }));
  153. }
  154. }]);
  155. return RadioButton;
  156. }(_react.Component);
  157. RadioButton.defaultProps = {
  158. checked: false,
  159. disabled: false,
  160. labelPosition: 'right'
  161. };
  162. RadioButton.contextTypes = {
  163. muiTheme: _propTypes2.default.object.isRequired
  164. };
  165. RadioButton.propTypes = process.env.NODE_ENV !== "production" ? {
  166. /**
  167. * @ignore
  168. * checked if true
  169. * Used internally by `RadioButtonGroup`.
  170. */
  171. checked: _propTypes2.default.bool,
  172. /**
  173. * The icon element to show when the radio button is checked.
  174. */
  175. checkedIcon: _propTypes2.default.element,
  176. /**
  177. * If true, the radio button is disabled.
  178. */
  179. disabled: _propTypes2.default.bool,
  180. /**
  181. * Override the inline-styles of the icon element.
  182. */
  183. iconStyle: _propTypes2.default.object,
  184. /**
  185. * Override the inline-styles of the input element.
  186. */
  187. inputStyle: _propTypes2.default.object,
  188. /**
  189. * @ignore
  190. * Used internally by `RadioButtonGroup`. Use the `labelPosition` property of `RadioButtonGroup` instead.
  191. * Where the label will be placed next to the radio button.
  192. */
  193. labelPosition: _propTypes2.default.oneOf(['left', 'right']),
  194. /**
  195. * Override the inline-styles of the label element.
  196. */
  197. labelStyle: _propTypes2.default.object,
  198. /**
  199. * @ignore
  200. * Callback function fired when the radio button is checked. Note that this
  201. * function will not be called if the radio button is part of a
  202. * radio button group: in this case, use the `onChange` property of
  203. * `RadioButtonGroup`.
  204. *
  205. * @param {object} event `change` event targeting the element.
  206. * @param {string} value The element's `value`.
  207. */
  208. onCheck: _propTypes2.default.func,
  209. /**
  210. * Override the inline-styles of the root element.
  211. */
  212. style: _propTypes2.default.object,
  213. /**
  214. * The icon element to show when the radio button is unchecked.
  215. */
  216. uncheckedIcon: _propTypes2.default.element,
  217. /**
  218. * The value of the radio button.
  219. */
  220. value: _propTypes2.default.any
  221. } : {};
  222. exports.default = RadioButton;