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.

215 lines (181 loc) 7.89 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default; exports.__esModule = true; exports.default = void 0; var React = _interopRequireWildcard(require("react")); var _styledComponents = _interopRequireWildcard(require("styled-components")); var _defaultTheme = _interopRequireDefault(require("../../defaultTheme")); var _FormLabel = _interopRequireDefault(require("../../FormLabel")); var _InputField = require("../InputField"); var _Select = require("../Select"); var _FormFeedback = _interopRequireDefault(require("../FormFeedback")); var _consts = require("./consts"); var _rtl = require("../../utils/rtl"); var _getSpacingToken = _interopRequireDefault(require("../../common/getSpacingToken")); var _randomID = _interopRequireDefault(require("../../utils/randomID")); var _formElementFocus = _interopRequireDefault(require("../InputField/helpers/formElementFocus")); var _mediaQuery = _interopRequireDefault(require("../../utils/mediaQuery")); const getToken = name => ({ theme, size }) => { const tokens = { [_consts.TOKENS.height]: { [_consts.SIZE_OPTIONS.SMALL]: theme.orbit.heightInputSmall, [_consts.SIZE_OPTIONS.NORMAL]: theme.orbit.heightInputNormal }, [_consts.TOKENS.heightLine]: { [_consts.SIZE_OPTIONS.SMALL]: "16px", [_consts.SIZE_OPTIONS.NORMAL]: "24px" } }; return tokens[name][size]; }; const FakeGroup = (0, _styledComponents.default)(({ children, className }) => /*#__PURE__*/React.createElement("span", { className: className }, children)).withConfig({ displayName: "InputGroup__FakeGroup", componentId: "sc-ed1mas-0" })(["", ""], ({ theme, error, disabled }) => (0, _styledComponents.css)(["width:100%;display:block;position:absolute;top:0px;left:0;z-index:1;box-sizing:border-box;height:", ";box-shadow:", ";box-shadow:", ";", ";background-color:", ";font-size:", ";transition:box-shadow ", " ease-in-out;border-radius:6px;", ";&:hover{box-shadow:inset 0 0 0 ", ";}"], getToken(_consts.TOKENS.height), `inset 0 0 0 ${theme.orbit.borderWidthInput} ${theme.orbit.borderColorInput}`, error && `inset 0 0 0 ${theme.orbit.borderWidthInput} ${theme.orbit.borderColorInputError}`, ({ active }) => active && _formElementFocus.default, disabled ? theme.orbit.backgroundInputDisabled : theme.orbit.backgroundInput, theme.orbit.fontSizeInputNormal, theme.orbit.durationFast, _mediaQuery.default.tablet((0, _styledComponents.css)(["border-radius:", ";"], theme.orbit.borderRadiusNormal)), `${theme.orbit.borderWidthInput} ${error ? theme.orbit.borderColorInputErrorHover : theme.orbit.borderColorInputHover}`)); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 FakeGroup.defaultProps = { theme: _defaultTheme.default }; const StyledChildren = _styledComponents.default.div.withConfig({ displayName: "InputGroup__StyledChildren", componentId: "sc-ed1mas-1" })(["display:flex;position:relative;"]); const StyledChild = _styledComponents.default.div.withConfig({ displayName: "InputGroup__StyledChild", componentId: "sc-ed1mas-2" })(["flex:", ";padding:", ";:last-child{padding:0;}"], ({ flex }) => flex, ({ theme }) => (0, _rtl.rtlSpacing)(`0 ${theme.orbit.spaceXSmall} 0 0`)); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 StyledChild.defaultProps = { theme: _defaultTheme.default }; const StyledInputGroup = (0, _styledComponents.default)(({ children, className, dataTest, role, ariaLabelledby, forwardRef, dataState }) => /*#__PURE__*/React.createElement("div", { "data-state": dataState, ref: forwardRef, className: className, "data-test": dataTest, role: role, "aria-labelledby": ariaLabelledby }, children)).withConfig({ displayName: "InputGroup__StyledInputGroup", componentId: "sc-ed1mas-3" })(["display:flex;width:100%;flex-direction:column;position:relative;margin-bottom:", ";", "{", "{box-shadow:none;background-color:transparent;display:none;align-items:center;justify-content:flex-end;}", "{background-color:transparent;> select{box-shadow:none;background-color:transparent;&:focus{box-shadow:none;}}}", ":after,", ":after{content:\" \";position:absolute;top:50%;transform:translateY(-50%);", ":0;height:", ";width:1px;background-color:", ";transition:background-color ", " ease-in-out;display:block;z-index:2;}&:last-of-type{", ":after,", ":after{content:none;}}}", " ", "{display:", ";}", ":focus ~ ", "{box-shadow:none;}"], _getSpacingToken.default, StyledChild, _InputField.FakeInput, _Select.SelectContainer, _InputField.InputContainer, _Select.SelectContainer, _rtl.right, getToken(_consts.TOKENS.heightLine), ({ theme, error, active }) => error && !active ? theme.orbit.borderColorInputError : theme.orbit.borderColorInput, ({ theme }) => theme.orbit.durationFast, _InputField.InputContainer, _Select.SelectContainer, StyledChild, _FormLabel.default, ({ label }) => label && "none", _InputField.Input, _InputField.FakeInput); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 StyledInputGroup.defaultProps = { theme: _defaultTheme.default }; const findPropInChild = (propToFind, children) => { return React.Children.toArray(children).map(el => { if (el.props && typeof el.props[propToFind] !== "undefined") return el.props[propToFind]; return null; }).filter(el => el !== null && el !== ""); }; const InputGroup = ({ children, label, flex = "0 1 auto", size = _consts.SIZE_OPTIONS.NORMAL, help, error, dataTest, spaceAfter = "medium", onFocus, onBlur, onChange }) => { const [active, setActive] = React.useState(false); const [filled, setFilled] = React.useState(false); const inputID = React.useMemo(() => (0, _randomID.default)("inputGroupID"), []); const isFilled = React.useCallback(() => setFilled(findPropInChild("value", children).length === React.Children.toArray(children).length), [children]); React.useEffect(() => { isFilled(); }, [isFilled]); const handleFocus = ev => { setActive(true); if (onFocus) { onFocus(ev); } }; const handleBlur = ev => { isFilled(); setActive(false); if (onBlur) { onBlur(ev); } }; const handleChange = ev => { isFilled(); if (onChange) { onChange(ev); } }; return /*#__PURE__*/React.createElement(StyledInputGroup, { label: label, error: error, active: active, size: size, dataTest: dataTest, spaceAfter: spaceAfter, role: "group", ariaLabelledby: label && inputID }, label && /*#__PURE__*/React.createElement(_FormLabel.default, { filled: filled, id: inputID }, label), /*#__PURE__*/React.createElement(StyledChildren, null, React.Children.map(children, (item, key) => { // either array, array with one length or string // if it's not defined, use the first or string const childFlex = Array.isArray(flex) && flex.length !== 1 ? flex[key] || flex[0] : flex; return /*#__PURE__*/React.createElement(StyledChild, { flex: childFlex }, /*#__PURE__*/React.cloneElement(item, { size, label: undefined, help: undefined, error: undefined, onChange: item.props.onChange != null ? item.props.onChange : handleChange, onBlur: item.props.onBlur != null ? item.props.onChange : handleBlur, onFocus: item.props.onFocus != null ? item.props.onFocus : handleFocus })); }), /*#__PURE__*/React.createElement(FakeGroup, { label: label, error: error, active: active, size: size })), /*#__PURE__*/React.createElement(_FormFeedback.default, { error: error, help: help })); }; var _default = InputGroup; exports.default = _default;