Debian Codegraph @

node/@patternfly/react-table/src/deprecated/components/Table/base/body-row.tsx

raw
1 /**
2 * body-row.tsx
3 *
4 * Forked from reactabular-table version 8.14.0
5 * https://github.com/reactabular/reactabular/tree/v8.14.0/packages/reactabular-table/src
6 */
7 import { Component, createElement } from 'react';
8 import isEqual from 'lodash/isEqual';
9 import {
10 createElementType,
11 formatterValueType,
12 ColumnType,
13 ColumnsType,
14 RowType,
15 RendererType
16 } from '../../../../components/Table/base/types';
17 import { columnsAreEqual } from '../../../../components/Table/base/columns-are-equal';
18 import { evaluateFormatters } from '../../../../components/Table/base/evaluate-formatters';
19 import { evaluateTransforms } from '../../../../components/Table/base/evaluate-transforms';
20 import { mergeProps } from '../../../../components/Table/base/merge-props';
21
22 export interface BodyRowProps {
23 columns: ColumnsType;
24 renderers: RendererType;
25 onRow?: Function;
26 rowIndex: number;
27 rowData: RowType;
28 rowKey: string;
29 }
30
31 class BodyRow extends Component<BodyRowProps, {}> {
32 static displayName = 'BodyRow';
33 static defaultProps = {
34 // eslint-disable-next-line @typescript-eslint/no-unused-vars
35 onRow: (...args: any) => ({})
36 };
37
38 shouldComponentUpdate(nextProps: BodyRowProps) {
39 const { columns, rowData } = this.props;
40
41 // Check for row based override.
42 const { renderers } = nextProps;
43
44 if (renderers && renderers.row && (renderers.row as React.Component).shouldComponentUpdate) {
45 if (typeof (renderers.row as React.Component).shouldComponentUpdate === 'function') {
46 return (renderers.row as React.Component).shouldComponentUpdate.call(this, nextProps, {}, {});
47 }
48
49 return true;
50 }
51
52 return !(columnsAreEqual(columns, nextProps.columns) && isEqual(rowData, nextProps.rowData));
53 }
54 render() {
55 const { columns, renderers, onRow, rowKey, rowIndex, rowData } = this.props;
56
57 return createElement(
58 renderers.row as createElementType,
59 onRow(rowData, { rowIndex, rowKey }),
60 (columns as []).map((column: ColumnType, columnIndex: number) => {
61 const { property, cell, props } = column;
62 const evaluatedProperty = (property || (cell && cell.property)) as string;
63 const { transforms = [], formatters = [] } = cell || {};
64 const extraParameters = {
65 columnIndex,
66 property: evaluatedProperty,
67 column,
68 rowData,
69 rowIndex,
70 rowKey
71 };
72 const transformed = evaluateTransforms(transforms, rowData[evaluatedProperty], extraParameters);
73
74 if (!transformed) {
75 // eslint-disable-next-line no-console
76 console.warn('Table.Body - Failed to receive a transformed result');
77 }
78
79 let additionalFormaters = [];
80 if (rowData[evaluatedProperty]) {
81 additionalFormaters = rowData[evaluatedProperty].formatters;
82 }
83
84 return createElement(
85 renderers.cell as createElementType,
86 {
87 key: `col-${columnIndex}-row-${rowIndex}`,
88 ...mergeProps(props, cell && cell.props, transformed)
89 },
90 (!rowData.fullWidth && transformed.children) ||
91 evaluateFormatters([...formatters, ...additionalFormaters])(
92 rowData[`_${evaluatedProperty}`] || (rowData[evaluatedProperty] as formatterValueType),
93 extraParameters
94 )
95 );
96 })
97 );
98 }
99 }
100
101 export { BodyRow };