| 1 |
/** |
| 2 |
* body.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 |
RowsType, |
| 11 |
RowType, |
| 12 |
RowKeyType, |
| 13 |
RenderersTypes, |
| 14 |
createElementType, |
| 15 |
ColumnsType |
| 16 |
} from '../../../../components/Table/base/types'; |
| 17 |
import { ProviderContext } from '../../../../components/Table/base/provider'; |
| 18 |
import { resolveRowKey } from './resolve-row-key'; |
| 19 |
import { BodyRow } from './body-row'; |
| 20 |
|
| 21 |
export interface BodyProps { |
| 22 |
onRow?: Function; |
| 23 |
rows: RowsType; |
| 24 |
rowKey?: RowKeyType; |
| 25 |
columns?: ColumnsType; |
| 26 |
renderers?: RenderersTypes['renderers']; |
| 27 |
mappedRows?: any; |
| 28 |
className?: string; |
| 29 |
} |
| 30 |
|
| 31 |
class BaseBody extends Component<BodyProps, {}> { |
| 32 |
static defaultProps = { |
| 33 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 34 |
onRow: (...args: any) => ({}) |
| 35 |
}; |
| 36 |
|
| 37 |
shouldComponentUpdate(nextProps: BodyProps) { |
| 38 |
// Skip checking props against `onRow` since that can be bound at render(). |
| 39 |
// That's not particularly good practice but you never know how the users |
| 40 |
// prefer to define the handler. |
| 41 |
// Check for wrapper based override. |
| 42 |
const { renderers } = nextProps; |
| 43 |
|
| 44 |
if ( |
| 45 |
renderers && |
| 46 |
renderers.body && |
| 47 |
renderers.body.wrapper && |
| 48 |
(renderers.body.wrapper as React.Component).shouldComponentUpdate |
| 49 |
) { |
| 50 |
if (typeof (renderers.body.wrapper as React.Component).shouldComponentUpdate === 'function') { |
| 51 |
return (renderers.body.wrapper as React.Component).shouldComponentUpdate.call(this, nextProps, {}, {}); |
| 52 |
} |
| 53 |
return true; |
| 54 |
} |
| 55 |
|
| 56 |
return !isEqual(this.omitOnRow(this.props), this.omitOnRow(nextProps)); |
| 57 |
} |
| 58 |
|
| 59 |
omitOnRow = (props: BodyProps) => { |
| 60 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 61 |
const { onRow, ...ret } = props; |
| 62 |
return ret; |
| 63 |
}; |
| 64 |
|
| 65 |
render() { |
| 66 |
const { onRow, rows, rowKey, columns, renderers, ...props } = this.props; |
| 67 |
const children = (rows as []).map((rowData: RowType, index: number) => { |
| 68 |
const key = resolveRowKey({ rowData, rowIndex: index, rowKey }); |
| 69 |
return createElement(BodyRow, { |
| 70 |
key, |
| 71 |
renderers: renderers.body, |
| 72 |
onRow, |
| 73 |
rowKey: key, |
| 74 |
rowIndex: index, |
| 75 |
rowData, |
| 76 |
columns |
| 77 |
}); |
| 78 |
}); |
| 79 |
|
| 80 |
return createElement(renderers.body.wrapper as createElementType, props, children); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
export const Body = (props: BodyProps) => ( |
| 85 |
<ProviderContext.Consumer> |
| 86 |
{({ columns, renderers }) => <BaseBody columns={columns} renderers={renderers} {...props} />} |
| 87 |
</ProviderContext.Consumer> |
| 88 |
); |