infinity-forge
Version:
81 lines • 4.48 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 = (0, formik_1.useField)({ name: props.name, type: props.type }), field = _a[0], state = _a[1], utils = _a[2];
var _b = (0, react_1.useState)(field.value), tempValue = _b[0], setTempValue = _b[1];
function formatValueInput(value) {
var _a, _b, _c;
if (props.type !== 'date') {
return value;
}
if (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) {
utils.setValue((_a = props.controlledInitialValue) === null || _a === void 0 ? void 0 : _a.value);
return;
}
}, []);
var value = (0, react_1.useMemo)(function () { return formatValueInput(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 () {
if (tempValue !== field.value) {
utils.setValue(tempValue);
props === null || props === void 0 ? void 0 : props.onChangeBlur(tempValue, undefined, { state: state, utils: utils });
}
};
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.name, placeholder: props.placeholder, type: props.type || 'text', readOnly: props.readOnly }, field, { autoComplete: props.autoComplete, 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) {
if (props.onChangeMode === 'blur') {
setTempValue(ev.target.value);
}
else {
field.onChange(ev);
}
props.onChangeInput && props.onChangeInput(ev.target.value, undefined, { state: state, utils: utils });
}, onBlur: props.onChangeMode === 'blur' ? handleBlur : undefined })) })));
}
//# sourceMappingURL=index.js.map