UNPKG

@age/quantum

Version:
170 lines (147 loc) 9.14 kB
"use strict"; 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); } Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireWildcard(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"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } 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; } var CHECKBOX_SIZE = '24px'; var Wrapper = _styledComponents.default.div.withConfig({ displayName: "Checkbox__Wrapper", componentId: "y1ytrh-0" })([""]); var CheckboxWrapper = _styledComponents.default.div.withConfig({ displayName: "Checkbox__CheckboxWrapper", componentId: "y1ytrh-1" })(["", ""], function (_ref) { var xxsmall = _ref.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: "y1ytrh-2" })(["", ""], function (_ref2) { var _ref2$theme = _ref2.theme, neutralColor = _ref2$theme.colors.neutral[700], small = _ref2$theme.spacing.small; return "\n color: ".concat(neutralColor, ";\n margin: 0 0 0 ").concat(small, "px;\n "); }); CheckboxLabel.displayName = 'CheckboxLabel'; var CheckIcon = (0, _styledComponents.default)(_Icon.default).attrs({ name: 'check' }).withConfig({ displayName: "Checkbox__CheckIcon", componentId: "y1ytrh-3" })([""]); var HiddenCheckbox = (0, _styledComponents.default)(_HiddenInput.default).attrs({ type: 'checkbox' }).withConfig({ displayName: "Checkbox__HiddenCheckbox", componentId: "y1ytrh-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 (_ref3) { var _ref3$theme = _ref3.theme, _ref3$theme$colors$ne = _ref3$theme.colors.neutral, neutral0 = _ref3$theme$colors$ne[0], neutral500 = _ref3$theme$colors$ne[500], fontSize = _ref3$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 (_ref4) { var _ref4$theme$colors = _ref4.theme.colors, primaryColor = _ref4$theme$colors.primary[700], neutralColor = _ref4$theme$colors.neutral[100]; return "\n background-color: ".concat(primaryColor, ";\n color: ").concat(neutralColor, ";\n "); }, CheckIcon, CheckIcon, function (_ref5) { var theme = _ref5.theme; var primaryColor = theme.colors.primary[700]; return "\n border-color: ".concat(primaryColor, ";\n ").concat((0, _shared.shadow)(5, primaryColor)({ theme: theme }), "\n "); }, function (_ref6) { var error = _ref6.error, theme = _ref6.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 (_ref7) { var _ref7$theme$colors$ne = _ref7.theme.colors.neutral, neutral500 = _ref7$theme$colors$ne[500], neutral300 = _ref7$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(_ref8) { var children = _ref8.children, id = _ref8.id, label = _ref8.label, value = _ref8.value, theme = _ref8.theme, errorProp = _ref8.error, onChangeProp = _ref8.onChange, props = _objectWithoutProperties(_ref8, ["children", "id", "label", "value", "theme", "error", "onChange"]); 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; return _react.default.createElement(Wrapper, null, _react.default.createElement(CheckboxWrapper, { theme: theme }, _react.default.createElement(HiddenCheckbox, _extends({}, props, { id: id, error: error, value: value, onChange: onChange, theme: theme })), _react.default.createElement(CheckIcon, null), _react.default.createElement(CheckboxLabel, { htmlFor: id, theme: theme }, children || label || value)), error && !errorContext && _react.default.createElement(_shared.ErrorMessage, null, error)); }; Checkbox.propTypes = { checked: _propTypes.default.bool, disabled: _propTypes.default.bool, children: _propTypes.default.string, error: _propTypes.default.string, id: _propTypes.default.string, label: _propTypes.default.string, 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.defaultProps = { checked: false, disabled: false, children: '', error: '', id: '', label: '', value: '', onChange: function onChange() {}, theme: { colors: _theme.colors, spacing: _theme.spacing, baseFontSize: _theme.baseFontSize } }; Checkbox.displayName = 'Checkbox'; var _default = Checkbox; exports.default = _default;