Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added expandable row header (expandableRowsHeader option) #1091

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ The component accepts the following props:
|**`isRowExpandable`**|function||Enable/disable expansion or collapse on certain expandable rows with custom function. Will be considered true if not provided. `function(dataIndex: number, expandedRows: object(lookup: {dataIndex: number}, data: arrayOfObjects: {index: number, dataIndex: number})) => boolean`.
|**`selectableRowsHeader`**|boolean|true|Show/hide the select all/deselect all checkbox header for selectable rows
|**`expandableRows`**|boolean|false|Enable/disable expandable rows
|**`expandableRowsHeader`**|boolean|true|Show/hide the expand all/collapse all row header for expandable rows.
|**`expandableRowsOnClick`**|boolean|false|Enable/disable expand trigger when row is clicked. When False, only expand icon will trigger this action.
|**`renderExpandableRow`**|function||Render expandable row. `function(rowData, rowMeta) => React Component`
|**`resizableColumns`**|boolean|false|Enable/disable resizable columns
Expand Down
1 change: 1 addition & 0 deletions examples/expandable-rows/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class Example extends React.Component {
filterType: 'dropdown',
responsive: 'scrollMaxHeight',
expandableRows: true,
expandableRowsHeader: true,
expandableRowsOnClick: true,
isRowExpandable: (dataIndex, expandedRows) => {
// Prevent expand/collapse of any row if there are 4 rows expanded already (but allow those already expanded to be collapsed)
Expand Down
61 changes: 61 additions & 0 deletions src/MUIDataTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ class MUIDataTable extends React.Component {
textLabels: PropTypes.object,
pagination: PropTypes.bool,
expandableRows: PropTypes.bool,
expandableRowsHeader: PropTypes.bool,
expandableRowsOnClick: PropTypes.bool,
renderExpandableRow: PropTypes.func,
customToolbar: PropTypes.oneOfType([PropTypes.func, PropTypes.element]),
Expand Down Expand Up @@ -303,6 +304,7 @@ class MUIDataTable extends React.Component {
textLabels,
serverSideFilterList: [],
expandableRows: false,
expandableRowsHeader: true,
expandableRowsOnClick: false,
resizableColumns: false,
selectableRows: 'multiple',
Expand Down Expand Up @@ -1139,6 +1141,62 @@ class MUIDataTable extends React.Component {
);
};

// Collapses or expands all expanded rows
toggleAllExpandableRows = () => {
let expandedRowsData = [...this.state.expandedRows.data];
const { isRowExpandable } = this.options;
let affecttedRows = [];

if (expandedRowsData.length > 0) {
// collapse all
for (let ii = expandedRowsData.length - 1; ii >= 0; ii--) {
let item = expandedRowsData[ii];
if (!isRowExpandable || (isRowExpandable && isRowExpandable(item.dataIndex, this.state.expandedRows))) {
affecttedRows.push(expandedRowsData.splice(ii, 1));
}
}

} else {
// expand all
for (let ii = 0; ii < this.state.data.length; ii++) {
let item = this.state.data[ii];
if (!isRowExpandable || (isRowExpandable && isRowExpandable(item.dataIndex, this.state.expandedRows))) {
if ( this.state.expandedRows.lookup[item.index] !== true ) {
let newItem = {
index: ii,
dataIndex: item.index
};
expandedRowsData.push(newItem);
affecttedRows.push(newItem);
}
}
}
}

this.setState(
{
expandedRows: {
lookup: buildMap(expandedRowsData),
data: expandedRowsData,
},
},
() => {
this.setTableAction('expandRow');
if (this.options.onRowExpansionChange) {
this.options.onRowExpansionChange(
affecttedRows,
this.state.expandedRows.data,
this.state.expandedRows.data.map(item => item.dataIndex),
);
}
},
);
}

areAllRowsExpanded = () => {
return this.state.expandedRows.data.length === this.state.data.length;
}

selectRowUpdate = (type, value, shiftAdjacentRows = []) => {
// safety check
const { selectableRows } = this.options;
Expand Down Expand Up @@ -1435,6 +1493,9 @@ class MUIDataTable extends React.Component {
selectRowUpdate={this.selectRowUpdate}
toggleSort={this.toggleSortColumn}
setCellRef={this.setHeadCellRef}
expandedRows={expandedRows}
areAllRowsExpanded={this.areAllRowsExpanded}
toggleAllExpandableRows={this.toggleAllExpandableRows}
options={this.options}
/>
<TableBody
Expand Down
5 changes: 4 additions & 1 deletion src/components/TableHead.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class TableHead extends React.Component {
};

render() {
const { classes, columns, count, options, data, setCellRef, selectedRows } = this.props;
const { classes, columns, count, options, data, setCellRef, selectedRows, expandedRows } = this.props;

const numSelected = (selectedRows && selectedRows.data.length) || 0;
let isIndeterminate = numSelected > 0 && numSelected < count;
Expand Down Expand Up @@ -65,11 +65,14 @@ class TableHead extends React.Component {
indeterminate={isIndeterminate}
checked={isChecked}
isHeaderCell={true}
expandedRows={expandedRows}
expandableRowsHeader={options.expandableRowsHeader}
expandableOn={options.expandableRows}
selectableOn={options.selectableRows}
fixedHeader={options.fixedHeader}
fixedHeaderOptions={options.fixedHeaderOptions}
selectableRowsHeader={options.selectableRowsHeader}
onExpand={this.props.toggleAllExpandableRows}
isRowSelectable={true}
/>
{columns.map(
Expand Down
26 changes: 21 additions & 5 deletions src/components/TableSelectCell.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import TableCell from '@material-ui/core/TableCell';
import IconButton from '@material-ui/core/IconButton';
import { withStyles } from '@material-ui/core/styles';
import KeyboardArrowRight from '@material-ui/icons/KeyboardArrowRight';
import Remove from '@material-ui/icons/Remove';

const defaultSelectCellStyles = theme => ({
root: {},
Expand Down Expand Up @@ -90,6 +91,9 @@ class TableSelectCell extends React.Component {
isRowSelectable,
selectableRowsHeader,
hideExpandButton,
expandableRowsHeader,
expandedRows,
areAllRowsExpanded = () => (false),
...otherProps
} = this.props;
let fixedHeaderClasses;
Expand Down Expand Up @@ -117,8 +121,12 @@ class TableSelectCell extends React.Component {

const iconClass = classNames({
[classes.icon]: true,
[classes.hide]: isHeaderCell,
[classes.expanded]: isRowExpanded,
[classes.hide]: isHeaderCell && !expandableRowsHeader,
[classes.expanded]: isRowExpanded || (isHeaderCell && areAllRowsExpanded()),
});
const iconIndeterminateClass = classNames({
[classes.icon]: true,
[classes.hide]: isHeaderCell && !expandableRowsHeader,
});

const renderCheckBox = () => {
Expand All @@ -144,9 +152,17 @@ class TableSelectCell extends React.Component {
<TableCell className={cellClass} padding="checkbox">
<div style={{ display: 'flex', alignItems: 'center' }}>
{expandableOn && (
<IconButton onClick={onExpand} disabled={isHeaderCell} className={buttonClass}>
<KeyboardArrowRight id="expandable-button" className={iconClass} />
</IconButton>
<React.Fragment>
{(isHeaderCell && !areAllRowsExpanded() && expandedRows && expandedRows.data.length > 0 ) ?
<IconButton onClick={onExpand} style={{padding:0}} disabled={expandableRowsHeader === false}>
<Remove id="expandable-button" className={iconIndeterminateClass} />
</IconButton>
:
<IconButton onClick={onExpand} style={{padding:0}} disabled={expandableRowsHeader === false}>
<KeyboardArrowRight id="expandable-button" className={iconClass} />
</IconButton>
}
</React.Fragment>
)}
{selectableOn !== 'none' && renderCheckBox()}
</div>
Expand Down