@age/quantum
Version:
Catho react components
309 lines (250 loc) • 12.4 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _propTypes = _interopRequireDefault(require("prop-types"));
var _styledComponents = _interopRequireDefault(require("styled-components"));
var _react = _interopRequireDefault(require("react"));
var _reactTextMask = _interopRequireDefault(require("react-text-mask"));
var _shared = require("../shared");
var _Icon = _interopRequireDefault(require("../Icon"));
var _InputTypes = _interopRequireDefault(require("./InputTypes"));
var _subComponents = require("./sub-components");
var _theme = require("../shared/theme");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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 _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
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 _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
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); }
var ID_GENERATOR = (0, _shared.uniqId)('input-');
var InputIcon = (0, _styledComponents.default)(_Icon.default).withConfig({
displayName: "Input__InputIcon",
componentId: "w1ge5o-0"
})(["cursor:pointer;position:absolute;", ""], function (_ref) {
var _ref$theme$spacing = _ref.theme.spacing,
xsmall = _ref$theme$spacing.xsmall,
medium = _ref$theme$spacing.medium;
return "\n right: ".concat(medium, "px;\n bottom: ").concat(xsmall * 1.25, "px;\n ");
});
var InputSearchIcon = (0, _styledComponents.default)(InputIcon).attrs({
name: 'search'
}).withConfig({
displayName: "Input__InputSearchIcon",
componentId: "w1ge5o-1"
})(["pointer-events:none;", ""], function (_ref2) {
var medium = _ref2.theme.spacing.medium;
return "left: ".concat(medium, "px;");
});
var InputErrorIcon = (0, _styledComponents.default)(InputIcon).attrs({
name: 'error'
}).withConfig({
displayName: "Input__InputErrorIcon",
componentId: "w1ge5o-2"
})(["", ""], function (_ref3) {
var error700 = _ref3.theme.colors.error[700];
return "color: ".concat(error700, ";");
});
var DescriptionLabel = _styledComponents.default.span.withConfig({
displayName: "Input__DescriptionLabel",
componentId: "w1ge5o-3"
})(["cursor:text;display:block;", ""], function (_ref4) {
var fontSize = _ref4.theme.baseFontSize;
return "\n font-size: ".concat(fontSize * 0.875, "px;\n padding: 0;\n ");
});
var InputWrapper = _styledComponents.default.div.withConfig({
displayName: "Input__InputWrapper",
componentId: "w1ge5o-4"
})(["position:relative;"]);
InputIcon.displayName = 'InputIcon';
InputSearchIcon.displayName = 'InputSearchIcon';
InputErrorIcon.displayName = 'InputErrorIcon';
DescriptionLabel.displayName = 'DescriptionLabel';
/** A text field component to get user text data */
var Input =
/*#__PURE__*/
function (_React$Component) {
_inherits(Input, _React$Component);
function Input(props) {
var _this;
_classCallCheck(this, Input);
_this = _possibleConstructorReturn(this, _getPrototypeOf(Input).call(this, props));
_initialiseProps.call(_assertThisInitialized(_this));
var type = props.type,
id = props.id,
value = props.value;
_this.state = {
type: type,
currentValue: value,
hasDefaultValue: value !== null && value[0]
};
_this._id = id || ID_GENERATOR.next().value;
return _this;
}
_createClass(Input, [{
key: "render",
value: function render() {
var _this2 = this;
var _this$props = this.props,
label = _this$props.label,
error = _this$props.error,
typeProp = _this$props.type,
descriptionLabel = _this$props.descriptionLabel,
helperText = _this$props.helperText,
required = _this$props.required,
onClean = _this$props.onClean,
theme = _this$props.theme,
rest = _objectWithoutProperties(_this$props, ["label", "error", "type", "descriptionLabel", "helperText", "required", "onClean", "theme"]);
var _this$state = this.state,
currentValue = _this$state.currentValue,
hasDefaultValue = _this$state.hasDefaultValue;
var typeState = this.state.type;
var _isSearchType = typeProp === 'search';
var _isPassword = typeProp === 'password';
var hasValue = currentValue && currentValue[0];
var _hasIcon = error || _isPassword || hasValue;
var hasLabel = !!label;
var onCleanClick = function onCleanClick(e) {
_this2.setState({
currentValue: '',
hasDefaultValue: false
});
onClean(e);
};
return _react.default.createElement(_shared.FieldGroup, null, label && _react.default.createElement(_subComponents.InputLabel, {
htmlFor: this._id,
error: error
}, label, required && _react.default.createElement(_subComponents.RequiredMark, null, "*")), descriptionLabel && _react.default.createElement(DescriptionLabel, {
theme: theme
}, descriptionLabel), _react.default.createElement(InputWrapper, null, _isSearchType && _react.default.createElement(InputSearchIcon, {
description: descriptionLabel,
theme: theme
}), _react.default.createElement(_reactTextMask.default, _extends({}, rest, {
id: this._id,
required: required,
type: typeState,
value: currentValue,
onChange: this.onChangeInput,
render: function render(ref, props) {
return _react.default.createElement(_subComponents.TextInput, _extends({}, props, {
ref: ref,
error: error,
hasLabel: hasLabel,
hasRightIcon: _hasIcon,
hasLeftIcon: _isSearchType,
hasDefaultValue: hasDefaultValue
}));
}
})), error && !_isPassword && _react.default.createElement(InputErrorIcon, {
description: descriptionLabel,
theme: theme
}), _isPassword && _react.default.createElement(InputIcon, {
theme: theme,
name: typeState === 'password' ? 'visibility' : 'visibility_off',
description: descriptionLabel,
onClick: this._toggleInputType
}), hasValue && !_isPassword && !error && _react.default.createElement(InputIcon, {
theme: theme,
name: "clear",
description: descriptionLabel,
onClick: onCleanClick
})), helperText && _react.default.createElement(_subComponents.HelperText, null, helperText), error && _react.default.createElement(_subComponents.InputErrorMessage, {
theme: theme
}, error));
}
}], [{
key: "getDerivedStateFromProps",
value: function getDerivedStateFromProps(nextProps) {
var value = nextProps.value;
return {
currentValue: value
};
}
}]);
return Input;
}(_react.default.Component);
Input.CEP = _InputTypes.default.CEP;
Input.CNPJ = _InputTypes.default.CNPJ;
Input.CPF = _InputTypes.default.CPF;
Input.Date = _InputTypes.default.Date;
Input.Phone = _InputTypes.default.Phone;
Input.Password = _InputTypes.default.Password;
var _initialiseProps = function _initialiseProps() {
var _this3 = this;
this.onChangeInput = function (ev) {
var onChange = _this3.props.onChange;
var inputValue = ev.currentTarget.value;
_this3.setState({
currentValue: inputValue,
hasDefaultValue: false
});
onChange(ev);
};
this._changeType = function (type) {
_this3.setState({
type: type
});
};
this._toggleInputType = function () {
var type = _this3.state.type;
_this3._changeType(type === 'text' ? 'password' : 'text');
};
};
Input.propTypes = {
value: _propTypes.default.string,
/** Displays a label text that describes the field */
label: _propTypes.default.string,
/** Displays a helper text below the input */
helperText: _propTypes.default.string,
/** Displays a description text below the label */
descriptionLabel: _propTypes.default.string,
required: _propTypes.default.bool,
placeholder: _propTypes.default.string,
type: _propTypes.default.oneOf(['email', 'text', 'tel', 'number', 'password', 'search']),
/** Displays an error message and changes border color to error color */
error: _propTypes.default.string,
id: _propTypes.default.string,
/**
* Mask must follow this [rules](https://github.com/text-mask/text-mask/blob/master/componentDocumentation.md#mask)
*/
mask: _propTypes.default.oneOfType([_propTypes.default.array, _propTypes.default.bool, _propTypes.default.instanceOf(RegExp), _propTypes.default.func, _propTypes.default.string]),
onClean: _propTypes.default.func,
onChange: _propTypes.default.func,
theme: _propTypes.default.shape({
spacing: _propTypes.default.object,
colors: _propTypes.default.object,
baseFontSize: _propTypes.default.number
})
};
Input.defaultProps = {
error: '',
id: '',
label: '',
mask: function mask(val) {
return Array(val.length).fill(/./);
},
type: 'text',
value: '',
helperText: '',
descriptionLabel: '',
required: false,
placeholder: '',
onClean: function onClean() {},
onChange: function onChange() {},
theme: {
spacing: _theme.spacing,
colors: _theme.colors,
baseFontSize: _theme.baseFontSize
}
};
Input.displayName = 'Input';
var _default = Input;
exports.default = _default;