UNPKG

@catho/quantum

Version:
164 lines (163 loc) 9.95 kB
"use strict"; function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _react = require("react"); var _propTypes = _interopRequireDefault(require("prop-types")); var _styledComponents = _interopRequireDefault(require("styled-components")); var _shared = require("../shared"); var _HiddenInput = _interopRequireDefault(require("../shared/HiddenInput")); var _Icon = _interopRequireDefault(require("../Icon")); var _CheckboxGroupContext = _interopRequireDefault(require("./CheckboxGroupContext")); var _theme = require("../shared/theme"); var _jsxRuntime = require("react/jsx-runtime"); var _excluded = ["children", "id", "label", "value", "theme", "error", "onChange", "checked", "disabled"]; function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], t.indexOf(o) >= 0 || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; } function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.indexOf(n) >= 0) continue; t[n] = r[n]; } return t; } var CHECKBOX_SIZE = '24px'; var Wrapper = _styledComponents["default"].div.withConfig({ displayName: "Checkbox__Wrapper", componentId: "sc-1ca5whm-0" })(["", ""], function (_ref) { var inline = _ref.inline; return inline && "\n display: inline-block;\n margin-right: 16px;\n vertical-align: top;\n\n &:last-child {\n margin-right: 0;\n }\n "; }); var CheckboxWrapper = _styledComponents["default"].div.withConfig({ displayName: "Checkbox__CheckboxWrapper", componentId: "sc-1ca5whm-1" })(["", ""], function (_ref2) { var xxsmall = _ref2.theme.spacing.xxsmall; return "\n display: flex;\n position: relative;\n margin-top: ".concat(xxsmall, "px;\n"); }); var CheckboxLabel = (0, _styledComponents["default"])(_shared.Label).withConfig({ displayName: "Checkbox__CheckboxLabel", componentId: "sc-1ca5whm-2" })(["", ""], function (_ref3) { var _ref3$theme = _ref3.theme, neutralColor = _ref3$theme.colors.neutral[700], small = _ref3$theme.spacing.small; return "\n color: ".concat(neutralColor, ";\n margin: 0 0 0 ").concat(small, "px;\n\n a {\n position: relative;\n z-index: 5;\n }\n "); }); CheckboxLabel.displayName = 'CheckboxLabel'; var CheckIcon = (0, _styledComponents["default"])(_Icon["default"]).attrs({ name: 'check' }).withConfig({ displayName: "Checkbox__CheckIcon", componentId: "sc-1ca5whm-3" })([""]); var HiddenCheckbox = (0, _styledComponents["default"])(_HiddenInput["default"]).attrs({ type: 'checkbox' }).withConfig({ displayName: "Checkbox__HiddenCheckbox", componentId: "sc-1ca5whm-4" })(["cursor:pointer;height:100%;width:100%;+ ", "{align-items:center;border-radius:2px;box-sizing:border-box;color:transparent;display:flex;font-weight:bold;height:", ";justify-content:center;transition:all 0.2s ease-in-out;width:", ";", "}&:checked{+ ", "{border-width:0;", "}}&:hover + ", ",&:focus + ", "{", "}", " &[disabled]{cursor:not-allowed;", " &:hover + ", "{", "}}"], CheckIcon, CHECKBOX_SIZE, CHECKBOX_SIZE, function (_ref4) { var _ref4$theme = _ref4.theme, _ref4$theme$colors$ne = _ref4$theme.colors.neutral, neutral0 = _ref4$theme$colors$ne[0], neutral500 = _ref4$theme$colors$ne[500], fontSize = _ref4$theme.baseFontSize; return "\n background-color: ".concat(neutral0, ";\n border: 2px solid ").concat(neutral500, ";\n font-size: ").concat(fontSize * 1.25, "px;\n "); }, CheckIcon, function (_ref5) { var _ref5$theme$colors = _ref5.theme.colors, primaryColor = _ref5$theme$colors.primary[700], neutralColor = _ref5$theme$colors.neutral[100]; return "\n background-color: ".concat(primaryColor, ";\n color: ").concat(neutralColor, ";\n "); }, CheckIcon, CheckIcon, function (_ref6) { var theme = _ref6.theme; var primaryColor = theme.colors.primary[700]; return "\n border-color: ".concat(primaryColor, ";\n ").concat((0, _shared.shadow)(5, primaryColor)({ theme: theme }), "\n "); }, function (_ref7) { var error = _ref7.error, theme = _ref7.theme; var errorColor = theme.colors.error[700]; return error && "\n + ".concat(CheckIcon, " {\n border-color: ").concat(errorColor, ";\n }\n\n &:checked + ").concat(CheckIcon, " {\n background-color: ").concat(errorColor, ";\n }\n\n &:hover + ").concat(CheckIcon, ", &:focus + ").concat(CheckIcon, " {\n border-color: ").concat(errorColor, ";\n ").concat((0, _shared.shadow)(5, errorColor)({ theme: theme }), "\n }\n "); }, function (_ref8) { var _ref8$theme$colors$ne = _ref8.theme.colors.neutral, neutral500 = _ref8$theme$colors$ne[500], neutral300 = _ref8$theme$colors$ne[300]; return "\n ~ ".concat(CheckboxLabel, " {\n color: ").concat(neutral500, ";\n }\n\n + ").concat(CheckIcon, " {\n border-color: ").concat(neutral500, ";\n background-color: ").concat(neutral300, ";\n }\n\n &:checked + ").concat(CheckIcon, " {\n background-color: ").concat(neutral500, ";\n }\n "); }, CheckIcon, (0, _shared.shadow)()); HiddenCheckbox.displayName = 'HiddenCheckbox'; var Checkbox = function Checkbox(_ref9) { var _ref9$children = _ref9.children, children = _ref9$children === void 0 ? '' : _ref9$children, _ref9$id = _ref9.id, id = _ref9$id === void 0 ? '' : _ref9$id, _ref9$label = _ref9.label, label = _ref9$label === void 0 ? '' : _ref9$label, _ref9$value = _ref9.value, value = _ref9$value === void 0 ? '' : _ref9$value, _ref9$theme = _ref9.theme, theme = _ref9$theme === void 0 ? { colors: _theme.colors, spacing: _theme.spacing, baseFontSize: _theme.baseFontSize } : _ref9$theme, _ref9$error = _ref9.error, errorProp = _ref9$error === void 0 ? '' : _ref9$error, _ref9$onChange = _ref9.onChange, onChangeProp = _ref9$onChange === void 0 ? function () {} : _ref9$onChange, _ref9$checked = _ref9.checked, checked = _ref9$checked === void 0 ? false : _ref9$checked, _ref9$disabled = _ref9.disabled, disabled = _ref9$disabled === void 0 ? false : _ref9$disabled, props = _objectWithoutProperties(_ref9, _excluded); var context = (0, _react.useContext)(_CheckboxGroupContext["default"]); var errorContext = context.error; var _context$error = context.error, error = _context$error === void 0 ? errorProp : _context$error, _context$onChange = context.onChange, onChange = _context$onChange === void 0 ? onChangeProp : _context$onChange, inline = context.inline; return /*#__PURE__*/(0, _jsxRuntime.jsxs)(Wrapper, { inline: inline, children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(CheckboxWrapper, { theme: theme, children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(HiddenCheckbox, _objectSpread({ checked: checked, disabled: disabled, id: id, error: error, value: value, onChange: onChange, theme: theme }, props)), /*#__PURE__*/(0, _jsxRuntime.jsx)(CheckIcon, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(CheckboxLabel, { htmlFor: id, theme: theme, children: children || label || value })] }), error && !errorContext && /*#__PURE__*/(0, _jsxRuntime.jsx)(_shared.ErrorMessage, { children: error })] }); }; Checkbox.propTypes = { checked: _propTypes["default"].bool, disabled: _propTypes["default"].bool, children: _propTypes["default"].node, error: _propTypes["default"].string, id: _propTypes["default"].string, label: _propTypes["default"].node, name: _propTypes["default"].string.isRequired, value: _propTypes["default"].string, onChange: _propTypes["default"].func, theme: _propTypes["default"].shape({ colors: _propTypes["default"].object, spacing: _propTypes["default"].object, baseFontSize: _propTypes["default"].number }) }; Checkbox.displayName = 'Checkbox'; var _default = exports["default"] = Checkbox;