Debian Codegraph @

node/@patternfly/react-table/dist/js/components/Table/utils/utils.js

raw
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.capitalize = exports.toCamel = exports.applyCellEdits = exports.validateCellEdits = exports.cancelCellEdits = exports.getErrorTextByValidator = exports.isRowExpanded = void 0;
4 const hasCompoundParentsExpanded = (parentId, compoundParent, rows) => {
5 // max rows.length parents
6 // eslint-disable-next-line @typescript-eslint/no-unused-vars
7 for (const i of rows) {
8 if (rows[parentId].hasOwnProperty('parent')) {
9 parentId = rows[parentId].parent;
10 }
11 else {
12 return rows[parentId].cells[compoundParent].props.isOpen;
13 }
14 }
15 return false;
16 };
17 const hasParentsExpanded = (parentId, rows) => {
18 // max rows.length parents
19 // eslint-disable-next-line @typescript-eslint/no-unused-vars
20 for (const i of rows) {
21 if (rows[parentId].hasOwnProperty('parent')) {
22 parentId = rows[parentId].parent;
23 }
24 else {
25 return rows[parentId].isOpen;
26 }
27 }
28 return false;
29 };
30 const isRowExpanded = (row, rows) => {
31 if (row.parent !== undefined) {
32 if (row.hasOwnProperty('compoundParent')) {
33 return hasCompoundParentsExpanded(row.parent, row.compoundParent, rows);
34 }
35 return hasParentsExpanded(row.parent, rows) && rows[row.parent].isOpen;
36 }
37 return undefined;
38 };
39 exports.isRowExpanded = isRowExpanded;
40 const getErrorTextByValidator = (validatorName, validators) => {
41 const result = validators.filter((validator) => validator.name === validatorName);
42 return result[0].errorText;
43 };
44 exports.getErrorTextByValidator = getErrorTextByValidator;
45 const cancelCellEdits = (row) => {
46 row.cells.forEach((cell) => {
47 delete cell.props.errorText;
48 delete cell.props.editableValue;
49 cell.props.isValid = true;
50 // for editable selects, revert the selected property to its original value
51 if (cell.props.selected) {
52 cell.props.selected = cell.props.value;
53 }
54 });
55 row.isEditable = !row.isEditable;
56 row.isValid = true;
57 return row;
58 };
59 exports.cancelCellEdits = cancelCellEdits;
60 const validateCellEdits = (row, type, validationErrors, missingPropErrorTxt = 'Validation requires unique name property for row cells') => {
61 row.isValid = Object.keys(validationErrors).length ? false : true;
62 row.cells.forEach((cell) => {
63 delete cell.props.errorText;
64 const hasValue = cell.props.value !== undefined && cell.props.value !== null;
65 const hasEditableValue = cell.props.editableValue !== undefined && cell.props.editableValue !== null;
66 if (cell.props && hasValue && hasEditableValue) {
67 if (type === 'save') {
68 const errorMsg = Object.keys(validationErrors)
69 .filter((validatorName) => validationErrors[validatorName].includes(cell.props.name))
70 .map((validatorName) => (0, exports.getErrorTextByValidator)(validatorName, row.rowEditValidationRules));
71 if (errorMsg.length) {
72 cell.props.errorText = cell.props.name ? errorMsg.join(', ') : missingPropErrorTxt;
73 if (cell.props.name === undefined) {
74 // eslint-disable-next-line no-console
75 console.warn('Row edit validation reporting requires cell definitions to have a unique name property.');
76 }
77 }
78 else {
79 delete cell.props.errorText;
80 cell.props.isValid = true;
81 }
82 }
83 }
84 });
85 return row;
86 };
87 exports.validateCellEdits = validateCellEdits;
88 const applyCellEdits = (row, type) => {
89 row.cells.forEach((cell) => {
90 delete cell.props.errorText;
91 const hasValue = cell.props.value !== undefined && cell.props.value !== null;
92 const hasEditableValue = cell.props.editableValue !== undefined && cell.props.editableValue !== null;
93 // sync for validation
94 if (hasValue && !hasEditableValue) {
95 cell.props.editableValue = cell.props.value;
96 }
97 if (cell.props && hasValue && hasEditableValue) {
98 if (type === 'save') {
99 cell.props.value = cell.props.editableValue;
100 cell.props.isValid = true;
101 delete cell.props.errorText;
102 }
103 delete cell.props.editableValue;
104 }
105 });
106 row.isEditable = !row.isEditable;
107 row.isValid = true;
108 return row;
109 };
110 exports.applyCellEdits = applyCellEdits;
111 const camelize = (s) => s.toUpperCase().replace('-', '').replace('_', '');
112 const toCamel = (s) => s.replace(/([-_][a-z])/gi, camelize);
113 exports.toCamel = toCamel;
114 /**
115 * @param {string} input - String to capitalize
116 */
117 function capitalize(input) {
118 return input[0].toUpperCase() + input.substring(1);
119 }
120 exports.capitalize = capitalize;
121 //# sourceMappingURL=utils.js.map