UNPKG

@kiwicom/orbit-components

Version:

<div align="center"> <a href="https://orbit.kiwi" target="_blank"> <img alt="orbit-components" src="https://orbit.kiwi/wp-content/uploads/2018/08/orbit-components.png" srcset="https://orbit.kiwi/wp-content/uploads/2018/08/orbit-components@2x.png 2x"

280 lines (230 loc) 11.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var _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; }; var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _react = require("react"); var React = _interopRequireWildcard(_react); var _styledComponents = require("styled-components"); var _styledComponents2 = _interopRequireDefault(_styledComponents); var _defaultTokens = require("../defaultTokens"); var _defaultTokens2 = _interopRequireDefault(_defaultTokens); var _FormLabel = require("../FormLabel"); var _FormLabel2 = _interopRequireDefault(_FormLabel); var _InputField = require("../InputField"); var _Select = require("../Select"); var _FormFeedback = require("../FormFeedback"); var _FormFeedback2 = _interopRequireDefault(_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)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } 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, _styledComponents2.default)(function (_ref2) { var children = _ref2.children, className = _ref2.className; return React.createElement( "div", { className: className }, children ); }).withConfig({ displayName: "InputGroup__FakeGroup" })(["width:100%;position:absolute;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:23px;&: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 " + theme.orbit.borderWidthInput + " " + theme.orbit.borderColorInput; }, function (_ref5) { var theme = _ref5.theme, error = _ref5.error; return error && "inset 0 0 0 " + theme.orbit.borderWidthInput + " " + theme.orbit.borderColorInputError; }, function (_ref6) { var theme = _ref6.theme, active = _ref6.active; return active && "inset 0 0 0 " + theme.orbit.borderWidthInputFocus + " " + 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 theme = _ref10.theme, error = _ref10.error; return theme.orbit.borderWidthInput + " " + (error ? theme.orbit.borderColorInputErrorHover : theme.orbit.borderColorInputHover); }); FakeGroup.defaultProps = { theme: _defaultTokens2.default }; var StyledChildren = _styledComponents2.default.div.withConfig({ displayName: "InputGroup__StyledChildren" })(["display:flex;position:relative;"]); var StyledChild = _styledComponents2.default.div.withConfig({ displayName: "InputGroup__StyledChild" })(["flex:", ";padding-right:", ";:last-child{padding-right:0;}"], function (_ref11) { var flex = _ref11.flex; return flex; }, function (_ref12) { var theme = _ref12.theme; return theme.orbit.spaceXSmall; }); StyledChild.defaultProps = { theme: _defaultTokens2.default }; var StyledInputGroup = (0, _styledComponents2.default)(function (_ref13) { var children = _ref13.children, className = _ref13.className, dataTest = _ref13.dataTest; return React.createElement( "div", { className: className, "data-test": dataTest }, children ); }).withConfig({ displayName: "InputGroup__StyledInputGroup" })(["display:flex;flex-direction:column;position:relative;", "{", "{box-shadow:none;background-color:transparent;display:flex;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;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 (_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; }, _InputField.InputContainer, _Select.SelectContainer, StyledChild, _FormLabel2.default, function (_ref16) { var label = _ref16.label; return label && "none"; }, _InputField.Input, _InputField.FakeInput); StyledInputGroup.defaultProps = { theme: _defaultTokens2.default }; var InputGroup = function (_React$PureComponent) { _inherits(InputGroup, _React$PureComponent); function InputGroup() { var _ref17; var _temp, _this, _ret; _classCallCheck(this, InputGroup); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref17 = InputGroup.__proto__ || Object.getPrototypeOf(InputGroup)).call.apply(_ref17, [this].concat(args))), _this), _this.state = { active: false, filled: false }, _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) }); }, _this.handleFocus = function (ev) { var onFocus = _this.props.onFocus; _this.setState({ active: true }); if (onFocus) { onFocus(ev); } }, _this.handleBlur = function (ev) { var onBlur = _this.props.onBlur; _this.isFilled(); _this.setState({ active: false }); if (onBlur) { onBlur(ev); } }, _this.handleChange = function (ev) { var onChange = _this.props.onChange; _this.isFilled(); if (onChange) { onChange(ev); } }, _temp), _possibleConstructorReturn(_this, _ret); } _createClass(InputGroup, [{ key: "componentDidMount", value: function componentDidMount() { this.isFilled(); } }, { key: "componentDidUpdate", value: function componentDidUpdate() { this.isFilled(); } }, { key: "render", value: function render() { var _this2 = this; var _props = this.props, children = _props.children, label = _props.label, _props$flex = _props.flex, flex = _props$flex === undefined ? "0 1 auto" : _props$flex, _props$size = _props.size, size = _props$size === undefined ? _consts.SIZE_OPTIONS.NORMAL : _props$size, help = _props.help, error = _props.error, dataTest = _props.dataTest; var _state = this.state, active = _state.active, filled = _state.filled; return React.createElement( StyledInputGroup, { label: label, error: error, active: active, size: size, dataTest: dataTest }, label && React.createElement( _FormLabel2.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( _FormFeedback2.default, { type: "help" }, help ), error && React.createElement( _FormFeedback2.default, { type: "error" }, error ) ); } }]); return InputGroup; }(React.PureComponent); exports.default = InputGroup;