wix-style-react
Version:
wix-style-react
161 lines (118 loc) • 6.27 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _uniqueId = require('lodash/uniqueId');
var _uniqueId2 = _interopRequireDefault(_uniqueId);
var _classnames = require('classnames');
var _classnames2 = _interopRequireDefault(_classnames);
var _RadioButton = require('./RadioButton/RadioButton');
var _RadioButton2 = _interopRequireDefault(_RadioButton);
var _RadioGroup = require('./RadioGroup.scss');
var _RadioGroup2 = _interopRequireDefault(_RadioGroup);
var _WixComponent2 = require('../BaseComponents/WixComponent');
var _WixComponent3 = _interopRequireDefault(_WixComponent2);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
/**
* component for easy radio group creation.
*
* similar to HTML `<input type="radio"/>` except you don't need to handle `name` or click handlers
*/
var RadioGroup = function (_WixComponent) {
_inherits(RadioGroup, _WixComponent);
function RadioGroup(props) {
_classCallCheck(this, RadioGroup);
var _this = _possibleConstructorReturn(this, (RadioGroup.__proto__ || Object.getPrototypeOf(RadioGroup)).call(this, props));
_this.name = (0, _uniqueId2.default)('RadioGroup_');
return _this;
}
_createClass(RadioGroup, [{
key: 'render',
value: function render() {
var _this2 = this;
var _props = this.props,
onChange = _props.onChange,
disabled = _props.disabled,
disabledRadios = _props.disabledRadios,
value = _props.value,
vAlign = _props.vAlign,
display = _props.display,
type = _props.type,
spacing = _props.spacing,
lineHeight = _props.lineHeight;
return _react2.default.createElement(
'div',
{
className: (0, _classnames2.default)(_RadioGroup2.default[display], _defineProperty({}, _RadioGroup2.default.buttonType, type === 'button'))
},
_react2.default.Children.map(this.props.children, function (radio, index) {
return _react2.default.createElement(
RadioGroup.Radio,
{
dataHook: radio.props.dataHook,
value: radio.props.value,
name: _this2.name,
onChange: onChange,
vAlign: vAlign,
type: type,
disabled: disabled || disabledRadios.indexOf(radio.props.value) !== -1,
checked: radio.props.value === value,
style: display === 'vertical' && index > 0 ? { marginTop: spacing } : {},
icon: radio.props.icon,
lineHeight: lineHeight,
content: radio.props.content
},
radio.props.children
);
})
);
}
}]);
return RadioGroup;
}(_WixComponent3.default);
RadioGroup.propTypes = {
/** Callback function when user selects a different value */
onChange: _propTypes2.default.func,
/** Selected radio button value */
value: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number]),
/** the values of the disabled radio buttons */
disabledRadios: _propTypes2.default.arrayOf(_propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number])),
/** Positioning of the radio bottom compared to the label */
vAlign: _propTypes2.default.oneOf(['center', 'top']),
/** Make the entire control disabled */
disabled: _propTypes2.default.bool,
/** Decided which type of child controls to render */
type: _propTypes2.default.oneOf(['default', 'button']),
/** Display direction of the radios */
display: _propTypes2.default.oneOf(['vertical', 'horizontal']),
children: _propTypes2.default.arrayOf(function (propValue, key) {
if (propValue[key].type.displayName !== _RadioButton2.default.displayName) {
return new Error('RadioGroup: Invalid Prop children was given. Validation failed on child number ' + key);
}
}),
/** Vertical spacing between radio buttons */
spacing: _propTypes2.default.string,
lineHeight: _propTypes2.default.string
};
RadioGroup.defaultProps = {
disabledRadios: [],
onChange: function onChange() {},
value: '',
vAlign: 'center',
display: 'vertical',
spacing: '12px',
lineHeight: '24px',
type: 'default'
};
RadioGroup.Radio = _RadioButton2.default;
RadioGroup.displayName = 'RadioGroup';
exports.default = RadioGroup;