UNPKG

@up-group-ui/react-controls

Version:
90 lines 5.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var jsx_runtime_1 = require("react/jsx-runtime"); var react_1 = (0, tslib_1.__importDefault)(require("react")); var BaseControl_1 = require("../_Common/BaseControl/BaseControl"); var theming_1 = (0, tslib_1.__importDefault)(require("../../../Common/theming")); var react_textarea_autosize_1 = (0, tslib_1.__importDefault)(require("react-textarea-autosize")); var styles_1 = require("./styles"); var classnames_1 = (0, tslib_1.__importDefault)(require("classnames")); var BaseTextArea = (function (_super) { (0, tslib_1.__extends)(BaseTextArea, _super); function BaseTextArea(p, c) { var _this = _super.call(this, p, c) || this; _this.setInput = function (input) { if (_this.textArea == undefined) { _this.textArea = input; } }; return _this; } BaseTextArea.prototype.componentDidMount = function () { if (this.props.dataFor && this.textArea) { this.textArea._rootDOMNode.setAttribute('data-tip', 'tooltip'); this.textArea._rootDOMNode.setAttribute('data-for', this.props.dataFor); } }; BaseTextArea.prototype.render = function () { var _a; var _b = this.props, className = _b.className, value = _b.value, onChange = _b.onChange, name = _b.name, tabIndex = _b.tabIndex, placeholder = _b.placeholder, readonly = _b.readonly, maxChar = _b.maxChar, maxCharMsg = _b.maxCharMsg, maxCharMsgShowNumber = _b.maxCharMsgShowNumber; var shouldDisplayMaxCharMsg = maxCharMsgShowNumber ? (value === null || value === void 0 ? void 0 : value.length) > maxCharMsgShowNumber : true; var templateMsg = maxCharMsg && maxCharMsg .replace('{{numberOfChar}}', ((_a = value === null || value === void 0 ? void 0 : value.length) === null || _a === void 0 ? void 0 : _a.toString()) || '0') .replace('{{maxChar}}', (maxChar === null || maxChar === void 0 ? void 0 : maxChar.toString()) || ''); return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(react_textarea_autosize_1.default, { value: value, readOnly: readonly, name: name, placeholder: placeholder, ref: this.setInput, tabIndex: tabIndex, className: (0, classnames_1.default)(className, 'up-text', { 'up-text-error': (value === null || value === void 0 ? void 0 : value.length) > maxChar, }), onChange: function (e) { return onChange(e, e.target.value); } }, void 0), maxChar && shouldDisplayMaxCharMsg && ((0, jsx_runtime_1.jsx)("div", (0, tslib_1.__assign)({ className: (0, classnames_1.default)(className, 'up-text-max-characters') }, { children: (0, jsx_runtime_1.jsx)("span", (0, tslib_1.__assign)({ className: (0, classnames_1.default)(className, 'up-text-max-characters-msg') }, { children: templateMsg }), void 0) }), void 0))] }, void 0)); }; return BaseTextArea; }(react_1.default.Component)); var UpText = (function (_super) { (0, tslib_1.__extends)(UpText, _super); function UpText(p, c) { var _this = _super.call(this, p, c) || this; _this.onChange = function (event, value) { event.persist(); var _a = _this.props, forceMaxChar = _a.forceMaxChar, maxChar = _a.maxChar; var _value = event.target.value; if (forceMaxChar && maxChar && _value && _value.length > maxChar) { _value = _value.substr(0, maxChar); } event.target.value = _value; _this.handleChangeEvent(event, _value); }; _this.getValue = _this.getValue.bind(_this); _this.state = { value: p.value, }; return _this; } UpText.prototype.showError = function () { return this.props.showError !== undefined ? typeof this.props.showError === 'function' ? this.props.showError(this.state) : this.props.showError === true : this.hasError; }; UpText.prototype.showSuccess = function () { return this.props.showSuccess; }; UpText.prototype.getValue = function (event) { return event != null && event.target != null ? event.target.value : event; }; UpText.prototype.renderControl = function () { var _a = this.props, onChange = _a.onChange, className = _a.className, tooltip = _a.tooltip, value = _a.value, others = (0, tslib_1.__rest)(_a, ["onChange", "className", "tooltip", "value"]); return ((0, jsx_runtime_1.jsx)(BaseTextArea, (0, tslib_1.__assign)({ className: (0, classnames_1.default)((0, styles_1.getStyles)(this.props), className), value: this.currentValue, onChange: this.onChange }, others), void 0)); }; UpText.defaultProps = { width: 'fill', showError: true, theme: theming_1.default, readonly: false, maxCharMsg: 'Vous avez saisi {{numberOfChar}} sur un nombre maximal de {{maxChar}}', forceMaxChar: false, }; return UpText; }(BaseControl_1.BaseControlComponent)); exports.default = UpText; //# sourceMappingURL=UpText.js.map