Debian Codegraph @

node/@patternfly/react-table/src/components/Table/utils/utils.tsx

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