@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.
424 lines (406 loc) • 18 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import * as React from "react";
import styled, { css } from "styled-components";
import defaultTheme from "../defaultTheme";
import { SIZE_OPTIONS, TYPE_OPTIONS, TOKENS } from "./consts";
import { StyledServiceLogo } from "../ServiceLogo";
import { rtlSpacing } from "../utils/rtl";
import InputTags from "./InputTags";
import getSpacingToken from "../common/getSpacingToken";
import getFieldDataState from "../common/getFieldDataState";
import useRandomId from "../hooks/useRandomId";
import ErrorFormTooltip from "../ErrorFormTooltip";
import AlertCircle from "../icons/AlertCircle";
import InformationCircle from "../icons/InformationCircle";
import FormLabel from "../FormLabel";
import useErrorTooltip from "../ErrorFormTooltip/hooks/useErrorTooltip";
import formElementFocus from "./helpers/formElementFocus";
import { StyledButtonPrimitiveIconContainer } from "../primitives/ButtonPrimitive/components/ButtonPrimitiveIconContainer";
import mq from "../utils/mediaQuery";
var getToken = function getToken(name) {
return function (_ref) {
var _TOKENS$heightInput, _TOKENS$fontSizeInput, _TOKENS$iconSize, _tokens;
var theme = _ref.theme,
size = _ref.size;
var tokens = (_tokens = {}, _defineProperty(_tokens, TOKENS.heightInput, (_TOKENS$heightInput = {}, _defineProperty(_TOKENS$heightInput, SIZE_OPTIONS.SMALL, theme.orbit.heightInputSmall), _defineProperty(_TOKENS$heightInput, SIZE_OPTIONS.NORMAL, theme.orbit.heightInputNormal), _TOKENS$heightInput)), _defineProperty(_tokens, TOKENS.fontSizeInput, (_TOKENS$fontSizeInput = {}, _defineProperty(_TOKENS$fontSizeInput, SIZE_OPTIONS.SMALL, theme.orbit.fontSizeInputSmall), _defineProperty(_TOKENS$fontSizeInput, SIZE_OPTIONS.NORMAL, theme.orbit.fontSizeInputNormal), _TOKENS$fontSizeInput)), _defineProperty(_tokens, TOKENS.iconSize, (_TOKENS$iconSize = {}, _defineProperty(_TOKENS$iconSize, SIZE_OPTIONS.SMALL, theme.orbit.widthIconSmall), _defineProperty(_TOKENS$iconSize, SIZE_OPTIONS.NORMAL, theme.orbit.widthIconMedium), _TOKENS$iconSize)), _tokens);
return tokens[name][size];
};
};
var getPadding = function getPadding() {
return function (_ref2) {
var _tokens2;
var theme = _ref2.theme,
size = _ref2.size;
var tokens = (_tokens2 = {}, _defineProperty(_tokens2, SIZE_OPTIONS.SMALL, theme.orbit.paddingInputSmall), _defineProperty(_tokens2, SIZE_OPTIONS.NORMAL, theme.orbit.paddingInputNormal), _tokens2);
return rtlSpacing(tokens[size]);
};
};
var getDOMType = function getDOMType(type) {
if (type === TYPE_OPTIONS.PASSPORTID) {
return TYPE_OPTIONS.TEXT;
}
return type;
};
var Field = styled( /*#__PURE__*/React.forwardRef(function (_ref3, ref) {
var Component = _ref3.component,
className = _ref3.className,
children = _ref3.children,
spaceAfter = _ref3.spaceAfter,
theme = _ref3.theme,
props = _objectWithoutProperties(_ref3, ["component", "className", "children", "spaceAfter", "theme"]);
return /*#__PURE__*/React.createElement(Component, _extends({
className: className,
ref: ref
}, props), children);
})).withConfig({
displayName: "InputField__Field",
componentId: "sc-jjv066-0"
})(["", ""], function (_ref4) {
var theme = _ref4.theme,
$width = _ref4.$width;
return css(["font-family:", ";position:relative;display:block;flex:1 1 100%;width:", ";margin-bottom:", ";"], theme.orbit.fontFamily, $width, getSpacingToken);
}); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198
Field.defaultProps = {
theme: defaultTheme
};
export var FakeInput = styled(function (_ref5) {
var children = _ref5.children,
className = _ref5.className;
return /*#__PURE__*/React.createElement("div", {
className: className
}, children);
}).withConfig({
displayName: "InputField__FakeInput",
componentId: "sc-jjv066-1"
})(["width:100%;position:absolute;z-index:1;top:0;left:0;box-sizing:border-box;height:", ";box-shadow:inset 0 0 0 ", ";background-color:", ";font-size:", ";transition:all ", " ease-in-out;border-radius:6px;", ";"], getToken(TOKENS.heightInput), function (_ref6) {
var theme = _ref6.theme,
error = _ref6.error;
return "".concat(theme.orbit.borderWidthInput, " ").concat(error ? theme.orbit.borderColorInputError : theme.orbit.borderColorInput);
}, function (_ref7) {
var disabled = _ref7.disabled,
theme = _ref7.theme;
return disabled ? theme.orbit.backgroundInputDisabled : theme.orbit.backgroundInput;
}, getToken(TOKENS.fontSizeInput), function (_ref8) {
var theme = _ref8.theme;
return theme.orbit.durationFast;
}, mq.tablet(css(["border-radius:", ";"], function (_ref9) {
var theme = _ref9.theme;
return theme.orbit.borderRadiusNormal;
}))); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198
FakeInput.defaultProps = {
theme: defaultTheme
};
export var InputContainer = styled(function (_ref10) {
var children = _ref10.children,
className = _ref10.className,
labelRef = _ref10.labelRef;
return /*#__PURE__*/React.createElement("div", {
ref: labelRef,
className: className
}, children);
}).withConfig({
displayName: "InputField__InputContainer",
componentId: "sc-jjv066-2"
})(["display:flex;position:relative;flex-direction:row;align-items:center;justify-content:space-between;box-sizing:border-box;height:", ";border-radius:", ";color:", ";font-size:", ";cursor:", ";&:hover > ", "{", ";}"], getToken(TOKENS.heightInput), function (_ref11) {
var theme = _ref11.theme;
return theme.orbit.borderRadiusNormal;
}, function (_ref12) {
var disabled = _ref12.disabled,
theme = _ref12.theme;
return disabled ? theme.orbit.colorTextInputDisabled : theme.orbit.colorTextInput;
}, getToken(TOKENS.fontSizeInput), function (_ref13) {
var disabled = _ref13.disabled;
return disabled ? "not-allowed" : "text";
}, FakeInput, function (_ref14) {
var disabled = _ref14.disabled,
theme = _ref14.theme,
error = _ref14.error;
return !disabled && "box-shadow: inset 0 0 0 ".concat(theme.orbit.borderWidthInput, " ").concat(error ? theme.orbit.borderColorInputErrorHover : theme.orbit.borderColorInputHover);
}); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198
InputContainer.defaultProps = {
theme: defaultTheme
};
var StyledInlineLabel = styled.div.withConfig({
displayName: "InputField__StyledInlineLabel",
componentId: "sc-jjv066-3"
})(["height:100%;display:flex;align-items:center;pointer-events:none;justify-content:center;padding:", ";", "{margin-bottom:0;font-size:", ";line-height:normal;z-index:3;white-space:nowrap;}"], function (_ref15) {
var theme = _ref15.theme,
hasTags = _ref15.hasTags,
hasFeedback = _ref15.hasFeedback;
return rtlSpacing("0 0 0 ".concat(!hasTags && hasFeedback ? theme.orbit.spaceXXSmall : theme.orbit.spaceSmall));
}, FormLabel, getToken(TOKENS.fontSizeInput)); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198
StyledInlineLabel.defaultProps = {
theme: defaultTheme
};
export var Prefix = styled(function (_ref16) {
var children = _ref16.children,
className = _ref16.className,
iconRef = _ref16.iconRef;
return /*#__PURE__*/React.createElement("div", {
className: className,
ref: iconRef
}, children);
}).withConfig({
displayName: "InputField__Prefix",
componentId: "sc-jjv066-4"
})(["height:100%;color:", ";display:flex;align-items:center;pointer-events:none;justify-content:center;padding:", ";z-index:3;& > svg{color:", ";}& * svg,& > svg{width:", ";height:", ";}", "{color:", ";}"], function (_ref17) {
var theme = _ref17.theme;
return theme.orbit.colorTextInputPrefix;
}, function (_ref18) {
var theme = _ref18.theme;
return rtlSpacing("0 0 0 ".concat(theme.orbit.spaceSmall));
}, function (_ref19) {
var theme = _ref19.theme;
return theme.orbit.colorIconInput;
}, getToken(TOKENS.iconSize), getToken(TOKENS.iconSize), StyledButtonPrimitiveIconContainer, function (_ref20) {
var theme = _ref20.theme;
return theme.orbit.colorIconSecondary;
}); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198
Prefix.defaultProps = {
theme: defaultTheme
};
var Suffix = styled(function (_ref21) {
var children = _ref21.children,
className = _ref21.className;
return /*#__PURE__*/React.createElement("div", {
className: className
}, children);
}).withConfig({
displayName: "InputField__Suffix",
componentId: "sc-jjv066-5"
})(["height:", ";color:", ";display:flex;flex-shrink:0;align-items:center;justify-content:center;pointer-events:", ";z-index:3;", "{color:", ";}", "{height:16px;padding:", ";}"], getToken(TOKENS.heightInput), function (_ref22) {
var theme = _ref22.theme;
return theme.orbit.colorTextInputPrefix;
}, function (_ref23) {
var disabled = _ref23.disabled;
return disabled && "none";
}, StyledButtonPrimitiveIconContainer, function (_ref24) {
var theme = _ref24.theme;
return theme.orbit.colorIconSecondary;
}, StyledServiceLogo, function (_ref25) {
var theme = _ref25.theme;
return rtlSpacing("0 ".concat(theme.orbit.spaceSmall, " 0 0"));
}); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198
Suffix.defaultProps = {
theme: defaultTheme
};
export var Input = styled( /*#__PURE__*/React.forwardRef(function (_ref26, ref) {
var type = _ref26.type,
size = _ref26.size,
theme = _ref26.theme,
error = _ref26.error,
help = _ref26.help,
inlineLabel = _ref26.inlineLabel,
dataAttrs = _ref26.dataAttrs,
required = _ref26.required,
ariaLabelledby = _ref26.ariaLabelledby,
props = _objectWithoutProperties(_ref26, ["type", "size", "theme", "error", "help", "inlineLabel", "dataAttrs", "required", "ariaLabelledby"]);
return /*#__PURE__*/React.createElement("input", _extends({
type: getDOMType(type)
}, props, dataAttrs, {
ref: ref // aria-required is passed to make the field required for screen-reader
// we do not pass required field by reason, to avoid native browser message
,
"aria-required": required // in case when there is no label
,
"aria-labelledby": ariaLabelledby
}));
})).withConfig({
displayName: "InputField__Input",
componentId: "sc-jjv066-6"
})(["appearance:none;-webkit-text-fill-color:", ";font-family:", ";border:none;padding:", ";font-size:inherit;font-weight:", ";color:inherit;background-color:transparent;cursor:inherit;flex:1 1 20%;width:100%;height:100%;box-sizing:border-box;border-radius:", ";z-index:2;box-shadow:none;min-width:0;font-variant-numeric:", ";letter-spacing:", ";&::-webkit-inner-spin-button,&::-webkit-outer-spin-button{-webkit-appearance:none;margin:0;}&[data-com-onepassword-filled]{background-color:inherit !important;}&:focus{outline:none;& ~ ", "{", "}}&::placeholder{color:", ";opacity:1;}&:-ms-input-placeholder{color:", ";}&::-ms-input-placeholder{color:", ";}&::-ms-clear,&::-ms-reveal{display:none;}"], function (_ref27) {
var disabled = _ref27.disabled,
theme = _ref27.theme;
return disabled && theme.orbit.colorTextInputDisabled;
}, function (_ref28) {
var theme = _ref28.theme;
return theme.orbit.fontFamily;
}, getPadding(), function (_ref29) {
var inlineLabel = _ref29.inlineLabel,
theme = _ref29.theme;
return inlineLabel ? theme.orbit.fontWeightMedium : theme.orbit.fontWeightNormal;
}, function (_ref30) {
var theme = _ref30.theme;
return theme.orbit.borderRadiusNormal;
}, function (_ref31) {
var type = _ref31.type;
return type === TYPE_OPTIONS.PASSPORTID && "tabular-nums";
}, function (_ref32) {
var type = _ref32.type;
return type === TYPE_OPTIONS.PASSPORTID && "2px";
}, FakeInput, formElementFocus, function (_ref33) {
var theme = _ref33.theme;
return theme.orbit.colorPlaceholderInput;
}, function (_ref34) {
var theme = _ref34.theme;
return theme.orbit.colorPlaceholderInput;
}, function (_ref35) {
var theme = _ref35.theme;
return theme.orbit.colorPlaceholderInput;
}); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198
Input.defaultProps = {
theme: defaultTheme
};
var StyledIconWrapper = styled.span.withConfig({
displayName: "InputField__StyledIconWrapper",
componentId: "sc-jjv066-7"
})(["display:flex;"]);
var InputField = /*#__PURE__*/React.forwardRef(function (props, ref) {
var disabled = props.disabled,
_props$size = props.size,
size = _props$size === void 0 ? SIZE_OPTIONS.NORMAL : _props$size,
_props$type = props.type,
type = _props$type === void 0 ? TYPE_OPTIONS.TEXT : _props$type,
label = props.label,
inlineLabel = props.inlineLabel,
dataTest = props.dataTest,
required = props.required,
error = props.error,
name = props.name,
prefix = props.prefix,
onChange = props.onChange,
onFocus = props.onFocus,
onBlur = props.onBlur,
onSelect = props.onSelect,
onMouseUp = props.onMouseUp,
onMouseDown = props.onMouseDown,
onKeyUp = props.onKeyUp,
onKeyDown = props.onKeyDown,
placeholder = props.placeholder,
minValue = props.minValue,
maxValue = props.maxValue,
minLength = props.minLength,
maxLength = props.maxLength,
suffix = props.suffix,
help = props.help,
value = props.value,
tags = props.tags,
tabIndex = props.tabIndex,
readOnly = props.readOnly,
autoComplete = props.autoComplete,
autoFocus = props.autoFocus,
spaceAfter = props.spaceAfter,
id = props.id,
_props$width = props.width,
width = _props$width === void 0 ? "100%" : _props$width,
inputMode = props.inputMode,
insideInputGroup = props.insideInputGroup,
dataAttrs = props.dataAttrs;
var forID = useRandomId();
var inputId = id || forID;
var _useErrorTooltip = useErrorTooltip({
onFocus: onFocus
}),
tooltipShown = _useErrorTooltip.tooltipShown,
tooltipShownHover = _useErrorTooltip.tooltipShownHover,
setTooltipShown = _useErrorTooltip.setTooltipShown,
setTooltipShownHover = _useErrorTooltip.setTooltipShownHover,
labelRef = _useErrorTooltip.labelRef,
iconRef = _useErrorTooltip.iconRef,
handleFocus = _useErrorTooltip.handleFocus;
var shown = tooltipShown || tooltipShownHover;
var fieldRef = React.useRef(null);
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Field, {
component: label ? "label" : "div",
$width: width,
spaceAfter: spaceAfter,
ref: fieldRef,
htmlFor: label ? inputId : undefined
}, label && !inlineLabel && /*#__PURE__*/React.createElement(FormLabel, {
inlineLabel: inlineLabel,
filled: !!value,
required: required,
error: !!error,
help: !!help,
labelRef: labelRef,
iconRef: iconRef,
onMouseEnter: function onMouseEnter() {
return setTooltipShownHover(true);
},
onMouseLeave: function onMouseLeave() {
return setTooltipShownHover(false);
}
}, label), /*#__PURE__*/React.createElement(InputContainer, {
size: size,
disabled: disabled,
error: error,
labelRef: label ? null : labelRef
}, inlineLabel && !tags && (error || help) ? /*#__PURE__*/React.createElement(Prefix, {
size: size
}, help && !error && /*#__PURE__*/React.createElement(StyledIconWrapper, {
ref: iconRef
}, /*#__PURE__*/React.createElement(InformationCircle, {
color: "secondary",
size: "small"
})), error && /*#__PURE__*/React.createElement(StyledIconWrapper, {
ref: iconRef
}, /*#__PURE__*/React.createElement(AlertCircle, {
color: "critical",
size: "small"
}))) : prefix && /*#__PURE__*/React.createElement(Prefix, {
size: size
}, prefix), label && inlineLabel && /*#__PURE__*/React.createElement(StyledInlineLabel, {
hasTags: !!tags,
hasFeedback: !!(error || help),
ref: labelRef,
size: size
}, /*#__PURE__*/React.createElement(FormLabel, {
filled: !!value,
required: required,
error: !!error,
help: !!help,
inlineLabel: inlineLabel
}, label)), tags && /*#__PURE__*/React.createElement(InputTags, null, tags), /*#__PURE__*/React.createElement(Input, {
"data-test": dataTest,
required: required,
"data-state": insideInputGroup && typeof error === "undefined" ? undefined : getFieldDataState(!!error),
onChange: onChange,
onFocus: handleFocus,
onBlur: onBlur,
onKeyUp: onKeyUp,
onKeyDown: onKeyDown,
onSelect: onSelect,
onMouseUp: onMouseUp,
onMouseDown: onMouseDown,
name: name,
type: type,
value: value,
placeholder: placeholder,
disabled: disabled,
min: minValue,
max: maxValue,
minLength: minLength,
maxLength: maxLength,
size: size,
error: insideInputGroup ? undefined : error,
ref: ref,
tabIndex: tabIndex,
ariaLabelledby: !label ? inputId : null,
inlineLabel: inlineLabel,
readOnly: readOnly,
autoComplete: autoComplete,
autoFocus: autoFocus,
id: inputId,
inputMode: inputMode,
dataAttrs: dataAttrs
}), suffix && /*#__PURE__*/React.createElement(Suffix, {
size: size
}, suffix), /*#__PURE__*/React.createElement(FakeInput, {
size: size,
disabled: disabled,
error: error
})), !insideInputGroup && /*#__PURE__*/React.createElement(ErrorFormTooltip, {
help: help,
id: inputId,
shown: shown,
onShown: setTooltipShown,
error: error,
inputSize: size,
inlineLabel: inlineLabel,
referenceElement: inlineLabel && !tags ? iconRef : fieldRef
})));
});
InputField.displayName = "InputField";
export default InputField;