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.

188 lines (171 loc) 7.24 kB
import * as React from "react"; import styled, { css } from "styled-components"; import { convertHexToRgba } from "@kiwicom/orbit-design-tokens"; import defaultTheme from "../defaultTheme"; import TOKENS from "./consts"; import Check from "../icons/Check"; import { StyledText } from "../Text"; import { rtlSpacing } from "../utils/rtl"; import getFieldDataState from "../common/getFieldDataState"; import cloneWithTooltip from "../utils/cloneWithTooltip"; import media from "../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 = { [TOKENS.background]: getBackground(), [TOKENS.borderColor]: resolveBorderColor(), [TOKENS.iconColor]: disabled ? theme.orbit.paletteCloudNormal : theme.orbit.colorIconCheckboxRadio }; return tokens[name]; }; const IconContainer = styled.div.withConfig({ displayName: "Checkbox__IconContainer", componentId: "sc-1xqef2c-0" })(["", ";"], ({ theme, checked }) => css(["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(TOKENS.background), theme.orbit.heightCheckbox, theme.orbit.widthCheckbox, theme.orbit.borderRadiusLarge, theme.orbit.durationFast, checked ? theme.orbit.paletteBlueDark : theme.orbit.backgroundInput, media.desktop(css(["border-radius:", ";"], theme.orbit.borderRadiusNormal)))); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 IconContainer.defaultProps = { theme: defaultTheme }; const TextContainer = styled.div.withConfig({ displayName: "Checkbox__TextContainer", componentId: "sc-1xqef2c-1" })(["display:flex;flex-direction:column;margin:", ";flex:1;"], ({ theme }) => rtlSpacing(`0 0 0 ${theme.orbit.spaceXSmall}`)); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 TextContainer.defaultProps = { theme: defaultTheme }; const Info = styled.span.withConfig({ displayName: "Checkbox__Info", componentId: "sc-1xqef2c-2" })(["", ";"], ({ theme }) => css(["font-size:", ";color:", ";line-height:", ";"], theme.orbit.fontSizeFormFeedback, theme.orbit.colorInfoCheckBoxRadio, theme.orbit.lineHeightTextSmall)); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 Info.defaultProps = { theme: defaultTheme }; const LabelText = styled.span.withConfig({ displayName: "Checkbox__LabelText", componentId: "sc-1xqef2c-3" })(["", ""], ({ theme }) => css(["font-family:", ";font-weight:", ";font-size:", ";color:", ";line-height:", ";", "{font-weight:", ";font-size:", ";color:", ";line-height:", ";}"], theme.orbit.fontFamily, theme.orbit.fontWeightMedium, theme.orbit.fontSizeFormLabel, theme.orbit.colorFormLabel, theme.orbit.heightCheckbox, StyledText, theme.orbit.fontWeightMedium, theme.orbit.fontSizeFormLabel, theme.orbit.colorFormLabel, theme.orbit.heightCheckbox)); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 LabelText.defaultProps = { theme: defaultTheme }; const Input = styled.input.withConfig({ displayName: "Checkbox__Input", componentId: "sc-1xqef2c-4" })(["opacity:0;z-index:-1;position:absolute;&:checked + ", " > svg{visibility:visible;}&:focus + ", "{border:", ";box-shadow:0 0 0 3px ", ";}"], IconContainer, IconContainer, ({ theme, error }) => `1px ${theme.orbit.borderStyleInput} ${error ? theme.orbit.paletteRedNormal : theme.orbit.borderColorCheckboxRadioFocus}`, ({ theme, error }) => 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 }; export const Label = styled(({ 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(TOKENS.iconColor), getToken(TOKENS.borderColor), IconContainer, ({ disabled, theme, checked }) => !disabled && checked ? theme.orbit.paletteBlueDark : theme.orbit.paletteBlueLightActive, IconContainer, ({ disabled, theme }) => disabled ? getToken(TOKENS.borderColor) : theme.orbit.paletteBlueNormal, ({ disabled, theme }) => !disabled && `scale(${theme.orbit.modifierScaleCheckboxRadioActive})`); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 Label.defaultProps = { theme: defaultTheme }; const Checkbox = /*#__PURE__*/React.forwardRef((props, ref) => { const { label, value, hasError = false, disabled = false, checked = false, name, onChange, dataTest, id, info, readOnly, tabIndex, tooltip } = props; const preventOnClick = ev => ev.preventDefault(); return /*#__PURE__*/React.createElement(Label, { disabled: disabled, hasError: hasError, checked: checked }, /*#__PURE__*/React.createElement(Input, { "data-test": dataTest, id: id, "data-state": getFieldDataState(!!hasError), value: value, type: "checkbox", disabled: disabled, name: name, tabIndex: tabIndex, checked: checked, onChange: onChange, ref: ref, readOnly: readOnly, error: hasError }), cloneWithTooltip(tooltip, /*#__PURE__*/React.createElement(IconContainer, { disabled: disabled, checked: checked, onClick: readOnly ? preventOnClick : null }, /*#__PURE__*/React.createElement(Check, { 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"; export default Checkbox;