@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.
236 lines (219 loc) • 9.15 kB
JavaScript
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import * as React from "react";
import styled, { css } from "styled-components";
import defaultTheme from "../defaultTheme";
import FormLabel from "../FormLabel";
import { FakeInput, Input, InputContainer } from "../InputField";
import { SelectContainer } from "../Select";
import FormFeedback from "../FormFeedback";
import { SIZE_OPTIONS, TOKENS } from "./consts";
import { right, rtlSpacing } from "../utils/rtl";
import getSpacingToken from "../common/getSpacingToken";
import randomID from "../utils/randomID";
import formElementFocus from "../InputField/helpers/formElementFocus";
import mq from "../utils/mediaQuery";
var useState = React.useState,
useEffect = React.useEffect,
useCallback = React.useCallback;
var getToken = function getToken(name) {
return function (_ref) {
var _TOKENS$height, _TOKENS$heightLine, _tokens;
var theme = _ref.theme,
size = _ref.size;
var tokens = (_tokens = {}, _defineProperty(_tokens, TOKENS.height, (_TOKENS$height = {}, _defineProperty(_TOKENS$height, SIZE_OPTIONS.SMALL, theme.orbit.heightInputSmall), _defineProperty(_TOKENS$height, SIZE_OPTIONS.NORMAL, theme.orbit.heightInputNormal), _TOKENS$height)), _defineProperty(_tokens, TOKENS.heightLine, (_TOKENS$heightLine = {}, _defineProperty(_TOKENS$heightLine, SIZE_OPTIONS.SMALL, "16px"), _defineProperty(_TOKENS$heightLine, SIZE_OPTIONS.NORMAL, "24px"), _TOKENS$heightLine)), _tokens);
return tokens[name][size];
};
};
var FakeGroup = styled(function (_ref2) {
var children = _ref2.children,
className = _ref2.className;
return /*#__PURE__*/React.createElement("span", {
className: className
}, children);
}).withConfig({
displayName: "InputGroup__FakeGroup",
componentId: "cyuwoi-0"
})(["width:100%;display:block;position:absolute;top:0;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(TOKENS.height), function (_ref3) {
var theme = _ref3.theme;
return "inset 0 0 0 ".concat(theme.orbit.borderWidthInput, " ").concat(theme.orbit.borderColorInput);
}, function (_ref4) {
var theme = _ref4.theme,
error = _ref4.error;
return error && "inset 0 0 0 ".concat(theme.orbit.borderWidthInput, " ").concat(theme.orbit.borderColorInputError);
}, function (_ref5) {
var active = _ref5.active;
return active && formElementFocus;
}, function (_ref6) {
var disabled = _ref6.disabled,
theme = _ref6.theme;
return disabled ? theme.orbit.backgroundInputDisabled : theme.orbit.backgroundInput;
}, function (_ref7) {
var theme = _ref7.theme;
return theme.orbit.fontSizeInputNormal;
}, 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;
})), function (_ref10) {
var theme = _ref10.theme,
error = _ref10.error;
return "".concat(theme.orbit.borderWidthInput, " ").concat(error ? theme.orbit.borderColorInputErrorHover : theme.orbit.borderColorInputHover);
});
FakeGroup.defaultProps = {
theme: defaultTheme
};
var StyledChildren = styled.div.withConfig({
displayName: "InputGroup__StyledChildren",
componentId: "cyuwoi-1"
})(["display:flex;position:relative;"]);
var StyledChild = styled.div.withConfig({
displayName: "InputGroup__StyledChild",
componentId: "cyuwoi-2"
})(["flex:", ";padding:", ";:last-child{padding:0;}"], function (_ref11) {
var flex = _ref11.flex;
return flex;
}, function (_ref12) {
var theme = _ref12.theme;
return rtlSpacing("0 ".concat(theme.orbit.spaceXSmall, " 0 0"));
});
StyledChild.defaultProps = {
theme: defaultTheme
};
var StyledInputGroup = styled(function (_ref13) {
var children = _ref13.children,
className = _ref13.className,
dataTest = _ref13.dataTest,
role = _ref13.role,
ariaLabelledby = _ref13.ariaLabelledby;
return /*#__PURE__*/React.createElement("div", {
className: className,
"data-test": dataTest,
role: role,
"aria-labelledby": ariaLabelledby
}, children);
}).withConfig({
displayName: "InputGroup__StyledInputGroup",
componentId: "cyuwoi-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, StyledChild, FakeInput, SelectContainer, InputContainer, SelectContainer, right, getToken(TOKENS.heightLine), function (_ref14) {
var theme = _ref14.theme,
error = _ref14.error,
active = _ref14.active;
return error && !active ? theme.orbit.borderColorInputError : theme.orbit.borderColorInput;
}, function (_ref15) {
var theme = _ref15.theme;
return theme.orbit.durationFast;
}, InputContainer, SelectContainer, StyledChild, FormLabel, function (_ref16) {
var label = _ref16.label;
return label && "none";
}, Input, FakeInput);
StyledInputGroup.defaultProps = {
theme: defaultTheme
};
var findPropInChild = function findPropInChild(propToFind, children) {
return React.Children.toArray(children).map(function (el) {
if (el.props && typeof el.props[propToFind] !== "undefined") return el.props[propToFind];
return null;
}).filter(function (el) {
return el !== null && el !== "";
});
};
var InputGroup = function InputGroup(_ref17) {
var children = _ref17.children,
label = _ref17.label,
_ref17$flex = _ref17.flex,
flex = _ref17$flex === void 0 ? "0 1 auto" : _ref17$flex,
_ref17$size = _ref17.size,
size = _ref17$size === void 0 ? SIZE_OPTIONS.NORMAL : _ref17$size,
help = _ref17.help,
error = _ref17.error,
dataTest = _ref17.dataTest,
spaceAfter = _ref17.spaceAfter,
onFocus = _ref17.onFocus,
onBlur = _ref17.onBlur,
onChange = _ref17.onChange;
var _useState = useState(false),
_useState2 = _slicedToArray(_useState, 2),
active = _useState2[0],
setActive = _useState2[1];
var _useState3 = useState(false),
_useState4 = _slicedToArray(_useState3, 2),
filled = _useState4[0],
setFilled = _useState4[1];
var inputID = React.useMemo(function () {
return randomID("inputGroupID");
}, []);
var isFilled = useCallback(function () {
return setFilled(findPropInChild("value", children).length === React.Children.toArray(children).length);
}, [children]);
useEffect(function () {
isFilled();
}, [isFilled]);
var handleFocus = function handleFocus(ev) {
setActive(true);
if (onFocus) {
onFocus(ev);
}
};
var handleBlur = function handleBlur(ev) {
isFilled();
setActive(false);
if (onBlur) {
onBlur(ev);
}
};
var handleChange = function 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, {
filled: filled,
id: inputID
}, label), /*#__PURE__*/React.createElement(StyledChildren, null, React.Children.map(children, function (item, key) {
// either array, array with one length or string
// if it's not defined, use the first or string
var childFlex = Array.isArray(flex) && flex.length !== 1 ? flex[key] || flex[0] : flex;
return /*#__PURE__*/React.createElement(StyledChild, {
flex: childFlex
}, /*#__PURE__*/React.cloneElement(item, {
ref: function ref(node) {
// Call the original ref, if any
var ref = item.ref;
if (typeof ref === "function") {
ref(node);
} else if (ref !== null) {
ref.current = node;
}
},
size: 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, {
error: error,
help: help
}));
};
export default InputGroup;