MM-24743 Migrate DataRowGrid to functional component (#24795)

* Migrate DataRowGrid to functional component

* Change renderCell to a functional component dataGridCell and memoize DataGridRow

* Adjust test snapshot to include memoised DataGridRow

* use component notation when using DataGridCell

---------

Co-authored-by: js029 <js029@gmail.com>
Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
js029
2023-10-19 12:11:53 +02:00
коммит произвёл GitHub
родитель ec394c1162
Коммит c97731e4a6
2 изменённых файлов: 54 добавлений и 45 удалений

Просмотреть файл

@@ -61,7 +61,7 @@ exports[`components/admin_console/data_grid/DataGrid should match snapshot with
className="DataGrid_rows" className="DataGrid_rows"
style={Object {}} style={Object {}}
> >
<DataGridRow <Memo(DataGridRow)
columns={ columns={
Array [ Array [
Object { Object {
@@ -87,7 +87,7 @@ exports[`components/admin_console/data_grid/DataGrid should match snapshot with
} }
} }
/> />
<DataGridRow <Memo(DataGridRow)
columns={ columns={
Array [ Array [
Object { Object {
@@ -113,7 +113,7 @@ exports[`components/admin_console/data_grid/DataGrid should match snapshot with
} }
} }
/> />
<DataGridRow <Memo(DataGridRow)
columns={ columns={
Array [ Array [
Object { Object {
@@ -170,7 +170,7 @@ exports[`components/admin_console/data_grid/DataGrid should match snapshot with
className="DataGrid_rows" className="DataGrid_rows"
style={Object {}} style={Object {}}
> >
<DataGridRow <Memo(DataGridRow)
columns={ columns={
Array [ Array [
Object { Object {
@@ -193,7 +193,7 @@ exports[`components/admin_console/data_grid/DataGrid should match snapshot with
} }
} }
/> />
<DataGridRow <Memo(DataGridRow)
columns={ columns={
Array [ Array [
Object { Object {
@@ -216,7 +216,7 @@ exports[`components/admin_console/data_grid/DataGrid should match snapshot with
} }
} }
/> />
<DataGridRow <Memo(DataGridRow)
columns={ columns={
Array [ Array [
Object { Object {

Просмотреть файл

@@ -9,13 +9,17 @@ import type {Row, Column} from './data_grid';
import './data_grid.scss'; import './data_grid.scss';
type Props = { type DataGridRowProps = {
columns: Column[]; columns: Column[];
row: Row; row: Row;
} }
class DataGridRow extends React.Component<Props> { type DataGridCellProps = {
renderCell(row: Row, column: Column) { column: Column;
row: Row;
}
const DataGridCell = ({row, column}: DataGridCellProps) => {
const style: CSSProperties = {}; const style: CSSProperties = {};
if (column.width) { if (column.width) {
style.flexGrow = column.width; style.flexGrow = column.width;
@@ -38,19 +42,24 @@ class DataGridRow extends React.Component<Props> {
{row.cells[column.field]} {row.cells[column.field]}
</div> </div>
); );
} };
render() { const DataGridRow = ({row, columns}: DataGridRowProps) => {
const cells = this.props.columns.map((col) => this.renderCell(this.props.row, col)); const cells = columns.map((column, index) => (
<DataGridCell
key={index}
row={row}
column={column}
/>
));
return ( return (
<div <div
className='DataGrid_row' className='DataGrid_row'
onClick={this.props.row.onClick} onClick={row.onClick}
> >
{cells} {cells}
</div> </div>
); );
} };
}
export default DataGridRow; export default React.memo(DataGridRow);