@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.
184 lines (173 loc) • 8.49 kB
JavaScript
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import * as React from "react";
import styled, { css } from "styled-components";
import { convertHexToRgba } from "@kiwicom/orbit-design-tokens";
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";
var getBorderColor = function getBorderColor() {
return function (_ref) {
var theme = _ref.theme,
hasError = _ref.hasError,
disabled = _ref.disabled,
checked = _ref.checked;
if (disabled) return theme.orbit.paletteCloudDarker;
if (checked) return theme.orbit.paletteBlueNormal;
if (hasError && !disabled && !checked) return theme.orbit.borderColorCheckboxRadioError;
return theme.orbit.borderColorCheckboxRadio;
};
};
var getBackground = function getBackground() {
return function (_ref2) {
var theme = _ref2.theme,
disabled = _ref2.disabled,
checked = _ref2.checked;
if (disabled && checked) return theme.orbit.paletteCloudDarker;
if (disabled && !checked) return theme.orbit.paletteCloudNormal;
return checked ? theme.orbit.paletteBlueNormal : theme.orbit.backgroundInput;
};
};
var Glyph = styled.span.withConfig({
displayName: "Radio__Glyph",
componentId: "sc-5lanou-0"
})(["visibility:hidden;width:8px;height:8px;background-color:", ";border-radius:", ";flex-shrink:0;"], function (_ref3) {
var theme = _ref3.theme,
disabled = _ref3.disabled;
return disabled ? theme.orbit.paletteCloudNormal : theme.orbit.paletteWhite;
}, function (_ref4) {
var theme = _ref4.theme;
return theme.orbit.borderRadiusCircle;
}); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198
Glyph.defaultProps = {
theme: defaultTheme
};
var IconContainer = styled.div.withConfig({
displayName: "Radio__IconContainer",
componentId: "sc-5lanou-1"
})(["", ""], function (_ref5) {
var theme = _ref5.theme;
return 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);
}); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198
IconContainer.defaultProps = {
theme: defaultTheme
};
var TextContainer = styled.div.withConfig({
displayName: "Radio__TextContainer",
componentId: "sc-5lanou-2"
})(["display:flex;flex-direction:column;margin:", ";flex:1;"], function (_ref6) {
var theme = _ref6.theme;
return rtlSpacing("0 0 0 ".concat(theme.orbit.spaceXSmall));
}); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198
TextContainer.defaultProps = {
theme: defaultTheme
};
var Info = styled.span.withConfig({
displayName: "Radio__Info",
componentId: "sc-5lanou-3"
})(["font-size:", ";color:", ";line-height:", ";"], function (_ref7) {
var theme = _ref7.theme;
return theme.orbit.fontSizeFormFeedback;
}, function (_ref8) {
var theme = _ref8.theme;
return theme.orbit.colorInfoCheckBoxRadio;
}, function (_ref9) {
var theme = _ref9.theme;
return theme.orbit.lineHeightTextSmall;
}); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198
Info.defaultProps = {
theme: defaultTheme
};
var LabelText = styled.span.withConfig({
displayName: "Radio__LabelText",
componentId: "sc-5lanou-4"
})(["", ""], function (_ref10) {
var theme = _ref10.theme;
return css(["font-weight:", ";font-size:", ";color:", ";line-height:", ";", "{font-weight:", ";font-size:", ";color:", ";line-height:", ";}"], theme.orbit.fontWeightNormal, theme.orbit.fontSizeFormLabel, theme.orbit.colorFormLabel, theme.orbit.heightCheckbox, StyledText, theme.orbit.fontWeightNormal, 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
};
var Input = styled.input.withConfig({
displayName: "Radio__Input",
componentId: "sc-5lanou-5"
})(["position:absolute;opacity:0;&:checked ~ ", " > ", "{font-weight:", ";& > ", "{font-weight:", ";}}&:checked + ", " > ", "{visibility:visible;}&:focus + ", "{outline:0;border:", ";box-shadow:0 0 0 3px ", ";", "}"], TextContainer, LabelText, function (_ref11) {
var theme = _ref11.theme;
return theme.orbit.fontWeightMedium;
}, StyledText, function (_ref12) {
var theme = _ref12.theme;
return theme.orbit.fontWeightMedium;
}, IconContainer, Glyph, IconContainer, function (_ref13) {
var theme = _ref13.theme,
hasError = _ref13.hasError;
return "2px ".concat(theme.orbit.borderStyleInput, " ").concat(hasError ? theme.orbit.paletteRedNormal : theme.orbit.borderColorCheckboxRadioFocus);
}, function (_ref14) {
var theme = _ref14.theme,
hasError = _ref14.hasError;
return convertHexToRgba(hasError ? theme.orbit.paletteRedNormal : theme.orbit.borderColorInputFocus, 15);
}, media.largeMobile(css(["border-width:1px;"]))); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198
Input.defaultProps = {
theme: defaultTheme
};
var Label = styled(function (_ref15) {
var disabled = _ref15.disabled,
theme = _ref15.theme,
type = _ref15.type,
hasError = _ref15.hasError,
props = _objectWithoutProperties(_ref15, ["disabled", "theme", "type", "hasError"]);
return /*#__PURE__*/React.createElement("label", props, props.children);
}).withConfig({
displayName: "Radio__Label",
componentId: "sc-5lanou-6"
})(["", ""], function (_ref16) {
var theme = _ref16.theme,
disabled = _ref16.disabled;
return css(["font-family:", ";display:flex;width:100%;flex-direction:row;align-items:self-start;opacity:", ";cursor:", ";position:relative;", "{border:2px solid ", ";}&:hover ", "{border-color:", ";}&:active ", "{border-color:", ";transform:", ";}", ""], theme.orbit.fontFamily, disabled ? theme.orbit.opacityCheckboxDisabled : "1", disabled ? "default" : "pointer", IconContainer, getBorderColor, IconContainer, !disabled && theme.orbit.paletteBlueLightActive, IconContainer, disabled ? getBorderColor : theme.orbit.paletteBlueNormal, !disabled && "scale(".concat(theme.orbit.modifierScaleCheckboxRadioActive, ")"), media.largeMobile(css(["", "{border:1px solid ", ";}"], IconContainer, getBorderColor)));
}); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198
Label.defaultProps = {
theme: defaultTheme
};
var Radio = /*#__PURE__*/React.forwardRef(function (props, ref) {
var label = props.label,
value = props.value,
_props$hasError = props.hasError,
hasError = _props$hasError === void 0 ? false : _props$hasError,
_props$disabled = props.disabled,
disabled = _props$disabled === void 0 ? false : _props$disabled,
_props$checked = props.checked,
checked = _props$checked === void 0 ? false : _props$checked,
onChange = props.onChange,
name = props.name,
info = props.info,
readOnly = props.readOnly,
tabIndex = props.tabIndex,
dataTest = props.dataTest,
tooltip = props.tooltip;
return /*#__PURE__*/React.createElement(Label, {
disabled: disabled,
hasError: hasError,
checked: checked
}, /*#__PURE__*/React.createElement(Input, {
"data-test": dataTest,
"data-state": getFieldDataState(hasError),
value: value,
type: "radio",
disabled: disabled,
checked: checked,
onChange: onChange,
name: name,
tabIndex: tabIndex,
ref: ref,
readOnly: readOnly,
hasError: hasError
}), cloneWithTooltip(tooltip, /*#__PURE__*/React.createElement(IconContainer, {
disabled: disabled,
checked: checked
}, /*#__PURE__*/React.createElement(Glyph, {
disabled: disabled
}))), (label || info) && /*#__PURE__*/React.createElement(TextContainer, null, label && /*#__PURE__*/React.createElement(LabelText, null, label), info && /*#__PURE__*/React.createElement(Info, null, info)));
});
Radio.displayName = "Radio";
export default Radio;