Skip to content

Commit

Permalink
fix: display export button for selected state
Browse files Browse the repository at this point in the history
  • Loading branch information
Domino987 committed Jun 27, 2022
1 parent f204080 commit cf6c06b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 29 deletions.
38 changes: 11 additions & 27 deletions src/components/MTableToolbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export function MTableToolbar(props) {
const options = useOptionStore();

const selectedRows = React.useMemo(
() => props.selectedRows.filter((a) => a.tableData.checked),
[props.selectedRows]
() => props.originalData.filter((a) => a.tableData.checked),
[props.originalData]
);

const onSearchChange = (searchText) => {
Expand Down Expand Up @@ -127,12 +127,12 @@ export function MTableToolbar(props) {
}
}

function renderDefaultActions() {
function renderDefaultActions(isSelectionActive) {
const { classes } = props;

const diplayedActions = isSelectionActive ? 'toolbarOnSelect' : 'toolbar';
return (
<div style={{ display: 'flex' }}>
{options.columnsButton && (
{options.columnsButton && !isSelectionActive && (
<span>
<Tooltip title={localization.showColumnsTitle}>
<IconButton
Expand Down Expand Up @@ -243,39 +243,22 @@ export function MTableToolbar(props) {
<props.components.Actions
actions={
props.actions &&
props.actions.filter((a) => a.position === 'toolbar')
props.actions.filter((a) => a.position === diplayedActions)
}
data={isSelectionActive ? selectedRows : undefined}
components={props.components}
/>
</span>
</div>
);
}

function renderSelectedActions() {
return (
<React.Fragment>
<props.components.Actions
actions={props.actions.filter(
(a) => a.position === 'toolbarOnSelect'
)}
data={selectedRows}
components={props.components}
/>
</React.Fragment>
);
}

function renderActions() {
const { classes } = props;

return (
<div className={classes.actions}>
<div>
{selectedRows.length > 0
? renderSelectedActions()
: renderDefaultActions()}
</div>
<div>{renderDefaultActions(selectedRows.length > 0)}</div>
</div>
);
}
Expand Down Expand Up @@ -311,6 +294,7 @@ export function MTableToolbar(props) {
: options.showTitle
? props.title
: null;

return (
<Toolbar
ref={props.forwardedRef}
Expand All @@ -334,7 +318,7 @@ MTableToolbar.defaultProps = {
columns: [],
columnsHiddenInColumnsButton: false, // By default, all columns are shown in the Columns Button (columns action when `options.columnsButton = true`)
searchText: '',
selectedRows: [],
originalData: [],
title: 'No Title!'
};

Expand All @@ -347,7 +331,7 @@ MTableToolbar.propTypes = {
dataManager: PropTypes.object.isRequired,
searchText: PropTypes.string,
onSearchChanged: PropTypes.func.isRequired,
selectedRows: PropTypes.array,
originalData: PropTypes.array,
title: PropTypes.oneOfType([PropTypes.element, PropTypes.string]),
data: PropTypes.func,
classes: PropTypes.object
Expand Down
3 changes: 2 additions & 1 deletion src/material-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,7 @@ export default class MaterialTable extends React.Component {
};
getRenderData = () =>
this.props.options.exportAll ? this.state.data : this.state.renderData;

render() {
const props = this.getProps();
return (
Expand All @@ -1089,7 +1090,7 @@ export default class MaterialTable extends React.Component {
<this.props.components.Toolbar
actions={this.state.actions}
components={this.props.components}
selectedRows={this.state.originalData}
originalData={this.state.originalData}
columns={this.state.columns}
getFieldValue={this.dataManager.getFieldValue}
data={this.getRenderData}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/data-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ export default class DataManager {
lastEditingRow: this.lastEditingRow,
orderBy: this.orderBy,
orderDirection: this.orderDirection,
originalData: this.data,
originalData: [...this.data],
pageSize: this.pageSize,
renderData: this.pagedData,
searchText: this.searchText,
Expand Down

0 comments on commit cf6c06b

Please sign in to comment.