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.

320 lines (319 loc) 13.7 kB
"use strict"; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; exports.__esModule = true; exports.default = exports.Prefix = exports.InputContainer = exports.Input = exports.Field = exports.FakeInput = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var React = _interopRequireWildcard(require("react")); var _styledComponents = _interopRequireWildcard(require("styled-components")); var _defaultTheme = _interopRequireDefault(require("../defaultTheme")); var _consts = require("./consts"); var _ServiceLogo = require("../ServiceLogo"); var _rtl = require("../utils/rtl"); var _InputTags = _interopRequireDefault(require("./InputTags")); var _getSpacingToken = _interopRequireDefault(require("../common/getSpacingToken")); var _getFieldDataState = _interopRequireDefault(require("../common/getFieldDataState")); var _useRandomId = _interopRequireDefault(require("../hooks/useRandomId")); var _ErrorFormTooltip = _interopRequireDefault(require("../ErrorFormTooltip")); var _AlertCircle = _interopRequireDefault(require("../icons/AlertCircle")); var _InformationCircle = _interopRequireDefault(require("../icons/InformationCircle")); var _FormLabel = _interopRequireDefault(require("../FormLabel")); var _useErrorTooltip = _interopRequireDefault(require("../ErrorFormTooltip/hooks/useErrorTooltip")); var _formElementFocus = _interopRequireDefault(require("./helpers/formElementFocus")); var _ButtonPrimitiveIconContainer = require("../primitives/ButtonPrimitive/components/ButtonPrimitiveIconContainer"); var _mediaQuery = _interopRequireDefault(require("../utils/mediaQuery")); const getDOMType = type => { if (type === _consts.TYPE_OPTIONS.PASSPORTID) return _consts.TYPE_OPTIONS.TEXT; return type; }; const Field = (0, _styledComponents.default)( /*#__PURE__*/React.forwardRef(({ component: Component, className, children, spaceAfter, theme, $width, ...props }, ref) => { return ( /*#__PURE__*/ // @ts-expect-error TODO React.createElement(Component, (0, _extends2.default)({ className: className, ref: ref }, props), children) ); })).withConfig({ displayName: "InputField__Field", componentId: "sc-19cgljf-0" })(["", ""], ({ theme, $width }) => (0, _styledComponents.css)(["font-family:", ";position:relative;display:block;flex:1 1 100%;width:", ";margin-bottom:", ";"], theme.orbit.fontFamily, $width, _getSpacingToken.default)); exports.Field = Field; Field.defaultProps = { theme: _defaultTheme.default }; const FakeInput = (0, _styledComponents.default)(({ children, className }) => /*#__PURE__*/React.createElement("div", { className: className }, children)).withConfig({ displayName: "InputField__FakeInput", componentId: "sc-19cgljf-1" })(["", ""], ({ theme, error, disabled }) => (0, _styledComponents.css)(["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:", ";", ";"], theme.orbit.heightInputNormal, `${theme.orbit.borderWidthInput} ${error ? theme.orbit.borderColorInputError : theme.orbit.borderColorInput}`, disabled ? theme.orbit.backgroundInputDisabled : theme.orbit.backgroundInput, theme.orbit.fontSizeInputNormal, theme.orbit.durationFast, theme.orbit.borderRadiusLarge, _mediaQuery.default.tablet((0, _styledComponents.css)(["border-radius:", ";"], theme.orbit.borderRadiusNormal)))); exports.FakeInput = FakeInput; FakeInput.defaultProps = { theme: _defaultTheme.default }; const InputContainer = (0, _styledComponents.default)(({ children, className, labelRef }) => /*#__PURE__*/React.createElement("div", { ref: labelRef, className: className }, children)).withConfig({ displayName: "InputField__InputContainer", componentId: "sc-19cgljf-2" })(["", ""], ({ theme, disabled, error }) => (0, _styledComponents.css)(["display:flex;position:relative;flex-direction:row;align-items:center;justify-content:space-between;box-sizing:border-box;height:", ";color:", ";font-size:", ";cursor:", ";&:hover > ", "{", ";}"], theme.orbit.heightInputNormal, disabled ? theme.orbit.colorTextInputDisabled : theme.orbit.colorTextInput, theme.orbit.fontSizeInputNormal, disabled ? "not-allowed" : "text", FakeInput, !disabled && `box-shadow: inset 0 0 0 ${theme.orbit.borderWidthInput} ${error ? theme.orbit.borderColorInputErrorHover : theme.orbit.borderColorInputHover}`)); exports.InputContainer = InputContainer; InputContainer.defaultProps = { theme: _defaultTheme.default }; const StyledInlineLabel = _styledComponents.default.div.withConfig({ displayName: "InputField__StyledInlineLabel", componentId: "sc-19cgljf-3" })(["", ""], ({ theme, hasTags, hasFeedback }) => (0, _styledComponents.css)(["height:100%;display:flex;align-items:center;pointer-events:none;justify-content:center;padding:", ";", "{margin-bottom:0;font-size:", ";line-height:", ";z-index:3;white-space:nowrap;}"], (0, _rtl.rtlSpacing)(`0 0 0 ${!hasTags && hasFeedback ? theme.orbit.spaceXXSmall : theme.orbit.spaceSmall}`), _FormLabel.default, theme.orbit.fontSizeInputNormal, theme.orbit.lineHeightTextNormal)); StyledInlineLabel.defaultProps = { theme: _defaultTheme.default }; const Prefix = (0, _styledComponents.default)(({ children, className, iconRef }) => /*#__PURE__*/React.createElement("div", { className: className, ref: iconRef }, children)).withConfig({ displayName: "InputField__Prefix", componentId: "sc-19cgljf-4" })(["", ""], ({ theme }) => (0, _styledComponents.css)(["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:", ";}"], theme.orbit.colorTextInputPrefix, (0, _rtl.rtlSpacing)(`0 0 0 ${theme.orbit.spaceSmall}`), theme.orbit.colorIconInput, theme.orbit.widthIconNormal, theme.orbit.heightIconNormal, _ButtonPrimitiveIconContainer.StyledButtonPrimitiveIconContainer, theme.orbit.colorIconSecondary)); exports.Prefix = Prefix; Prefix.defaultProps = { theme: _defaultTheme.default }; const Suffix = (0, _styledComponents.default)(({ children, className }) => /*#__PURE__*/React.createElement("div", { className: className }, children)).withConfig({ displayName: "InputField__Suffix", componentId: "sc-19cgljf-5" })(["", ""], ({ theme, disabled }) => (0, _styledComponents.css)(["height:", ";color:", ";display:flex;flex-shrink:0;align-items:center;justify-content:center;pointer-events:", ";z-index:3;", "{color:", ";}", "{height:16px;padding:", ";}"], theme.orbit.heightInputNormal, theme.orbit.colorTextInputPrefix, disabled && "none", _ButtonPrimitiveIconContainer.StyledButtonPrimitiveIconContainer, theme.orbit.colorIconSecondary, _ServiceLogo.StyledServiceLogo, (0, _rtl.rtlSpacing)(`0 ${theme.orbit.spaceSmall} 0 0`))); Suffix.defaultProps = { theme: _defaultTheme.default }; const Input = _styledComponents.default.input.withConfig({ displayName: "InputField__Input", componentId: "sc-19cgljf-6" })(["", ""], ({ theme, disabled, inlineLabel, type }) => (0, _styledComponents.css)(["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;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-clear,&::-ms-reveal{display:none;}"], disabled && theme.orbit.colorTextInputDisabled, theme.orbit.fontFamily, theme.orbit.paddingInputNormal, inlineLabel ? theme.orbit.fontWeightMedium : theme.orbit.fontWeightNormal, type === _consts.TYPE_OPTIONS.PASSPORTID && "tabular-nums", type === _consts.TYPE_OPTIONS.PASSPORTID && "2px", FakeInput, _formElementFocus.default, theme.orbit.colorPlaceholderInput, theme.orbit.colorPlaceholderInput)); exports.Input = Input; Input.defaultProps = { theme: _defaultTheme.default }; const StyledIconWrapper = _styledComponents.default.span.withConfig({ displayName: "InputField__StyledIconWrapper", componentId: "sc-19cgljf-7" })(["display:flex;"]); const InputField = /*#__PURE__*/React.forwardRef((props, ref) => { const { disabled, type = _consts.TYPE_OPTIONS.TEXT, label, inlineLabel, dataTest, required, error, name, prefix, onChange, onFocus, onBlur, onSelect, onMouseUp, onMouseDown, onKeyUp, onKeyDown, placeholder, minValue = -Infinity, maxValue = Infinity, minLength, maxLength, suffix, help, helpClosable = true, value, tags, tabIndex, readOnly, list, autoComplete, ariaAutocomplete, ariaActiveDescendant, ariaHasPopup, ariaExpanded, ariaControls, autoFocus, spaceAfter, id, width = "100%", inputMode, insideInputGroup, dataAttrs, role } = props; const forID = (0, _useRandomId.default)(); const inputId = id || forID; const hasTooltip = Boolean(error || help); const { tooltipShown, tooltipShownHover, setTooltipShown, setTooltipShownHover, labelRef, iconRef, handleFocus } = (0, _useErrorTooltip.default)({ onFocus, hasTooltip }); const shown = tooltipShown || tooltipShownHover; const fieldRef = React.useRef(null); return /*#__PURE__*/React.createElement(Field, { component: label ? "label" : "div", $width: width, spaceAfter: spaceAfter, ref: fieldRef, htmlFor: label ? inputId : undefined // enable tooltip on hover, if it's disabled , onMouseEnter: () => disabled && inlineLabel ? setTooltipShownHover(true) : undefined, onMouseLeave: () => disabled && inlineLabel ? setTooltipShownHover(false) : undefined }, label && !inlineLabel && /*#__PURE__*/React.createElement(_FormLabel.default, { inlineLabel: inlineLabel, filled: !!value, required: required, error: !!error, help: !!help, labelRef: labelRef, iconRef: iconRef, onMouseEnter: () => setTooltipShownHover(true), onMouseLeave: () => setTooltipShownHover(false) }, label), /*#__PURE__*/React.createElement(InputContainer, { disabled: disabled, error: error, labelRef: label ? null : labelRef }, inlineLabel && !tags && (error || help) ? /*#__PURE__*/React.createElement(Prefix, null, help && !error && /*#__PURE__*/React.createElement(StyledIconWrapper, { ref: iconRef }, /*#__PURE__*/React.createElement(_InformationCircle.default, { color: "info", size: "small" })), error && /*#__PURE__*/React.createElement(StyledIconWrapper, { ref: iconRef }, /*#__PURE__*/React.createElement(_AlertCircle.default, { color: "critical", size: "small" }))) : prefix && /*#__PURE__*/React.createElement(Prefix, null, prefix), label && inlineLabel && /*#__PURE__*/React.createElement(StyledInlineLabel, { hasTags: !!tags, hasFeedback: !!(error || help), ref: labelRef }, /*#__PURE__*/React.createElement(_FormLabel.default, { filled: !!value, required: required, error: !!error, help: !!help, inlineLabel: inlineLabel }, label)), tags && /*#__PURE__*/React.createElement(_InputTags.default, null, tags), /*#__PURE__*/React.createElement(Input, (0, _extends2.default)({ "data-test": dataTest, required: required, "data-state": insideInputGroup && typeof error === "undefined" ? undefined : (0, _getFieldDataState.default)(!!error), onChange: onChange, onFocus: handleFocus, onBlur: onBlur, onKeyUp: onKeyUp, onKeyDown: onKeyDown, onSelect: onSelect, onMouseUp: onMouseUp, onMouseDown: onMouseDown, name: name, type: getDOMType(type), value: value, placeholder: placeholder, disabled: disabled, min: minValue, max: maxValue, minLength: minLength, maxLength: maxLength, error: insideInputGroup ? undefined : error, ref: ref, tabIndex: tabIndex !== undefined ? Number(tabIndex) : undefined, list: list, "aria-labelledby": !label ? inputId : undefined, "aria-describedby": shown ? `${inputId}-feedback` : undefined, "aria-invalid": error ? true : undefined, "aria-autocomplete": ariaAutocomplete, "aria-haspopup": ariaHasPopup, "aria-expanded": ariaExpanded, "aria-controls": ariaControls, "aria-activedescendant": ariaActiveDescendant, inlineLabel: inlineLabel, readOnly: readOnly, autoCapitalize: "off", autoCorrect: "off", role: role, autoComplete: autoComplete, autoFocus: autoFocus, id: inputId, inputMode: inputMode }, dataAttrs)), suffix && /*#__PURE__*/React.createElement(Suffix, null, suffix), /*#__PURE__*/React.createElement(FakeInput, { disabled: disabled, error: error })), !insideInputGroup && hasTooltip && /*#__PURE__*/React.createElement(_ErrorFormTooltip.default, { help: help, id: `${inputId}-feedback`, shown: shown, helpClosable: helpClosable, onShown: setTooltipShown, error: error, inlineLabel: inlineLabel, referenceElement: inlineLabel && !tags ? iconRef : fieldRef })); }); InputField.displayName = "InputField"; var _default = InputField; exports.default = _default;