UNPKG

kea-react

Version:

Componentes comunes de react

123 lines (122 loc) 5.07 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0) t[p[i]] = s[p[i]]; return t; }; Object.defineProperty(exports, "__esModule", { value: true }); var React = require("react"); var fastInputDelay = 300; /** * Un input controlado se vuelve muy lento en android debido a que se estan actualizando constantemente sus * propiedades. * * Este componente envuelve a un input no controlado y solo actualiza su valor cuando es necesario, logrando * que se pueda escribir de manera fluida en este en móviles */ var FastInput = /** @class */ (function (_super) { __extends(FastInput, _super); function FastInput() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.handleRef = function (input) { if (input) { input.addEventListener("input", _this.handleOnChange); input.addEventListener("blur", _this.handleBlur); } else if (_this.input) { _this.input.removeEventListener("input", _this.handleOnChange); _this.input.removeEventListener("blur", _this.handleBlur); } _this.input = input; _this.setInputValue(_this.props.value || ""); }; _this.propsCount = 0; _this.unbouncedRefresh = function (value) { _this.propsCount++; var currentCount = _this.propsCount; var currentChangeCount = _this.changeCount; setTimeout(function () { if (currentCount == _this.propsCount && currentChangeCount == _this.changeCount) { _this.setInputValue(value); } }, fastInputDelay); }; _this.changeCount = 0; _this.raiseOnChange = function (value) { if (_this.props.onChange && _this.lastValue !== value) { _this.lastValue = value; _this.props.onChange(value); } }; _this.handleOnChange = function (e) { _this.changeCount++; var currentCount = _this.changeCount; setTimeout(function () { if (currentCount == _this.changeCount) { _this.raiseOnChange(e.target.value); } }, fastInputDelay); }; _this.handleBlur = function (e) { _this.propsCount++; _this.changeCount++; if (_this.input) { _this.raiseOnChange(_this.input.value); } }; return _this; } FastInput.prototype.componentWillReceiveProps = function (newProps) { var isFocused = this.input == document.activeElement; if (isFocused) { this.unbouncedRefresh(newProps.value); } else if (this.input) { this.input.value = newProps.value; } }; /** * Establece el valor del input igual al valor de props.value * @param value Nuevo valor */ FastInput.prototype.setInputValue = function (value) { if (this.input && this.input.value != value) { this.input.value = value; } }; FastInput.prototype.render = function () { var props = this.props.onChange ? Object.assign({}, this.props, { value: undefined, onChange: undefined }) : Object.assign({}, this.props); var elementType = this.props.multiline ? "textarea" : "input"; //Quitamos los props que no le pertenecen al textarea ni al input var multiline = props.multiline, newProps = __rest(props, ["multiline"]); var ref = (this.props.onChange && this.handleRef); var element = React.createElement(elementType, __assign({ ref: ref }, newProps)); return element; }; return FastInput; }(React.Component)); exports.FastInput = FastInput;