@novin-dev/formalite
Version:
Generate MUI form with few line of code
130 lines (127 loc) • 4.56 kB
JavaScript
import { __spreadArray, __assign } from 'tslib';
import numeral from 'numeral';
import { get } from 'lodash-es';
import { ViewTypes } from '../Formalite.type.js';
var getData = function (_a) {
var source = _a.source, key = _a.key;
return get(source, String(key));
};
var generateNestedKeyForYup = function (key) {
var splitedKey = key.split(".");
var generatedKey = "fields.".concat(splitedKey[0]);
splitedKey.forEach(function (item, index) {
if (index !== 0 && index % 2 === 0) {
generatedKey += ".innerType.fields.".concat(item);
}
});
return generatedKey;
};
var generalCheck = function (_a, type) {
var _b;
var schema = _a.schema, formikValues = _a.formikValues, key = _a.key;
var generatedKey = generateNestedKeyForYup(key);
var isRequired = false;
var fieldSchema = get(schema, generatedKey);
if (((_b = fieldSchema === null || fieldSchema === void 0 ? void 0 : fieldSchema.conditions) === null || _b === void 0 ? void 0 : _b.length) > 0) {
fieldSchema.conditions.every(function (condition) {
var _a;
var refValues = condition.refs.map(function (ref) {
return getData({ source: formikValues, key: ref.key });
});
var newSchema = (_a = condition.fn).bind.apply(_a, __spreadArray(__spreadArray([null], refValues, false), [fieldSchema], false))();
isRequired = !!get(newSchema, "exclusiveTests.".concat(type));
return !isRequired;
});
}
else {
var value = get(fieldSchema, "exclusiveTests.".concat(type));
isRequired = !!value;
}
return isRequired;
};
var checkIsRequired = function (_a) {
var schema = _a.schema, formikValues = _a.formikValues, key = _a.key;
return generalCheck({ schema: schema, formikValues: formikValues, key: key }, "required");
};
var checkIsMin = function (_a) {
var schema = _a.schema, formikValues = _a.formikValues, key = _a.key;
return generalCheck({ schema: schema, formikValues: formikValues, key: key }, "min");
};
var showErrorMessage = function (error) {
console.info("Formalite Error", error);
};
function fData(number) {
return numeral(number).format("0.0 b");
}
var getDefaultValue = function (type) {
if ([
ViewTypes.MultiDropZoneView,
ViewTypes.SingleDropZoneView,
ViewTypes.AvatarDropZoneView,
ViewTypes.CheckGroupView,
ViewTypes.SwitchGroupView,
ViewTypes.RepeaterView,
].includes(type)) {
return [];
}
if ([
ViewTypes.DatePickerView,
ViewTypes.DateTimePickerView,
ViewTypes.TimePickerView,
].includes(type)) {
return null;
}
if (ViewTypes.TextDropZoneView === type) {
return {
text: "",
files: [],
};
}
return "";
};
var handleRandomClassNameOrId = function () {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < 5; i += 1) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
};
var checkRegex = function (_a) {
var regexs = _a.regexs, text = _a.text;
if (!regexs) {
return true;
}
// const allRegexs = regexs.filter((item) => !!item);
var testResult = true;
regexs.forEach(function (item) {
if (item && !item.test(text)) {
testResult = false;
}
});
return testResult;
};
var resetFormFunction = function (formString) {
var result = {};
Object.keys(formString).forEach(function (item) {
if (formString[item].type === ViewTypes.GroupView) {
result = __assign(__assign({}, result), resetFormFunction(formString[item].options));
}
result[item] = getDefaultValue(formString[item].type);
});
return result;
};
var isIOS = function () {
return ([
"iPad Simulator",
"iPhone Simulator",
"iPod Simulator",
"iPad",
"iPhone",
"iPod",
].includes(navigator.platform) ||
// iPad on iOS 13 detection
(navigator.userAgent.includes("Mac") && "ontouchend" in document));
};
export { checkIsMin, checkIsRequired, checkRegex, fData, generateNestedKeyForYup, getData, getDefaultValue, handleRandomClassNameOrId, isIOS, resetFormFunction, showErrorMessage };
//# sourceMappingURL=utils.js.map