@catho/quantum
Version:
Catho react components
287 lines (285 loc) • 15.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _react = require("react");
var _propTypes = _interopRequireDefault(require("prop-types"));
var _styledComponents = _interopRequireWildcard(require("styled-components"));
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");
var _jsxRuntime = require("react/jsx-runtime");
var _excluded = ["label", "error", "type", "descriptionLabel", "helperText", "required", "onClean", "theme", "disabled", "skin"];
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], t.indexOf(o) >= 0 || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.indexOf(n) >= 0) continue; t[n] = r[n]; } return t; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } // eslint-disable-next-line import/no-cycle
var uniqId = (0, _shared.createUniqId)('input-');
var InputIcon = (0, _styledComponents["default"])(_Icon["default"]).withConfig({
displayName: "Input__InputIcon",
componentId: "sc-16zet9e-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 (0, _styledComponents.css)(["right:", "px;bottom:", "px;width:", "px;color:inherit;"], medium, xsmall * 1.25, _theme.baseFontSize * 1.5);
});
var InputSearchIcon = (0, _styledComponents["default"])(InputIcon).attrs({
name: 'search'
}).withConfig({
displayName: "Input__InputSearchIcon",
componentId: "sc-16zet9e-1"
})(["pointer-events:none;", ""], function (_ref2) {
var medium = _ref2.theme.spacing.medium;
return (0, _styledComponents.css)(["left:", "px;"], medium);
});
var InputErrorIcon = (0, _styledComponents["default"])(InputIcon).attrs({
name: 'error'
}).withConfig({
displayName: "Input__InputErrorIcon",
componentId: "sc-16zet9e-2"
})(["", ""], function (_ref3) {
var error = _ref3.theme.colors.error,
skin = _ref3.skin;
return (0, _styledComponents.css)(["color:", ";"], skin === 'default' ? error[700] : 'inherit');
});
var DescriptionLabel = _styledComponents["default"].span.withConfig({
displayName: "Input__DescriptionLabel",
componentId: "sc-16zet9e-3"
})(["cursor:text;display:block;", " color:inherit;"], 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: "sc-16zet9e-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 (_Component) {
function Input(props) {
var _this;
_classCallCheck(this, Input);
_this = _callSuper(this, Input, [props]);
_defineProperty(_this, "onChangeInput", function (ev) {
var onChange = _this.props.onChange;
var inputValue = ev.currentTarget.value;
_this.setState({
currentValue: inputValue,
hasDefaultValue: false
});
onChange(ev);
});
_defineProperty(_this, "_changeType", function (type) {
_this.setState({
type: type
});
});
_defineProperty(_this, "_toggleInputType", function () {
var type = _this.state.type;
_this._changeType(type === 'text' ? 'password' : 'text');
});
var _type = props.type,
id = props.id,
value = props.value;
_this.state = {
type: _type,
currentValue: value,
hasDefaultValue: value !== null && value[0]
};
_this._id = id || uniqId();
return _this;
}
_inherits(Input, _Component);
return _createClass(Input, [{
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps, prevState) {
var currentValue = this.state.currentValue;
var value = this.props.value;
if (currentValue !== value && prevProps.value !== value) {
// eslint-disable-next-line react/no-did-update-set-state
this.setState(_objectSpread(_objectSpread({}, prevState), {}, {
currentValue: value
}));
}
}
}, {
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,
disabled = _this$props.disabled,
skin = _this$props.skin,
rest = _objectWithoutProperties(_this$props, _excluded);
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 /*#__PURE__*/(0, _jsxRuntime.jsxs)(_shared.FieldGroup, {
theme: theme,
skin: skin,
children: [label && /*#__PURE__*/(0, _jsxRuntime.jsxs)(_subComponents.InputLabel, {
htmlFor: this._id,
error: error,
children: [label, required && /*#__PURE__*/(0, _jsxRuntime.jsx)(_subComponents.RequiredMark, {
skin: skin,
children: "*"
})]
}), descriptionLabel && /*#__PURE__*/(0, _jsxRuntime.jsx)(DescriptionLabel, {
theme: theme,
children: descriptionLabel
}), /*#__PURE__*/(0, _jsxRuntime.jsxs)(InputWrapper, {
children: [_isSearchType && /*#__PURE__*/(0, _jsxRuntime.jsx)(InputSearchIcon, {
description: descriptionLabel,
theme: theme
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactTextMask["default"], _objectSpread(_objectSpread({}, rest), {}, {
disabled: disabled,
id: this._id,
required: required,
type: typeState,
value: currentValue,
onChange: this.onChangeInput,
render: function render(ref, props) {
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_subComponents.TextInput, _objectSpread(_objectSpread({}, props), {}, {
ref: ref,
error: error,
hasLabel: hasLabel,
hasRightIcon: _hasIcon,
hasLeftIcon: _isSearchType,
hasDefaultValue: hasDefaultValue,
skin: skin
}));
}
})), error && !_isPassword && /*#__PURE__*/(0, _jsxRuntime.jsx)(InputErrorIcon, {
description: descriptionLabel,
theme: theme,
skin: skin
}), _isPassword && /*#__PURE__*/(0, _jsxRuntime.jsx)(InputIcon, {
theme: theme,
name: typeState === 'password' ? 'visibility' : 'visibility_off',
description: descriptionLabel,
onClick: this._toggleInputType
}), hasValue && !_isPassword && !error && !disabled && /*#__PURE__*/(0, _jsxRuntime.jsx)(InputIcon, {
theme: theme,
name: "clear",
description: descriptionLabel,
onClick: onCleanClick
})]
}), helperText && /*#__PURE__*/(0, _jsxRuntime.jsx)(_subComponents.HelperText, {
children: helperText
}), error && /*#__PURE__*/(0, _jsxRuntime.jsx)(_subComponents.InputErrorMessage, {
theme: theme,
skin: skin,
children: error
})]
});
}
}]);
}(_react.Component);
_defineProperty(Input, "CEP", _InputTypes["default"].CEP);
_defineProperty(Input, "CNPJ", _InputTypes["default"].CNPJ);
_defineProperty(Input, "CPF", _InputTypes["default"].CPF);
_defineProperty(Input, "Date", _InputTypes["default"].Date);
_defineProperty(Input, "Phone", _InputTypes["default"].Phone);
_defineProperty(Input, "Password", _InputTypes["default"].Password);
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,
disabled: _propTypes["default"].bool,
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
}),
minLength: _propTypes["default"].oneOfType([_propTypes["default"].number, _propTypes["default"].string]),
maxLength: _propTypes["default"].oneOfType([_propTypes["default"].number, _propTypes["default"].string]),
skin: _propTypes["default"].oneOf(['default', 'dark'])
};
Input.defaultProps = {
error: '',
disabled: false,
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
},
minLength: 0,
maxLength: 255,
skin: 'default'
};
Input.displayName = 'Input';
var _default = exports["default"] = Input;