Skip to content

Commit 0a02ff2

Browse files
authored
Merge branch 'master' into data_id
2 parents a98c64a + 5c01bab commit 0a02ff2

File tree

4 files changed

+94
-48
lines changed

4 files changed

+94
-48
lines changed

__tests__/demo/demo-components/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export function BulkEditWithDetailPanel() {
111111
options={{
112112
showDetailPanelIcon: false
113113
}}
114-
detailPanel={(rowData) => {
114+
detailPanel={({ rowData }) => {
115115
return (
116116
<iframe
117117
width="100%"
@@ -323,7 +323,7 @@ export function OneDetailPanel() {
323323
title="One Detail Panel Preview"
324324
columns={global_cols}
325325
data={global_data}
326-
detailPanel={(rowData) => {
326+
detailPanel={({ rowData }) => {
327327
return (
328328
<div
329329
style={{
@@ -352,7 +352,7 @@ export function MultipleDetailPanels() {
352352
detailPanel={[
353353
{
354354
tooltip: 'Show Name',
355-
render: (rowData) => {
355+
render: ({ rowData }) => {
356356
return (
357357
<div
358358
style={{
@@ -370,7 +370,7 @@ export function MultipleDetailPanels() {
370370
{
371371
icon: 'account_circle',
372372
tooltip: 'Show Surname',
373-
render: (rowData) => {
373+
render: ({ rowData }) => {
374374
return (
375375
<div
376376
style={{
@@ -389,7 +389,7 @@ export function MultipleDetailPanels() {
389389
icon: 'favorite_border',
390390
openIcon: 'favorite',
391391
tooltip: 'Show Both',
392-
render: (rowData) => {
392+
render: ({ rowData }) => {
393393
return (
394394
<div
395395
style={{

src/components/m-table-body-row.js

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import React from 'react';
22
import Checkbox from '@material-ui/core/Checkbox';
33
import TableCell from '@material-ui/core/TableCell';
4-
import TableRow from '@material-ui/core/TableRow';
54
import IconButton from '@material-ui/core/IconButton';
65
import Icon from '@material-ui/core/Icon';
76
import Tooltip from '@material-ui/core/Tooltip';
8-
import Collapse from '@material-ui/core/Collapse';
7+
import TableRow from '@material-ui/core/TableRow';
8+
import { MTableDetailPanel } from './m-table-detailpanel';
99
import PropTypes from 'prop-types';
1010
import * as CommonValues from '../utils/common-values';
1111
import { useDoubleClick } from '../utils/hooks/useDoubleClick';
@@ -358,9 +358,7 @@ export default function MTableBodyRow(props) {
358358
};
359359

360360
const getStyle = (index, level) => {
361-
let style = {
362-
transition: 'all ease 300ms'
363-
};
361+
let style = {};
364362

365363
if (typeof props.options.rowStyle === 'function') {
366364
style = {
@@ -439,7 +437,6 @@ export default function MTableBodyRow(props) {
439437
/>
440438
);
441439
});
442-
443440
return (
444441
<>
445442
<TableRow
@@ -451,38 +448,13 @@ export default function MTableBodyRow(props) {
451448
>
452449
{renderColumns}
453450
</TableRow>
454-
<TableRow
455-
// selected={props.index % 2 === 0}
456-
>
457-
{props.options.detailPanelOffset.left > 0 && (
458-
<TableCell colSpan={props.options.detailPanelOffset.left} />
459-
)}
460-
<TableCell
461-
size={size}
462-
colSpan={
463-
renderColumns.length -
464-
props.options.detailPanelOffset.left -
465-
props.options.detailPanelOffset.right
466-
}
467-
padding="none"
468-
>
469-
{(typeof props.detailPanel === 'function') &&
470-
<Collapse in={Boolean(props.data.tableData && props.data.tableData.showDetailPanel)} timeout="auto" unmountOnExit>
471-
{props.detailPanel(props.data)}
472-
</Collapse>
473-
}
474-
{(typeof props.detailPanel === 'object') &&
475-
props.detailPanel.map((panel, index) => {
476-
return (
477-
<Collapse key={index} in={Boolean(props.data.tableData && props.data.tableData.showDetailPanel) && (props.data.tableData.showDetailPanel || '').toString() ===
478-
panel.render.toString()} timeout="auto" unmountOnExit>
479-
{panel.render(props.data)}
480-
</Collapse>
481-
)
482-
})
483-
}
484-
</TableCell>
485-
</TableRow>
451+
<MTableDetailPanel
452+
options={props.options}
453+
data={props.data}
454+
detailPanel={props.detailPanel}
455+
renderColumns={renderColumns}
456+
size={size}
457+
/>
486458
{props.data.tableData.childRows &&
487459
props.data.tableData.isTreeExpanded &&
488460
props.data.tableData.childRows.map((data, index) => {

src/components/m-table-detailpanel.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import React from 'react';
2+
import { TableCell, Collapse, TableRow } from '@material-ui/core';
3+
4+
function MTableDetailPanel(props) {
5+
const [isOpen, setOpen] = React.useState(false);
6+
const [, rerender] = React.useReducer((s) => s + 1, 0);
7+
const renderRef = React.useRef();
8+
React.useEffect(() => {
9+
const shouldOpen = Boolean(
10+
props.data.tableData && props.data.tableData.showDetailPanel
11+
);
12+
setTimeout(() => {
13+
setOpen(shouldOpen);
14+
}, 5);
15+
}, [props.data.tableData.showDetailPanel]);
16+
17+
let renderFunction;
18+
if (typeof props.detailPanel === 'function') {
19+
renderFunction = props.detailPanel;
20+
} else {
21+
renderFunction = props.detailPanel
22+
? props.detailPanel.find(
23+
(panel) =>
24+
panel.render.toString() ===
25+
(props.data.tableData.showDetailPanel || '').toString()
26+
)
27+
: undefined;
28+
renderFunction = renderFunction ? renderFunction.render : null;
29+
}
30+
React.useEffect(() => {
31+
if (renderFunction && isOpen) {
32+
renderRef.current = renderFunction;
33+
}
34+
});
35+
36+
if (!renderRef.current && !props.data.tableData.showDetailPanel) {
37+
return null;
38+
}
39+
const Render = renderFunction || renderRef.current;
40+
return (
41+
<TableRow>
42+
{props.options.detailPanelOffset.left > 0 && (
43+
<TableCell colSpan={props.options.detailPanelOffset.left} />
44+
)}
45+
<TableCell
46+
size={props.size}
47+
colSpan={
48+
props.renderColumns.length -
49+
props.options.detailPanelOffset.left -
50+
props.options.detailPanelOffset.right
51+
}
52+
padding="none"
53+
>
54+
<Collapse
55+
in={isOpen}
56+
timeout="auto"
57+
unmountOnExit
58+
mountOnEnter
59+
onExited={() => {
60+
renderRef.current = undefined;
61+
rerender();
62+
}}
63+
>
64+
<Render rowData={props.data} />
65+
</Collapse>
66+
</TableCell>
67+
</TableRow>
68+
);
69+
}
70+
71+
export { MTableDetailPanel };

types/index.d.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ export interface MaterialTableProps<RowData extends object> {
2525
components?: Components;
2626
data: RowData[] | ((query: Query<RowData>) => Promise<QueryResult<RowData>>);
2727
detailPanel?:
28-
| ((rowData: RowData) => React.ReactNode)
29-
| (DetailPanel<RowData> | ((rowData: RowData) => DetailPanel<RowData>))[];
28+
| (({ rowData }: { rowData: RowData }) => React.ReactNode)
29+
| (
30+
| DetailPanel<RowData>
31+
| (({ rowData }: { rowData: RowData }) => DetailPanel<RowData>)
32+
)[];
3033
editable?: {
3134
isEditable?: (rowData: RowData) => boolean;
3235
isDeletable?: (rowData: RowData) => boolean;
@@ -50,8 +53,8 @@ export interface MaterialTableProps<RowData extends object> {
5053
options?: Options<RowData>;
5154
parentChildData?: (row: RowData, rows: RowData[]) => RowData | undefined;
5255
localization?: Localization;
53-
onChangeRowsPerPage?: (pageSize: number) => void;
54-
onChangePage?: (page: number, pageSize: number) => void;
56+
onRowsPerPageChange?: (pageSize: number) => void;
57+
onPageChange?: (page: number, pageSize: number) => void;
5558
onChangeColumnHidden?: (column: Column<RowData>, hidden: boolean) => void;
5659
onColumnDragged?: (sourceIndex: number, destinationIndex: number) => void;
5760
onOrderChange?: (orderBy: number, orderDirection: 'asc' | 'desc') => void;
@@ -127,7 +130,7 @@ export interface DetailPanel<RowData extends object> {
127130
icon?: string | React.ComponentType<any>;
128131
openIcon?: string | React.ComponentType<any>;
129132
tooltip?: string;
130-
render: (rowData: RowData) => string | React.ReactNode;
133+
render: ({ rowData }: { rowData: RowData }) => string | React.ReactNode;
131134
}
132135

133136
export interface Action<RowData extends object> {

0 commit comments

Comments
 (0)