Debian Codegraph @

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

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