infinity-forge
Version:
103 lines • 6.52 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__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;
};
return __assign.apply(this, arguments);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Input = Input;
var jsx_runtime_1 = require("react/jsx-runtime");
var react_1 = require("react");
var formik_1 = require("formik");
var ui_1 = require("../../../../ui/index.js");
var input_control_1 = require("../input-control/index.js");
var moment_1 = __importDefault(require("moment"));
function Input(props) {
var _a, _b;
var _c = (0, formik_1.useField)({ name: props === null || props === void 0 ? void 0 : props.name }), field = _c[0], state = _c[1], utils = _c[2];
var _d = (0, react_1.useState)(field.value), tempValue = _d[0], setTempValue = _d[1];
var blurTimeoutRef = (0, react_1.useRef)(null);
var formHandler = (0, ui_1.useFormHandler)();
function formatValueInput(value) {
var _a, _b, _c;
if ((props === null || props === void 0 ? void 0 : props.type) !== 'date') {
return value;
}
if ((props === null || props === void 0 ? void 0 : props.type) === 'datetime-local') {
return value instanceof Date ? (0, moment_1.default)(state.initialValue).format("YYYY-MM-DDTHH:mm") : value.slice(0, 16);
}
if (value instanceof Date) {
var day = value === null || value === void 0 ? void 0 : value.getDate();
var month = (value === null || value === void 0 ? void 0 : value.getMonth()) + 1;
var year = value === null || value === void 0 ? void 0 : value.getFullYear();
var formattedDay = day < 10 ? '0' + day : day;
var formattedMonth = month < 10 ? '0' + month : month;
var formattedDate = year + '-' + formattedMonth + '-' + formattedDay;
return formattedDate;
}
return (_c = (_b = (_a = state === null || state === void 0 ? void 0 : state.initialValue) === null || _a === void 0 ? void 0 : _a.split) === null || _b === void 0 ? void 0 : _b.call(_a, 'T')) === null || _c === void 0 ? void 0 : _c[0];
}
(0, react_1.useEffect)(function () {
var _a;
if ((props === null || props === void 0 ? void 0 : props.controlledInitialValue) && (field === null || field === void 0 ? void 0 : field.onChange)) {
field.onChange((_a = props === null || props === void 0 ? void 0 : props.controlledInitialValue) === null || _a === void 0 ? void 0 : _a.value);
return;
}
}, [field === null || field === void 0 ? void 0 : field.onChange]);
var value = (0, react_1.useMemo)(function () { return formatValueInput((props === null || props === void 0 ? void 0 : props.onChangeMode) === 'blur' ? tempValue : field.value); }, [field.value, tempValue]);
(0, react_1.useEffect)(function () {
if (value && !(0, ui_1.isValidDate)(value) && ((props === null || props === void 0 ? void 0 : props.type) === 'date' || (props === null || props === void 0 ? void 0 : props.type) === 'datetime-local')) {
utils.setValue(undefined);
setTempValue(undefined);
}
}, [value]);
(0, react_1.useEffect)(function () {
if ((props === null || props === void 0 ? void 0 : props.onChangeMode) === 'blur') {
setTempValue(field.value);
}
}, [field.value]);
var handleBlur = function (ev) {
var value = ev.target.value;
if (value !== field.value) {
utils.setValue(value);
}
scheduleSearch(value);
};
var scheduleSearch = function (value) {
if (blurTimeoutRef.current) {
clearTimeout(blurTimeoutRef.current);
}
blurTimeoutRef.current = setTimeout(function () {
var _a;
(_a = props === null || props === void 0 ? void 0 : props.onChangeBlur) === null || _a === void 0 ? void 0 : _a.call(props, value, undefined, { state: state, utils: utils });
}, 800);
};
return ((0, jsx_runtime_1.jsx)(input_control_1.InputControl, __assign({}, props, { fieldValue: value, children: (0, jsx_runtime_1.jsx)("input", __assign({}, props, { id: props === null || props === void 0 ? void 0 : props.name, placeholder: props === null || props === void 0 ? void 0 : props.placeholder, type: (props === null || props === void 0 ? void 0 : props.type) || 'text', readOnly: (props === null || props === void 0 ? void 0 : props.readOnly) || ((_b = (_a = formHandler === null || formHandler === void 0 ? void 0 : formHandler.formHandlerProps) === null || _a === void 0 ? void 0 : _a.inputs) === null || _b === void 0 ? void 0 : _b.readOnly) }, field, { autoComplete: (props === null || props === void 0 ? void 0 : props.autoComplete) || 'new-password', min: props === null || props === void 0 ? void 0 : props.min, max: props === null || props === void 0 ? void 0 : props.max, value: value, onChange: function (ev) {
var _a, _b;
var value = ev.target.value;
if (blurTimeoutRef.current) {
clearTimeout(blurTimeoutRef.current);
blurTimeoutRef.current = null;
}
if ((props === null || props === void 0 ? void 0 : props.onChangeMode) === 'blur') {
setTempValue(value);
if ((props === null || props === void 0 ? void 0 : props.onChangeMode) === 'blur') {
scheduleSearch(value);
}
(_a = props === null || props === void 0 ? void 0 : props.onChangeInput) === null || _a === void 0 ? void 0 : _a.call(props, value, undefined, { state: state, utils: utils });
return;
}
field.onChange(ev);
(_b = props === null || props === void 0 ? void 0 : props.onChangeInput) === null || _b === void 0 ? void 0 : _b.call(props, value, undefined, { state: state, utils: utils });
}, onBlur: (props === null || props === void 0 ? void 0 : props.onChangeMode) === 'blur' ? handleBlur : undefined })) })));
}
//# sourceMappingURL=index.js.map