@age/quantum
Version:
Catho react components
204 lines (171 loc) • 8.99 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _styledComponents = _interopRequireDefault(require("styled-components"));
var _Button = _interopRequireDefault(require("../Button"));
var _HiddenInput = _interopRequireDefault(require("../shared/HiddenInput"));
var _Icon = _interopRequireDefault(require("../Icon"));
var _uniqId = _interopRequireDefault(require("../shared/uniqId"));
var _theme = require("../shared/theme");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
var ID_GENERATOR = (0, _uniqId.default)('radio-button-');
var LockIcon = (0, _styledComponents.default)(_Icon.default).attrs({
name: 'lock'
}).withConfig({
displayName: "RadioButton__LockIcon",
componentId: "sc-16wgqz-0"
})(["", ""], function (_ref) {
var xsmall = _ref.theme.spacing.xsmall;
return "\n margin-left: ".concat(xsmall, "px;\n");
});
var ButtonIcon = (0, _styledComponents.default)(_Icon.default).withConfig({
displayName: "RadioButton__ButtonIcon",
componentId: "sc-16wgqz-1"
})(["", ""], function (_ref2) {
var xsmall = _ref2.theme.spacing.xsmall;
return "\n margin-right: ".concat(xsmall, "px;\n");
});
var Wrapper = _styledComponents.default.div.withConfig({
displayName: "RadioButton__Wrapper",
componentId: "sc-16wgqz-2"
})(["", ""], function (_ref3) {
var inline = _ref3.inline,
_ref3$theme = _ref3.theme,
baseFontSize = _ref3$theme.baseFontSize,
_ref3$theme$spacing = _ref3$theme.spacing,
xsmall = _ref3$theme$spacing.xsmall,
medium = _ref3$theme$spacing.medium;
return inline && "\n display: inline-block;\n margin-right: ".concat(medium, "px;\n vertical-align: top;\n\n :last-child {\n margin-right: 0;\n }\n\n ").concat(LockIcon, " {\n font-size: ").concat(baseFontSize, "px;\n margin-left: ").concat(xsmall, "px;\n }\n ");
});
var RadioButton = (0, _styledComponents.default)(_Button.default).withConfig({
displayName: "RadioButton",
componentId: "sc-16wgqz-3"
})(["", " display:inline-flex;"], function (_ref4) {
var xsmall = _ref4.theme.spacing.xsmall;
return "\n margin-bottom: ".concat(xsmall, "px;\n ");
});
var Radio =
/*#__PURE__*/
function (_React$Component) {
_inherits(Radio, _React$Component);
function Radio(props) {
var _this;
_classCallCheck(this, Radio);
_this = _possibleConstructorReturn(this, _getPrototypeOf(Radio).call(this, props));
var id = props.id;
_this._id = id || ID_GENERATOR.next().value;
return _this;
}
_createClass(Radio, [{
key: "render",
value: function render() {
var _this$props = this.props,
children = _this$props.children,
label = _this$props.label,
error = _this$props.error,
disabled = _this$props.disabled,
_onChange = _this$props.onChange,
value = _this$props.value,
checked = _this$props.checked,
icon = _this$props.icon,
id = _this$props.id,
inline = _this$props.inline,
skin = _this$props.skin,
theme = _this$props.theme,
rest = _objectWithoutProperties(_this$props, ["children", "label", "error", "disabled", "onChange", "value", "checked", "icon", "id", "inline", "skin", "theme"]);
return _react.default.createElement(Wrapper, {
inline: inline,
theme: theme
}, _react.default.createElement(RadioButton, {
checked: checked,
disabled: disabled,
error: error,
htmlFor: this._id,
skin: error ? 'error' : skin,
stroked: !checked,
theme: theme,
$as: "label"
}, _react.default.createElement(_HiddenInput.default, _extends({
type: "radio",
checked: checked,
disabled: disabled,
id: this._id,
onChange: function onChange(e) {
return _onChange({
value: value,
label: label
}, e);
},
value: value,
skin: skin,
error: error
}, rest)), icon && _react.default.createElement(ButtonIcon, {
theme: theme,
name: icon
}), children || label, disabled && _react.default.createElement(LockIcon, {
theme: theme
})));
}
}]);
return Radio;
}(_react.default.Component);
Radio.displayName = 'RadioGroup.Button';
Radio.defaultProps = {
checked: false,
children: undefined,
skin: 'primary',
disabled: false,
error: false,
icon: undefined,
id: undefined,
inline: false,
label: undefined,
onChange: function onChange() {},
theme: {
baseFontSize: _theme.baseFontSize,
spacing: _theme.spacing,
colors: _theme.colors,
components: {
button: _theme.components.button
}
}
};
Radio.propTypes = {
checked: _propTypes.default.bool,
children: _propTypes.default.string,
skin: _propTypes.default.oneOf(['neutral', 'primary', 'success', 'warning', 'error']),
disabled: _propTypes.default.bool,
error: _propTypes.default.bool,
icon: _propTypes.default.string,
id: _propTypes.default.string,
inline: _propTypes.default.bool,
label: _propTypes.default.string,
onChange: _propTypes.default.func,
value: _propTypes.default.string.isRequired,
theme: _propTypes.default.shape({
baseFontSize: _propTypes.default.number,
spacing: _propTypes.default.object,
colors: _propTypes.default.object,
components: _propTypes.default.shape({
button: _propTypes.default.object
})
})
};
var _default = Radio;
exports.default = _default;