UNPKG

@kiwicom/orbit-components

Version:

Orbit-components is a React component library which provides developers with the easiest possible way of building Kiwi.com’s products.

252 lines (214 loc) 8.61 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = exports.Label = void 0; var React = _interopRequireWildcard(require("react")); var _styledComponents = _interopRequireWildcard(require("styled-components")); var _orbitDesignTokens = require("@kiwicom/orbit-design-tokens"); var _defaultTheme = _interopRequireDefault(require("../defaultTheme")); var _consts = _interopRequireDefault(require("./consts")); var _Check = _interopRequireDefault(require("../icons/Check")); var _Text = require("../Text"); var _rtl = require("../utils/rtl"); var _getFieldDataState = _interopRequireDefault(require("../common/getFieldDataState")); var _cloneWithTooltip = _interopRequireDefault(require("../utils/cloneWithTooltip")); var _mediaQuery = _interopRequireDefault(require("../utils/mediaQuery")); const getToken = name => ({ theme, hasError, disabled, checked }) => { const resolveBorderColor = () => { if (disabled) { return theme.orbit.paletteCloudDarker; } if (checked) { return theme.orbit.paletteBlueNormal; } if (hasError && !disabled && !checked) { return theme.orbit.borderColorCheckboxRadioError; } return theme.orbit.borderColorCheckboxRadio; }; const getBackground = () => { if (disabled && checked) { return theme.orbit.paletteCloudDarker; } if (disabled && !checked) { return theme.orbit.paletteCloudNormal; } return checked ? theme.orbit.paletteBlueNormal : theme.orbit.backgroundInput; }; const tokens = { [_consts.default.background]: getBackground(), [_consts.default.borderColor]: resolveBorderColor(), [_consts.default.iconColor]: disabled ? theme.orbit.paletteCloudNormal : theme.orbit.colorIconCheckboxRadio }; return tokens[name]; }; const IconContainer = _styledComponents.default.div.withConfig({ displayName: "Checkbox__IconContainer", componentId: "sc-1xqef2c-0" })(["position:relative;box-sizing:border-box;flex:0 0 auto;display:flex;align-items:center;justify-content:center;background-color:", ";height:", ";width:", ";border-radius:", ";transform:scale(1);transition:all ", " ease-in-out;& > svg{visibility:hidden;display:flex;align-items:center;justify-content:center;width:16px;height:16px;}&:hover{background-color:", ";}", ""], getToken(_consts.default.background), ({ theme }) => theme.orbit.heightCheckbox, ({ theme }) => theme.orbit.widthCheckbox, ({ theme }) => theme.orbit.borderRadiusLarge, ({ theme }) => theme.orbit.durationFast, ({ theme, checked }) => checked ? theme.orbit.paletteBlueDark : theme.orbit.backgroundInput, _mediaQuery.default.desktop((0, _styledComponents.css)(["border-radius:", ";"], ({ theme }) => theme.orbit.borderRadiusNormal))); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 IconContainer.defaultProps = { theme: _defaultTheme.default }; const TextContainer = _styledComponents.default.div.withConfig({ displayName: "Checkbox__TextContainer", componentId: "sc-1xqef2c-1" })(["display:flex;flex-direction:column;margin:", ";flex:1;"], ({ theme }) => (0, _rtl.rtlSpacing)(`0 0 0 ${theme.orbit.spaceXSmall}`)); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 TextContainer.defaultProps = { theme: _defaultTheme.default }; const Info = _styledComponents.default.span.withConfig({ displayName: "Checkbox__Info", componentId: "sc-1xqef2c-2" })(["font-size:", ";color:", ";line-height:", ";"], ({ theme }) => theme.orbit.fontSizeFormFeedback, ({ theme }) => theme.orbit.colorInfoCheckBoxRadio, ({ theme }) => theme.orbit.lineHeightTextSmall); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 Info.defaultProps = { theme: _defaultTheme.default }; const LabelText = _styledComponents.default.span.withConfig({ displayName: "Checkbox__LabelText", componentId: "sc-1xqef2c-3" })(["font-family:", ";font-weight:", ";font-size:", ";color:", ";line-height:", ";", "{font-weight:", ";font-size:", ";color:", ";line-height:", ";}"], ({ theme }) => theme.orbit.fontFamily, ({ theme }) => theme.orbit.fontWeightNormal, ({ theme }) => theme.orbit.fontSizeFormLabel, ({ theme }) => theme.orbit.colorFormLabel, ({ theme }) => theme.orbit.heightCheckbox, _Text.StyledText, ({ theme }) => theme.orbit.fontWeightNormal, ({ theme }) => theme.orbit.fontSizeFormLabel, ({ theme }) => theme.orbit.colorFormLabel, ({ theme }) => theme.orbit.heightCheckbox); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 LabelText.defaultProps = { theme: _defaultTheme.default }; const Input = _styledComponents.default.input.withConfig({ displayName: "Checkbox__Input", componentId: "sc-1xqef2c-4" })(["opacity:0;z-index:-1;position:absolute;&:checked ~ ", " > ", "{font-weight:", ";& > ", "{font-weight:", ";}}&:checked + ", " > svg{visibility:visible;}&:focus + ", "{border:", ";box-shadow:0 0 0 3px ", ";}"], TextContainer, LabelText, ({ theme }) => theme.orbit.fontWeightMedium, _Text.StyledText, ({ theme }) => theme.orbit.fontWeightMedium, IconContainer, IconContainer, ({ theme, error }) => `1px ${theme.orbit.borderStyleInput} ${error ? theme.orbit.paletteRedNormal : theme.orbit.borderColorCheckboxRadioFocus}`, ({ theme, error }) => (0, _orbitDesignTokens.convertHexToRgba)(error ? theme.orbit.paletteRedNormal : theme.orbit.borderColorInputFocus, 15)); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 Input.defaultProps = { theme: _defaultTheme.default }; const Label = (0, _styledComponents.default)(({ className, children, dataTest }) => /*#__PURE__*/React.createElement("label", { className: className, "data-test": dataTest }, children)).withConfig({ displayName: "Checkbox__Label", componentId: "sc-1xqef2c-5" })(["font-family:", ";display:flex;width:100%;flex-direction:row;align-items:self-start;opacity:", ";cursor:", ";position:relative;", "{color:", ";border:1px solid ", ";}&:hover ", "{border-color:", ";box-shadow:none;}&:active ", "{border-color:", ";transform:", ";}"], ({ theme }) => theme.orbit.fontFamily, ({ disabled, theme }) => disabled ? theme.orbit.opacityCheckboxDisabled : "1", ({ disabled }) => disabled ? "not-allowed" : "pointer", IconContainer, getToken(_consts.default.iconColor), getToken(_consts.default.borderColor), IconContainer, ({ disabled, theme, checked }) => !disabled && checked ? theme.orbit.paletteBlueDark : theme.orbit.paletteBlueLightActive, IconContainer, ({ disabled, theme }) => disabled ? getToken(_consts.default.borderColor) : theme.orbit.paletteBlueNormal, ({ disabled, theme }) => !disabled && `scale(${theme.orbit.modifierScaleCheckboxRadioActive})`); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 exports.Label = Label; Label.defaultProps = { theme: _defaultTheme.default }; const Checkbox = /*#__PURE__*/React.forwardRef((props, ref) => { const { label, value, hasError = false, disabled = false, checked = false, name, onChange, dataTest, info, readOnly, tabIndex, tooltip } = props; const preventOnClick = React.useCallback(ev => { ev.preventDefault(); }, []); return /*#__PURE__*/React.createElement(Label, { disabled: disabled, hasError: hasError, checked: checked }, /*#__PURE__*/React.createElement(Input, { "data-test": dataTest, "data-state": (0, _getFieldDataState.default)(!!hasError), value: value, type: "checkbox", disabled: disabled, name: name, tabIndex: tabIndex, checked: checked, onChange: onChange, ref: ref, readOnly: readOnly, error: hasError }), (0, _cloneWithTooltip.default)(tooltip, /*#__PURE__*/React.createElement(IconContainer, { disabled: disabled, checked: checked, onClick: readOnly ? preventOnClick : null }, /*#__PURE__*/React.createElement(_Check.default, { customColor: "white" }))), (label || info) && /*#__PURE__*/React.createElement(TextContainer, null, label && /*#__PURE__*/React.createElement(LabelText, null, label), info && /*#__PURE__*/React.createElement(Info, null, info))); }); Checkbox.displayName = "Checkbox"; var _default = Checkbox; exports.default = _default;