react-localized-input
Version:
A simple input library for React and Next.js
126 lines (116 loc) • 5.12 kB
JavaScript
import React, { forwardRef, useState, useRef, useEffect } from 'react';
/******************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
/* global Reflect, Promise, SuppressedError, Symbol */
var __assign = function() {
__assign = Object.assign || function __assign(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;
};
return __assign.apply(this, arguments);
};
function __rest(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 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
}
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
var e = new Error(message);
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
};
var InputComponent = forwardRef(function (_a, ref) {
var languageValidation = _a.languageValidation, _b = _a.className, className = _b === undefined ? "" : _b, style = _a.style, props = __rest(_a, ["languageValidation", "className", "style"]);
var _c = useState(false), isComposing = _c[0], setIsComposing = _c[1];
var inputRef = useRef(null);
var lastValidValueRef = useRef(props.value);
var defaultStyle = {
padding: "8px 12px",
fontSize: "16px",
border: "1px solid #ccc",
borderRadius: "4px",
width: "100%",
boxSizing: "border-box",
};
useEffect(function () {
if (inputRef.current) {
inputRef.current.value = lastValidValueRef.current;
}
}, [languageValidation]);
var validateInput = function (value) {
return value.split("").every(function (char) { return languageValidation.isValid(char) || char === " "; });
};
var handleInputChange = function (e) {
var newValue = e.target.value;
if (!isComposing) {
var isValid = validateInput(newValue);
if (isValid) {
lastValidValueRef.current = newValue;
if (props.onChange) {
props.onChange(e);
}
}
else {
// Reset to last valid value
e.target.value = lastValidValueRef.current;
if (props.onChange) {
props.onChange(__assign(__assign({}, e), { target: __assign(__assign({}, e.target), { value: lastValidValueRef.current }) }));
}
}
}
else if (props.onChange) {
props.onChange(e);
}
};
var handleCompositionStart = function () {
setIsComposing(true);
};
var handleCompositionEnd = function (e) {
setIsComposing(false);
var value = e.currentTarget.value;
var isValid = validateInput(value);
if (isValid) {
lastValidValueRef.current = value;
if (props.onChange) {
props.onChange({ target: { value: value } });
}
}
else {
// Reset to last valid value
e.currentTarget.value = lastValidValueRef.current;
if (props.onChange) {
props.onChange({ target: { value: lastValidValueRef.current } });
}
}
};
return (React.createElement("input", __assign({}, props, { ref: function (node) {
inputRef.current = node;
if (typeof ref === "function") {
ref(node);
}
else if (ref) {
ref.current = node;
}
}, onChange: handleInputChange, onCompositionStart: handleCompositionStart, onCompositionEnd: handleCompositionEnd, lang: languageValidation.lang, className: "border border-gray-300 rounded-md p-2 ".concat(className), style: __assign(__assign({}, defaultStyle), style) })));
});
InputComponent.displayName = "InputComponent";
export { InputComponent };
//# sourceMappingURL=index.esm.js.map