@worktif/purei
Version:
Work TIF Material UI Theme Provider and Customization Suite for React applications with dark mode support and dynamic color schemes
115 lines • 5.38 kB
JavaScript
;
/*
* Work TIF, Raman Marozau.
* All rights reserved.
* Copyright(c), 2025-present.
*
* Business Source License 1.1
*
* Copyright (C) 2025 Raman Marozau, raman@worktif.com
* Use of this software is governed by the Business Source License included in the LICENSE file and at www.mariadb.com/bsl11.
*
* Change Date: Never
* On the date above, in accordance with the Business Source License, use of this software will be governed by the open source license specified in the LICENSE file.
* Additional Use Grant: Free for personal and non-commercial research use only.
*
*
* SPDX-License-Identifier: BUSL-1.1
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.FormErrors = void 0;
const React = __importStar(require("react"));
const react_1 = require("react");
const material_1 = require("@mui/material");
const form_errors_text_1 = require("./form-errors-text");
const forms_context_1 = require("../../../context/forms/forms.context");
const form_error_1 = require("../../../context/forms/form.error");
const i18n_1 = require("../../../i18n/i18n");
const ui_1 = require("../../ui");
/**
* FormErrors is a functional React component that renders a list of error messages relevant to a specific form.
*
* This component is designed to handle validation errors that may occur during form submission or interaction.
* It supports rendering both form-specific errors derived from the form's value structure and custom error messages passed as props.
*
* FormErrors also utilizes localized labels for form fields and styles to provide a user-friendly display of error feedback.
*
* Props:
* - formValue (object[]): An array of form structures where each structure represents a form state. The component will extract error data from the specific index determined by `formIndex`.
* - messages (string[]): An optional array of additional string error messages to display.
* - formIndex (number): The index of the form within `formValue` that is being evaluated for errors. Defaults to 0.
*
* Behavior:
* - If the `formValue` is defined and contains errors at the specified `formIndex`, those errors are rendered.
* - If `messages` is provided, it appends those messages to the list of errors.
* - Each error renders with an optional field label, which is translated using an internationalization schema accessed via React context.
* - If there are no errors, the component returns `null`.
*/
const FormErrors = ({ formValue, messages, formIndex = 0, }) => {
const { i18nSchema } = (0, react_1.useContext)(forms_context_1.FormsContext);
if (!(formValue && formValue[formIndex])) {
return null;
}
const formErrorList = Object.keys(formValue[formIndex])
.map((formField, index) => {
const formItem = formValue[0][formField];
if (formItem) {
return formItem instanceof form_error_1.FormError ? (React.createElement(form_errors_text_1.FormErrorsText, { message: React.createElement(React.Fragment, null,
React.createElement(material_1.ListItem, { sx: (theme) => ({ color: theme.palette.error.main }) }, (0, i18n_1.getFormFieldLabel)(i18nSchema, formField)),
' ',
"- ",
formItem.getMessage()) })) : (void 0);
}
return void 0;
})
.filter(item => item !== undefined);
const errorList = messages
? messages.filter((message) => !!message)
.map((message, index) => (React.createElement(form_errors_text_1.FormErrorsText, { message: message, key: index })))
.concat(formErrorList)
: formErrorList;
if (errorList.length > 0) {
return (React.createElement(React.Fragment, null,
React.createElement(ui_1.HighlightedDividerAlert, { sx: {
width: '100%',
height: '1px',
} }),
errorList));
}
return null;
};
exports.FormErrors = FormErrors;
//# sourceMappingURL=index.js.map