@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.
188 lines (177 loc) • 7.28 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import * as React from "react";
import styled, { css } from "styled-components";
import defaultTheme from "../defaultTheme";
import ErrorFormTooltip from "../ErrorFormTooltip";
import FormLabel from "../FormLabel";
import { SIZE_OPTIONS, RESIZE_OPTIONS } from "./consts";
import { rtlSpacing } from "../utils/rtl";
import getSpacingToken from "../common/getSpacingToken";
import useErrorTooltip from "../ErrorFormTooltip/hooks/useErrorTooltip";
import formElementFocus from "../InputField/helpers/formElementFocus";
import getFieldDataState from "../common/getFieldDataState";
import mq from "../utils/mediaQuery";
var Field = styled.label.withConfig({
displayName: "Textarea__Field",
componentId: "sc-1ezoqgy-0"
})(["font-family:", ";display:flex;width:100%;position:relative;height:", ";flex-direction:column;flex:", ";margin-bottom:", ";"], function (_ref) {
var theme = _ref.theme;
return theme.orbit.fontFamily;
}, function (_ref2) {
var fullHeight = _ref2.fullHeight;
return fullHeight && "100%";
}, function (_ref3) {
var fullHeight = _ref3.fullHeight;
return fullHeight && "1";
}, getSpacingToken); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198
Field.defaultProps = {
theme: defaultTheme
};
var getFontSize = function getFontSize(_ref4) {
var _tokens;
var theme = _ref4.theme,
size = _ref4.size;
var tokens = (_tokens = {}, _defineProperty(_tokens, SIZE_OPTIONS.SMALL, theme.orbit.fontSizeInputSmall), _defineProperty(_tokens, SIZE_OPTIONS.NORMAL, theme.orbit.fontSizeInputNormal), _tokens);
return tokens[size];
};
var getLineHeight = function getLineHeight(_ref5) {
var _tokens2;
var theme = _ref5.theme,
size = _ref5.size;
var tokens = (_tokens2 = {}, _defineProperty(_tokens2, SIZE_OPTIONS.SMALL, theme.orbit.lineHeightTextSmall), _defineProperty(_tokens2, SIZE_OPTIONS.NORMAL, theme.orbit.lineHeightTextNormal), _tokens2);
return tokens[size];
};
var getPadding = function getPadding(_ref6) {
var _tokens3;
var theme = _ref6.theme,
size = _ref6.size;
var tokens = (_tokens3 = {}, _defineProperty(_tokens3, SIZE_OPTIONS.SMALL, theme.orbit.paddingTextareaSmall), _defineProperty(_tokens3, SIZE_OPTIONS.NORMAL, theme.orbit.paddingTextareaNormal), _tokens3);
return rtlSpacing(tokens[size]);
};
var StyledTextArea = styled.textarea.withConfig({
displayName: "Textarea__StyledTextArea",
componentId: "sc-1ezoqgy-1"
})(["appearance:none;box-sizing:border-box;display:block;width:100%;height:", ";padding:", ";box-shadow:inset 0 0 0 ", ";background-color:", ";color:", ";font-size:", ";line-height:", ";cursor:", ";font-family:", ";resize:", ";transition:box-shadow ", " ease-in-out;min-height:44px;border-radius:6px;", ";flex:", ";border:1px solid transparent;overflow:auto;&::placeholder{color:", ";}&:hover{box-shadow:", ";}&:focus{", " outline:none;}"], function (_ref7) {
var fullHeight = _ref7.fullHeight;
return fullHeight && "100%";
}, getPadding, function (_ref8) {
var theme = _ref8.theme,
error = _ref8.error;
return "".concat(theme.orbit.borderWidthInput, " ").concat(error ? theme.orbit.borderColorInputError : theme.orbit.borderColorInput);
}, function (_ref9) {
var disabled = _ref9.disabled,
theme = _ref9.theme;
return disabled ? theme.orbit.backgroundInputDisabled : theme.orbit.backgroundInput;
}, function (_ref10) {
var disabled = _ref10.disabled,
theme = _ref10.theme;
return disabled ? theme.orbit.colorTextInputDisabled : theme.orbit.colorTextInput;
}, getFontSize, getLineHeight, function (_ref11) {
var disabled = _ref11.disabled;
return disabled ? "not-allowed" : "text";
}, function (_ref12) {
var theme = _ref12.theme;
return theme.orbit.fontFamily;
}, function (_ref13) {
var resize = _ref13.resize;
return resize;
}, function (_ref14) {
var theme = _ref14.theme;
return theme.orbit.durationFast;
}, mq.tablet(css(["border-radius:", ";"], function (_ref15) {
var theme = _ref15.theme;
return theme.orbit.borderRadiusNormal;
})), function (_ref16) {
var fullHeight = _ref16.fullHeight;
return fullHeight && "1";
}, function (_ref17) {
var theme = _ref17.theme;
return theme.orbit.colorPlaceholderInput;
}, function (_ref18) {
var disabled = _ref18.disabled,
theme = _ref18.theme,
error = _ref18.error;
return !disabled && "inset 0 0 0 ".concat(theme.orbit.borderWidthInput, " ").concat(error ? theme.orbit.borderColorInputErrorHover : theme.orbit.borderColorInputHover);
}, formElementFocus); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198
StyledTextArea.defaultProps = {
theme: defaultTheme
};
var Textarea = /*#__PURE__*/React.forwardRef(function (props, ref) {
var _props$size = props.size,
size = _props$size === void 0 ? SIZE_OPTIONS.NORMAL : _props$size,
disabled = props.disabled,
_props$resize = props.resize,
resize = _props$resize === void 0 ? RESIZE_OPTIONS.VERTICAL : _props$resize,
dataTest = props.dataTest,
spaceAfter = props.spaceAfter,
fullHeight = props.fullHeight,
value = props.value,
label = props.label,
name = props.name,
error = props.error,
placeholder = props.placeholder,
maxLength = props.maxLength,
onChange = props.onChange,
onFocus = props.onFocus,
onBlur = props.onBlur,
tabIndex = props.tabIndex,
help = props.help,
rows = props.rows,
readOnly = props.readOnly;
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;
return /*#__PURE__*/React.createElement(Field, {
fullHeight: fullHeight,
spaceAfter: spaceAfter,
ref: labelRef
}, label && /*#__PURE__*/React.createElement(FormLabel, {
filled: !!value,
error: !!error,
help: !!help,
disabled: disabled,
iconRef: iconRef,
onMouseEnter: function onMouseEnter() {
return setTooltipShownHover(true);
},
onMouseLeave: function onMouseLeave() {
return setTooltipShownHover(false);
}
}, label), /*#__PURE__*/React.createElement(StyledTextArea, {
"data-state": getFieldDataState(!!error),
"data-test": dataTest,
name: name,
value: value,
size: size,
fullHeight: fullHeight,
disabled: disabled,
error: error,
placeholder: placeholder,
maxLength: maxLength,
onChange: onChange,
rows: rows,
onFocus: handleFocus,
onBlur: onBlur,
resize: resize,
tabIndex: tabIndex,
readOnly: readOnly,
ref: ref
}), /*#__PURE__*/React.createElement(ErrorFormTooltip, {
help: help,
inputSize: size,
error: error,
onShown: setTooltipShown,
shown: shown,
referenceElement: labelRef
}));
});
Textarea.displayName = "Textarea";
export default Textarea;