271 lines
8.5 KiB
JavaScript
271 lines
8.5 KiB
JavaScript
'use strict';
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
|
|
var _extends2 = require('babel-runtime/helpers/extends');
|
|
|
|
var _extends3 = _interopRequireDefault(_extends2);
|
|
|
|
var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
|
|
|
|
var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
|
|
|
|
var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
|
|
|
|
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
|
|
|
|
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
|
|
|
|
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
|
|
|
|
var _createClass2 = require('babel-runtime/helpers/createClass');
|
|
|
|
var _createClass3 = _interopRequireDefault(_createClass2);
|
|
|
|
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
|
|
|
|
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
|
|
|
|
var _inherits2 = require('babel-runtime/helpers/inherits');
|
|
|
|
var _inherits3 = _interopRequireDefault(_inherits2);
|
|
|
|
var _simpleAssign = require('simple-assign');
|
|
|
|
var _simpleAssign2 = _interopRequireDefault(_simpleAssign);
|
|
|
|
var _react = require('react');
|
|
|
|
var _react2 = _interopRequireDefault(_react);
|
|
|
|
var _transitions = require('../styles/transitions');
|
|
|
|
var _transitions2 = _interopRequireDefault(_transitions);
|
|
|
|
var _EnhancedSwitch = require('../internal/EnhancedSwitch');
|
|
|
|
var _EnhancedSwitch2 = _interopRequireDefault(_EnhancedSwitch);
|
|
|
|
var _radioButtonUnchecked = require('../svg-icons/toggle/radio-button-unchecked');
|
|
|
|
var _radioButtonUnchecked2 = _interopRequireDefault(_radioButtonUnchecked);
|
|
|
|
var _radioButtonChecked = require('../svg-icons/toggle/radio-button-checked');
|
|
|
|
var _radioButtonChecked2 = _interopRequireDefault(_radioButtonChecked);
|
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
|
|
function getStyles(props, context) {
|
|
var radioButton = context.muiTheme.radioButton;
|
|
|
|
|
|
return {
|
|
icon: {
|
|
height: radioButton.size,
|
|
width: radioButton.size
|
|
},
|
|
target: {
|
|
transition: _transitions2.default.easeOut(),
|
|
position: 'absolute',
|
|
opacity: 1,
|
|
transform: 'scale(1)',
|
|
fill: radioButton.borderColor
|
|
},
|
|
fill: {
|
|
position: 'absolute',
|
|
opacity: 1,
|
|
transform: 'scale(0)',
|
|
transformOrigin: '50% 50%',
|
|
transition: _transitions2.default.easeOut(),
|
|
fill: radioButton.checkedColor
|
|
},
|
|
targetWhenChecked: {
|
|
opacity: 0,
|
|
transform: 'scale(0)'
|
|
},
|
|
fillWhenChecked: {
|
|
opacity: 1,
|
|
transform: 'scale(1)'
|
|
},
|
|
targetWhenDisabled: {
|
|
fill: radioButton.disabledColor
|
|
},
|
|
fillWhenDisabled: {
|
|
fill: radioButton.disabledColor
|
|
},
|
|
label: {
|
|
color: props.disabled ? radioButton.labelDisabledColor : radioButton.labelColor
|
|
},
|
|
ripple: {
|
|
color: props.checked ? radioButton.checkedColor : radioButton.borderColor
|
|
}
|
|
};
|
|
}
|
|
|
|
var RadioButton = function (_Component) {
|
|
(0, _inherits3.default)(RadioButton, _Component);
|
|
|
|
function RadioButton() {
|
|
var _ref;
|
|
|
|
var _temp, _this, _ret;
|
|
|
|
(0, _classCallCheck3.default)(this, RadioButton);
|
|
|
|
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
|
|
args[_key] = arguments[_key];
|
|
}
|
|
|
|
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) {
|
|
if (_this.props.onCheck) {
|
|
_this.props.onCheck(event, _this.props.value);
|
|
}
|
|
}, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
|
|
}
|
|
|
|
// Only called when selected, not when unselected.
|
|
|
|
|
|
(0, _createClass3.default)(RadioButton, [{
|
|
key: 'isChecked',
|
|
value: function isChecked() {
|
|
return this.refs.enhancedSwitch.isSwitched();
|
|
}
|
|
|
|
// Use RadioButtonGroup.setSelectedValue(newSelectionValue) to set a
|
|
// RadioButton's checked value.
|
|
|
|
}, {
|
|
key: 'setChecked',
|
|
value: function setChecked(newCheckedValue) {
|
|
this.refs.enhancedSwitch.setSwitched(newCheckedValue);
|
|
}
|
|
}, {
|
|
key: 'getValue',
|
|
value: function getValue() {
|
|
return this.refs.enhancedSwitch.getValue();
|
|
}
|
|
}, {
|
|
key: 'render',
|
|
value: function render() {
|
|
var _props = this.props,
|
|
checkedIcon = _props.checkedIcon,
|
|
checked = _props.checked,
|
|
iconStyle = _props.iconStyle,
|
|
labelStyle = _props.labelStyle,
|
|
labelPosition = _props.labelPosition,
|
|
onCheck = _props.onCheck,
|
|
uncheckedIcon = _props.uncheckedIcon,
|
|
disabled = _props.disabled,
|
|
other = (0, _objectWithoutProperties3.default)(_props, ['checkedIcon', 'checked', 'iconStyle', 'labelStyle', 'labelPosition', 'onCheck', 'uncheckedIcon', 'disabled']);
|
|
|
|
|
|
var styles = getStyles(this.props, this.context);
|
|
|
|
var uncheckedStyles = (0, _simpleAssign2.default)(styles.target, checked && styles.targetWhenChecked, iconStyle, disabled && styles.targetWhenDisabled);
|
|
|
|
var checkedStyles = (0, _simpleAssign2.default)(styles.fill, checked && styles.fillWhenChecked, iconStyle, disabled && styles.fillWhenDisabled);
|
|
|
|
var uncheckedElement = _react2.default.isValidElement(uncheckedIcon) ? _react2.default.cloneElement(uncheckedIcon, {
|
|
style: (0, _simpleAssign2.default)(uncheckedStyles, uncheckedIcon.props.style)
|
|
}) : _react2.default.createElement(_radioButtonUnchecked2.default, { style: uncheckedStyles });
|
|
|
|
var checkedElement = _react2.default.isValidElement(checkedIcon) ? _react2.default.cloneElement(checkedIcon, {
|
|
style: (0, _simpleAssign2.default)(checkedStyles, checkedIcon.props.style)
|
|
}) : _react2.default.createElement(_radioButtonChecked2.default, { style: checkedStyles });
|
|
|
|
var mergedIconStyle = (0, _simpleAssign2.default)(styles.icon, iconStyle);
|
|
var mergedLabelStyle = (0, _simpleAssign2.default)(styles.label, labelStyle);
|
|
|
|
return _react2.default.createElement(_EnhancedSwitch2.default, (0, _extends3.default)({}, other, {
|
|
ref: 'enhancedSwitch',
|
|
inputType: 'radio',
|
|
checked: checked,
|
|
switched: checked,
|
|
disabled: disabled,
|
|
rippleColor: styles.ripple.color,
|
|
iconStyle: mergedIconStyle,
|
|
labelStyle: mergedLabelStyle,
|
|
labelPosition: labelPosition,
|
|
onSwitch: this.handleSwitch,
|
|
switchElement: _react2.default.createElement(
|
|
'div',
|
|
null,
|
|
uncheckedElement,
|
|
checkedElement
|
|
)
|
|
}));
|
|
}
|
|
}]);
|
|
return RadioButton;
|
|
}(_react.Component);
|
|
|
|
RadioButton.defaultProps = {
|
|
checked: false,
|
|
disabled: false,
|
|
labelPosition: 'right'
|
|
};
|
|
RadioButton.contextTypes = {
|
|
muiTheme: _react.PropTypes.object.isRequired
|
|
};
|
|
process.env.NODE_ENV !== "production" ? RadioButton.propTypes = {
|
|
/**
|
|
* @ignore
|
|
* checked if true
|
|
* Used internally by `RadioButtonGroup`.
|
|
*/
|
|
checked: _react.PropTypes.bool,
|
|
/**
|
|
* The icon element to show when the radio button is checked.
|
|
*/
|
|
checkedIcon: _react.PropTypes.element,
|
|
/**
|
|
* If true, the radio button is disabled.
|
|
*/
|
|
disabled: _react.PropTypes.bool,
|
|
/**
|
|
* Override the inline-styles of the icon element.
|
|
*/
|
|
iconStyle: _react.PropTypes.object,
|
|
/**
|
|
* Override the inline-styles of the input element.
|
|
*/
|
|
inputStyle: _react.PropTypes.object,
|
|
/**
|
|
* @ignore
|
|
* Used internally by `RadioButtonGroup`. Use the `labelPosition` property of `RadioButtonGroup` instead.
|
|
* Where the label will be placed next to the radio button.
|
|
*/
|
|
labelPosition: _react.PropTypes.oneOf(['left', 'right']),
|
|
/**
|
|
* Override the inline-styles of the label element.
|
|
*/
|
|
labelStyle: _react.PropTypes.object,
|
|
/**
|
|
* @ignore
|
|
* Callback function fired when the radio button is checked. Note that this
|
|
* function will not be called if the radio button is part of a
|
|
* radio button group: in this case, use the `onChange` property of
|
|
* `RadioButtonGroup`.
|
|
*
|
|
* @param {object} event `change` event targeting the element.
|
|
* @param {string} value The element's `value`.
|
|
*/
|
|
onCheck: _react.PropTypes.func,
|
|
/**
|
|
* Override the inline-styles of the root element.
|
|
*/
|
|
style: _react.PropTypes.object,
|
|
/**
|
|
* The icon element to show when the radio button is unchecked.
|
|
*/
|
|
uncheckedIcon: _react.PropTypes.element,
|
|
/**
|
|
* The value of the radio button.
|
|
*/
|
|
value: _react.PropTypes.any
|
|
} : void 0;
|
|
exports.default = RadioButton; |