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.

351 lines (340 loc) 14.4 kB
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 FormFeedback from "../FormFeedback"; import DefaultFormLabel from "../FormLabel"; import { StyledServiceLogo } from "../ServiceLogo"; import { rtlSpacing } from "../utils/rtl"; import InputTags from "./InputTags"; import getSpacingToken from "../common/getSpacingToken"; import getFieldDataState from "../common/getFieldDataState"; import randomID from "../utils/randomID"; 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(function (_ref3) { 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 }, props), children); }).withConfig({ displayName: "InputField__Field", componentId: "sc-4opay-0" })(["font-family:", ";position:relative;display:block;z-index:2;flex:1 1 100%;width:100%;margin-bottom:", ";"], function (_ref4) { var theme = _ref4.theme; return theme.orbit.fontFamily; }, getSpacingToken); 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-4opay-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; }))); FakeInput.defaultProps = { theme: defaultTheme }; export var InputContainer = styled(function (_ref10) { var children = _ref10.children, className = _ref10.className; return /*#__PURE__*/React.createElement("div", { className: className }, children); }).withConfig({ displayName: "InputField__InputContainer", componentId: "sc-4opay-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); }); InputContainer.defaultProps = { theme: defaultTheme }; var StyledInlineLabel = styled.div.withConfig({ displayName: "InputField__StyledInlineLabel", componentId: "sc-4opay-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; return rtlSpacing("0 0 0 ".concat(theme.orbit.spaceSmall)); }, DefaultFormLabel, getToken(TOKENS.fontSizeInput)); StyledInlineLabel.defaultProps = { theme: defaultTheme }; export var Prefix = styled(function (_ref16) { var children = _ref16.children, className = _ref16.className; return /*#__PURE__*/React.createElement("div", { className: className }, children); }).withConfig({ displayName: "InputField__Prefix", componentId: "sc-4opay-4" })(["height:100%;color:", ";display:flex;align-items:center;pointer-events:none;justify-content:center;padding:", ";z-index:3;& > svg{width:", ";height:", ";color:", ";}", "{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)); }, getToken(TOKENS.iconSize), getToken(TOKENS.iconSize), function (_ref19) { var theme = _ref19.theme; return theme.orbit.colorIconInput; }, StyledButtonPrimitiveIconContainer, function (_ref20) { var theme = _ref20.theme; return theme.orbit.colorIconSecondary; }); 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-4opay-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")); }); 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, props = _objectWithoutProperties(_ref26, ["type", "size", "theme", "error", "help", "inlineLabel", "dataAttrs"]); return /*#__PURE__*/React.createElement("input", _extends({ type: getDOMType(type) }, props, dataAttrs, { ref: ref })); })).withConfig({ displayName: "InputField__Input", componentId: "sc-4opay-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;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; return disabled && "inherit"; }, 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; }); Input.defaultProps = { theme: defaultTheme }; var FormLabel = function FormLabel(_ref36) { var label = _ref36.label, isFilled = _ref36.isFilled, required = _ref36.required; return /*#__PURE__*/React.createElement(DefaultFormLabel, { filled: isFilled, required: required }, label); }; 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, inputMode = props.inputMode, dataAttrs = props.dataAttrs; var forID = React.useMemo(function () { return id || (label ? randomID("inputFieldID") : undefined); }, [id, label]); return /*#__PURE__*/React.createElement(Field, { component: label ? "label" : "div", spaceAfter: spaceAfter, htmlFor: label ? forID : undefined }, label && !inlineLabel && /*#__PURE__*/React.createElement(FormLabel, { label: label, isFilled: !!value, required: required }), /*#__PURE__*/React.createElement(InputContainer, { size: size, disabled: disabled, error: error }, prefix && /*#__PURE__*/React.createElement(Prefix, { size: size }, prefix), label && inlineLabel && /*#__PURE__*/React.createElement(StyledInlineLabel, { size: size }, /*#__PURE__*/React.createElement(FormLabel, { label: label, isFilled: !!value, required: required })), tags && /*#__PURE__*/React.createElement(InputTags, null, tags), /*#__PURE__*/React.createElement(Input, { "data-test": dataTest, "data-state": getFieldDataState(!!error), onChange: onChange, onFocus: onFocus, 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: error, ref: ref, tabIndex: tabIndex, inlineLabel: inlineLabel, readOnly: readOnly, autoComplete: autoComplete, autoFocus: autoFocus, id: forID, inputMode: inputMode, dataAttrs: dataAttrs }), suffix && /*#__PURE__*/React.createElement(Suffix, { size: size }, suffix), /*#__PURE__*/React.createElement(FakeInput, { size: size, disabled: disabled, error: error })), /*#__PURE__*/React.createElement(FormFeedback, { help: help, error: error })); }); InputField.displayName = "InputField"; export default InputField;