@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.
156 lines • 6.51 kB
JavaScript
import * as React from "react";
import styled, { css } from "styled-components";
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";
import { defaultFocus } from "../utils/common";
const getToken = name => ({
theme,
hasError,
disabled,
checked
}) => {
const resolveBorderColor = () => {
if (disabled) {
return theme.orbit.paletteCloudDark;
}
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.paletteCloudDark;
}
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.paletteCloudDark : theme.orbit.colorIconCheckboxRadio
};
return tokens[name];
};
const StyledIconContainer = styled.div.withConfig({
displayName: "Checkbox__StyledIconContainer",
componentId: "sc-y66hzm-0"
})(["", ";"], ({
theme,
checked,
disabled
}) => 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, !disabled && (checked ? theme.orbit.paletteBlueDark : theme.orbit.backgroundInput), media.desktop(css(["border-radius:", ";"], theme.orbit.borderRadiusNormal))));
StyledIconContainer.defaultProps = {
theme: defaultTheme
};
const StyledTextContainer = styled.div.withConfig({
displayName: "Checkbox__StyledTextContainer",
componentId: "sc-y66hzm-1"
})(["", ""], ({
disabled,
theme
}) => css(["display:flex;flex-direction:column;flex:1;margin:", ";opacity:", ";"], rtlSpacing(`0 0 0 ${theme.orbit.spaceXSmall}`), disabled ? theme.orbit.opacityCheckboxDisabled : "1"));
StyledTextContainer.defaultProps = {
theme: defaultTheme
};
const Info = styled.span.withConfig({
displayName: "Checkbox__Info",
componentId: "sc-y66hzm-2"
})(["", ";"], ({
theme
}) => css(["font-size:", ";color:", ";line-height:", ";"], theme.orbit.fontSizeFormFeedback, theme.orbit.colorInfoCheckBoxRadio, theme.orbit.lineHeightTextSmall));
Info.defaultProps = {
theme: defaultTheme
};
const StyledLabelText = styled.span.withConfig({
displayName: "Checkbox__StyledLabelText",
componentId: "sc-y66hzm-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));
StyledLabelText.defaultProps = {
theme: defaultTheme
};
const StyledInput = styled.input.withConfig({
displayName: "Checkbox__StyledInput",
componentId: "sc-y66hzm-4"
})(["opacity:0;z-index:-1;position:absolute;&:checked + ", " > svg{visibility:visible;}&:focus + ", "{", ";}"], StyledIconContainer, StyledIconContainer, defaultFocus);
StyledInput.defaultProps = {
theme: defaultTheme
};
export const StyledLabel = styled(({
className,
children,
dataTest
}) => /*#__PURE__*/React.createElement("label", {
className: className,
"data-test": dataTest
}, children)).withConfig({
displayName: "Checkbox__StyledLabel",
componentId: "sc-y66hzm-5"
})(["", ""], ({
theme,
disabled,
checked
}) => css(["font-family:", ";display:flex;width:100%;flex-direction:row;align-items:self-start;cursor:", ";position:relative;", "{color:", ";border:1px solid ", ";}&:hover ", "{border-color:", ";box-shadow:none;}&:active ", "{border-color:", ";transform:", ";}"], theme.orbit.fontFamily, disabled ? "not-allowed" : "pointer", StyledIconContainer, getToken(TOKENS.iconColor), getToken(TOKENS.borderColor), StyledIconContainer, !disabled && (checked ? theme.orbit.paletteBlueDark : theme.orbit.paletteBlueLightActive), StyledIconContainer, disabled ? getToken(TOKENS.borderColor) : theme.orbit.paletteBlueNormal, !disabled && `scale(${theme.orbit.modifierScaleCheckboxRadioActive})`));
StyledLabel.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(StyledLabel, {
disabled: disabled,
hasError: hasError,
checked: checked
}, /*#__PURE__*/React.createElement(StyledInput, {
"data-test": dataTest,
id: id,
"data-state": getFieldDataState(!!hasError),
value: value,
type: "checkbox",
disabled: disabled,
name: name,
tabIndex: tabIndex ? Number(tabIndex) : undefined,
checked: checked,
onChange: onChange,
ref: ref,
readOnly: readOnly,
error: hasError
}), cloneWithTooltip(tooltip, /*#__PURE__*/React.createElement(StyledIconContainer, {
disabled: disabled,
checked: checked,
onClick: readOnly ? preventOnClick : undefined
}, /*#__PURE__*/React.createElement(Check, {
customColor: "white"
}))), (label || info) && /*#__PURE__*/React.createElement(StyledTextContainer, {
disabled: disabled
}, label && /*#__PURE__*/React.createElement(StyledLabelText, null, label), info && /*#__PURE__*/React.createElement(Info, null, info)));
});
Checkbox.displayName = "Checkbox";
export default Checkbox;