@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.
142 lines • 5.97 kB
JavaScript
import * as React from "react";
import styled, { css } from "styled-components";
import defaultTheme from "../defaultTheme";
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 getBorderColor = () => ({
theme,
hasError,
disabled,
checked
}) => {
if (disabled) return theme.orbit.paletteCloudNormal;
if (checked) return theme.orbit.paletteBlueNormal;
if (hasError && !disabled && !checked) return theme.orbit.borderColorCheckboxRadioError;
return theme.orbit.borderColorCheckboxRadio;
};
const getBackground = () => ({
theme,
disabled,
checked
}) => {
if (disabled) return theme.orbit.paletteCloudNormal;
return checked ? theme.orbit.paletteBlueNormal : theme.orbit.backgroundInput;
};
const Glyph = styled.span.withConfig({
displayName: "Radio__Glyph",
componentId: "sc-1e6hy4x-0"
})(["", ""], ({
theme,
disabled
}) => css(["visibility:hidden;width:8px;height:8px;background-color:", ";border-radius:", ";flex-shrink:0;"], disabled ? theme.orbit.paletteCloudNormal : theme.orbit.paletteWhite, theme.orbit.borderRadiusCircle));
Glyph.defaultProps = {
theme: defaultTheme
};
const StyledIconContainer = styled.div.withConfig({
displayName: "Radio__StyledIconContainer",
componentId: "sc-1e6hy4x-1"
})(["", ""], ({
theme
}) => 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;"], getBackground, theme.orbit.heightCheckbox, theme.orbit.widthCheckbox, theme.orbit.borderRadiusCircle, theme.orbit.durationFast));
StyledIconContainer.defaultProps = {
theme: defaultTheme
};
const StyledTextContainer = styled.div.withConfig({
displayName: "Radio__StyledTextContainer",
componentId: "sc-1e6hy4x-2"
})(["", ""], ({
theme,
disabled
}) => css(["display:flex;flex-direction:column;font-weight:", ";margin:", ";flex:1;opacity:", ";"], theme.orbit.fontWeightMedium, rtlSpacing(`0 0 0 ${theme.orbit.spaceXSmall}`), disabled ? theme.orbit.opacityRadioButtonDisabled : "1"));
StyledTextContainer.defaultProps = {
theme: defaultTheme
};
const StyledInfo = styled.span.withConfig({
displayName: "Radio__StyledInfo",
componentId: "sc-1e6hy4x-3"
})(["", ""], ({
theme
}) => css(["font-size:", ";color:", ";line-height:", ";"], theme.orbit.fontSizeFormFeedback, theme.orbit.colorInfoCheckBoxRadio, theme.orbit.lineHeightTextSmall));
StyledInfo.defaultProps = {
theme: defaultTheme
};
const StyledLabelText = styled.span.withConfig({
displayName: "Radio__StyledLabelText",
componentId: "sc-1e6hy4x-4"
})(["", ""], ({
theme
}) => css(["font-weight:", ";font-size:", ";color:", ";line-height:", ";", "{font-weight:", ";font-size:", ";color:", ";line-height:", ";}"], 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: "Radio__StyledInput",
componentId: "sc-1e6hy4x-5"
})(["position:absolute;opacity:0;&:checked + ", " > ", "{visibility:visible;}&:focus + ", "{", ";", "}"], StyledIconContainer, Glyph, StyledIconContainer, defaultFocus, media.largeMobile(css(["border-width:1px;"])));
StyledInput.defaultProps = {
theme: defaultTheme
};
export const StyledLabel = styled(({
disabled,
theme,
type,
hasError,
...props
}) => /*#__PURE__*/React.createElement("label", props, props.children)).withConfig({
displayName: "Radio__StyledLabel",
componentId: "sc-1e6hy4x-6"
})(["", ""], ({
theme,
disabled
}) => css(["font-family:", ";display:flex;width:100%;flex-direction:row;align-items:self-start;cursor:", ";position:relative;", "{border:2px solid ", ";}&:hover ", "{border-color:", ";}&:active ", "{border-color:", ";transform:", ";}", ""], theme.orbit.fontFamily, disabled ? "not-allowed" : "pointer", StyledIconContainer, getBorderColor, StyledIconContainer, !disabled && theme.orbit.paletteBlueLightActive, StyledIconContainer, disabled ? getBorderColor : theme.orbit.paletteBlueNormal, !disabled && `scale(${theme.orbit.modifierScaleCheckboxRadioActive})`, media.largeMobile(css(["", "{border:1px solid ", ";}"], StyledIconContainer, getBorderColor))));
StyledLabel.defaultProps = {
theme: defaultTheme
};
const Radio = /*#__PURE__*/React.forwardRef((props, ref) => {
const {
label,
value,
hasError = false,
disabled = false,
checked = false,
onChange,
name,
info,
readOnly,
id,
tabIndex,
dataTest,
tooltip
} = props;
return /*#__PURE__*/React.createElement(StyledLabel, {
disabled: disabled,
hasError: hasError,
checked: checked
}, /*#__PURE__*/React.createElement(StyledInput, {
"data-test": dataTest,
"data-state": getFieldDataState(hasError),
value: value,
type: "radio",
disabled: disabled,
checked: checked,
id: id,
onChange: onChange,
name: name,
tabIndex: tabIndex ? Number(tabIndex) : undefined,
ref: ref,
readOnly: readOnly
}), cloneWithTooltip(tooltip, /*#__PURE__*/React.createElement(StyledIconContainer, {
disabled: disabled,
checked: checked
}, /*#__PURE__*/React.createElement(Glyph, {
disabled: disabled
}))), (label || info) && /*#__PURE__*/React.createElement(StyledTextContainer, {
disabled: disabled
}, label && /*#__PURE__*/React.createElement(StyledLabelText, null, label), info && /*#__PURE__*/React.createElement(StyledInfo, null, info)));
});
Radio.displayName = "Radio";
export default Radio;