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.

292 lines (241 loc) 13.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var React = _interopRequireWildcard(require("react")); var _styledComponents = _interopRequireDefault(require("styled-components")); var _defaultTokens = _interopRequireDefault(require("../defaultTokens")); var _FormLabel = _interopRequireDefault(require("../FormLabel")); var _InputField = require("../InputField"); var _Select = require("../Select"); var _FormFeedback = _interopRequireDefault(require("../FormFeedback")); var _consts = require("./consts"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } 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, _consts.TOKENS.height, (_TOKENS$height = {}, _defineProperty(_TOKENS$height, _consts.SIZE_OPTIONS.SMALL, theme.orbit.heightInputSmall), _defineProperty(_TOKENS$height, _consts.SIZE_OPTIONS.NORMAL, theme.orbit.heightInputNormal), _TOKENS$height)), _defineProperty(_tokens, _consts.TOKENS.heightLine, (_TOKENS$heightLine = {}, _defineProperty(_TOKENS$heightLine, _consts.SIZE_OPTIONS.SMALL, "16px"), _defineProperty(_TOKENS$heightLine, _consts.SIZE_OPTIONS.NORMAL, "24px"), _TOKENS$heightLine)), _tokens); return tokens[name][size]; }; }; var FakeGroup = (0, _styledComponents.default)(function (_ref2) { var children = _ref2.children, className = _ref2.className; return React.createElement("div", { className: className }, children); }).withConfig({ displayName: "InputGroup__FakeGroup", componentId: "cyuwoi-0" })(["width:100%;position:absolute;top:0;left:0;z-index:1;box-sizing:border-box;height:", ";border-radius:", ";box-shadow:", ";box-shadow:", ";box-shadow:", ";background-color:", ";font-size:", ";transition:box-shadow ", " ease-in-out;margin-top:", ";&:hover{box-shadow:inset 0 0 0 ", ";}"], getToken(_consts.TOKENS.height), function (_ref3) { var theme = _ref3.theme; return theme.orbit.borderRadiusNormal; }, function (_ref4) { var theme = _ref4.theme; return "inset 0 0 0 ".concat(theme.orbit.borderWidthInput, " ").concat(theme.orbit.borderColorInput); }, function (_ref5) { var theme = _ref5.theme, error = _ref5.error; return error && "inset 0 0 0 ".concat(theme.orbit.borderWidthInput, " ").concat(theme.orbit.borderColorInputError); }, function (_ref6) { var theme = _ref6.theme, active = _ref6.active; return active && "inset 0 0 0 ".concat(theme.orbit.borderWidthInputFocus, " ").concat(theme.orbit.borderColorInputFocus); }, function (_ref7) { var disabled = _ref7.disabled, theme = _ref7.theme; return disabled ? theme.orbit.backgroundInputDisabled : theme.orbit.backgroundInput; }, function (_ref8) { var theme = _ref8.theme; return theme.orbit.fontSizeInputNormal; }, function (_ref9) { var theme = _ref9.theme; return theme.orbit.durationFast; }, function (_ref10) { var label = _ref10.label; return label && "23px"; }, function (_ref11) { var theme = _ref11.theme, error = _ref11.error; return "".concat(theme.orbit.borderWidthInput, " ").concat(error ? theme.orbit.borderColorInputErrorHover : theme.orbit.borderColorInputHover); }); FakeGroup.defaultProps = { theme: _defaultTokens.default }; var StyledChildren = _styledComponents.default.div.withConfig({ displayName: "InputGroup__StyledChildren", componentId: "cyuwoi-1" })(["display:flex;position:relative;"]); var StyledChild = _styledComponents.default.div.withConfig({ displayName: "InputGroup__StyledChild", componentId: "cyuwoi-2" })(["flex:", ";padding-right:", ";:last-child{padding-right:0;}"], function (_ref12) { var flex = _ref12.flex; return flex; }, function (_ref13) { var theme = _ref13.theme; return theme.orbit.spaceXSmall; }); StyledChild.defaultProps = { theme: _defaultTokens.default }; var StyledInputGroup = (0, _styledComponents.default)(function (_ref14) { var children = _ref14.children, className = _ref14.className, dataTest = _ref14.dataTest; return React.createElement("div", { className: className, "data-test": dataTest }, children); }).withConfig({ displayName: "InputGroup__StyledInputGroup", componentId: "cyuwoi-3" })(["display:flex;flex-direction:column;position:relative;", "{", "{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;right:0;top:50%;transform:translateY(-50%);height:", ";width:1px;background-color:", ";transition:background-color ", " ease-in-out;display:block;z-index:2;}&:last-child{", ":after,", ":after{content:none;}}}", " ", "{display:", ";}", ":focus ~ ", "{box-shadow:none;}"], StyledChild, _InputField.FakeInput, _Select.SelectContainer, _InputField.InputContainer, _Select.SelectContainer, getToken(_consts.TOKENS.heightLine), function (_ref15) { var theme = _ref15.theme, error = _ref15.error, active = _ref15.active; return error && !active ? theme.orbit.borderColorInputError : theme.orbit.borderColorInput; }, function (_ref16) { var theme = _ref16.theme; return theme.orbit.durationFast; }, _InputField.InputContainer, _Select.SelectContainer, StyledChild, _FormLabel.default, function (_ref17) { var label = _ref17.label; return label && "none"; }, _InputField.Input, _InputField.FakeInput); StyledInputGroup.defaultProps = { theme: _defaultTokens.default }; var InputGroup = /*#__PURE__*/ function (_React$PureComponent) { _inherits(InputGroup, _React$PureComponent); function InputGroup() { var _getPrototypeOf2; var _this; _classCallCheck(this, InputGroup); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(InputGroup)).call.apply(_getPrototypeOf2, [this].concat(args))); _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "state", { active: false, filled: false }); _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "isFilled", function () { return _this.setState({ filled: !React.Children.map(_this.props.children, function (child) { return child.props.value !== undefined && child.props.value !== ""; }).includes(false) }); }); _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleFocus", function (ev) { var onFocus = _this.props.onFocus; _this.setState({ active: true }); if (onFocus) { onFocus(ev); } }); _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleBlur", function (ev) { var onBlur = _this.props.onBlur; _this.isFilled(); _this.setState({ active: false }); if (onBlur) { onBlur(ev); } }); _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleChange", function (ev) { var onChange = _this.props.onChange; _this.isFilled(); if (onChange) { onChange(ev); } }); return _this; } _createClass(InputGroup, [{ key: "componentDidMount", value: function componentDidMount() { this.isFilled(); } }, { key: "componentDidUpdate", value: function componentDidUpdate(prevProps) { if (this.props.children !== prevProps.children) { this.isFilled(); } } }, { key: "render", value: function render() { var _this2 = this; var _this$props = this.props, children = _this$props.children, label = _this$props.label, _this$props$flex = _this$props.flex, flex = _this$props$flex === void 0 ? "0 1 auto" : _this$props$flex, _this$props$size = _this$props.size, size = _this$props$size === void 0 ? _consts.SIZE_OPTIONS.NORMAL : _this$props$size, help = _this$props.help, error = _this$props.error, dataTest = _this$props.dataTest; var _this$state = this.state, active = _this$state.active, filled = _this$state.filled; return React.createElement(StyledInputGroup, { label: label, error: error, active: active, size: size, dataTest: dataTest }, label && React.createElement(_FormLabel.default, { filled: filled }, label), 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 React.createElement(StyledChild, { flex: childFlex }, React.createElement(item.type, _extends({}, item.props, { size: size, label: undefined, help: undefined, error: undefined, onChange: _this2.handleChange, onBlur: _this2.handleBlur, onFocus: _this2.handleFocus }))); })), React.createElement(FakeGroup, { label: label, error: error, active: active, size: size }), !error && help && React.createElement(_FormFeedback.default, { type: "help" }, help), error && React.createElement(_FormFeedback.default, { type: "error" }, error)); } }]); return InputGroup; }(React.PureComponent); var _default = InputGroup; exports.default = _default;