Skip to content

Commit

Permalink
column resizing ok
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrn committed Aug 23, 2020
1 parent 9c32f97 commit 3ff5821
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 27 deletions.
13 changes: 4 additions & 9 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,15 +428,15 @@ class App extends Component {
filterPlaceholder: "Adı filter",
tooltip: "This is tooltip text",
editPlaceholder: "This is placeholder",
width: "20%",
maxWidth: 50,
},
{
width: 100,
title: "Soyadı",
field: "surname",
initialEditValue: "test",
tooltip: "This is tooltip text",
editable: "never",
resizable: false,
},
{ title: "Evli", field: "isMarried" },
{
Expand All @@ -454,12 +454,6 @@ class App extends Component {
},
{ title: "Kayıt Tarihi", field: "insertDateTime", type: "datetime" },
{ title: "Zaman", field: "time", type: "time" },
{
title: "Adı",
field: "name",
filterPlaceholder: "Adı filter",
tooltip: "This is tooltip text",
},
],
remoteColumns: [
{
Expand Down Expand Up @@ -490,7 +484,7 @@ class App extends Component {
<MaterialTable
tableRef={this.tableRef}
columns={this.state.columns}
data={[] || this.state.data}
data={this.state.data}
title="Demo Title"
onFilterChange={(appliedFilter) => {
console.log("selected Filters : ", appliedFilter);
Expand All @@ -511,6 +505,7 @@ class App extends Component {
// }}
options={{
tableLayout: "fixed",
columnResizable: true,
headerSelectionProps: {
color: "primary",
},
Expand Down
2 changes: 2 additions & 0 deletions src/components/m-table-body-row.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default class MTableBodyRow extends React.Component {
rowData={this.props.data}
cellEditable={this.props.cellEditable}
onCellEditFinished={this.props.onCellEditFinished}
scrollWidth={this.props.scrollWidth}
/>
);
} else {
Expand All @@ -68,6 +69,7 @@ export default class MTableBodyRow extends React.Component {
columnDef.editable !== "never" && !!this.props.cellEditable
}
onCellEditStarted={this.props.onCellEditStarted}
scrollWidth={this.props.scrollWidth}
/>
);
}
Expand Down
7 changes: 7 additions & 0 deletions src/components/m-table-body.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class MTableBody extends React.Component {
onEditingApproved={this.props.onEditingApproved}
getFieldValue={this.props.getFieldValue}
onBulkEditRowChanged={this.props.onBulkEditRowChanged}
scrollWidth={this.props.scrollWidth}
/>
);
} else {
Expand Down Expand Up @@ -137,6 +138,7 @@ class MTableBody extends React.Component {
cellEditable={this.props.cellEditable}
onCellEditStarted={this.props.onCellEditStarted}
onCellEditFinished={this.props.onCellEditFinished}
scrollWidth={this.props.scrollWidth}
/>
);
}
Expand Down Expand Up @@ -177,6 +179,7 @@ class MTableBody extends React.Component {
onCellEditStarted={this.props.onCellEditStarted}
onCellEditFinished={this.props.onCellEditFinished}
onBulkEditRowChanged={this.props.onBulkEditRowChanged}
scrollWidth={this.props.scrollWidth}
/>
));
}
Expand Down Expand Up @@ -221,6 +224,7 @@ class MTableBody extends React.Component {
filterCellStyle={this.props.options.filterCellStyle}
filterRowStyle={this.props.options.filterRowStyle}
hideFilterIcons={this.props.options.hideFilterIcons}
scrollWidth={this.props.scrollWidth}
/>
)}
{this.props.showAddRow &&
Expand All @@ -247,6 +251,7 @@ class MTableBody extends React.Component {
onEditingCanceled={this.props.onEditingCanceled}
onEditingApproved={this.props.onEditingApproved}
getFieldValue={this.props.getFieldValue}
scrollWidth={this.props.scrollWidth}
/>
)}

Expand Down Expand Up @@ -277,6 +282,7 @@ class MTableBody extends React.Component {
onEditingCanceled={this.props.onEditingCanceled}
onEditingApproved={this.props.onEditingApproved}
getFieldValue={this.props.getFieldValue}
scrollWidth={this.props.scrollWidth}
/>
)}
{this.renderEmpty(emptyRowCount, renderData)}
Expand Down Expand Up @@ -318,6 +324,7 @@ MTableBody.propTypes = {
renderData: PropTypes.array,
initialFormData: PropTypes.object,
selection: PropTypes.bool.isRequired,
scrollWidth: PropTypes.number.isRequired,
showAddRow: PropTypes.bool,
treeDataMaxLevel: PropTypes.number,
localization: PropTypes.object,
Expand Down
11 changes: 10 additions & 1 deletion src/components/m-table-cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import * as React from "react";
import TableCell from "@material-ui/core/TableCell";
import PropTypes from "prop-types";
import parseISO from "date-fns/parseISO";
import * as CommonValues from "../utils/common-values";

/* eslint-enable no-unused-vars */

/* eslint-disable no-useless-escape */
Expand Down Expand Up @@ -115,9 +117,16 @@ export default class MTableCell extends React.Component {
};

getStyle = () => {
const width = CommonValues.reducePercentsInCalc(
this.props.columnDef.tableData.width,
this.props.scrollWidth
);

let cellStyle = {
color: "inherit",
width: this.props.columnDef.tableData.width,
width,
maxWidth: this.props.columnDef.maxWidth,
minWidth: this.props.columnDef.minWidth,
boxSizing: "border-box",
fontSize: "inherit",
fontFamily: "inherit",
Expand Down
4 changes: 4 additions & 0 deletions src/components/m-table-group-row.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export default class MTableGroupRow extends React.Component {
cellEditable={this.props.cellEditable}
onCellEditStarted={this.props.onCellEditStarted}
onCellEditFinished={this.props.onCellEditFinished}
scrollWidth={this.props.scrollWidth}
/>
));
} else {
Expand All @@ -71,6 +72,7 @@ export default class MTableGroupRow extends React.Component {
onEditingApproved={this.props.onEditingApproved}
getFieldValue={this.props.getFieldValue}
onBulkEditRowChanged={this.props.onBulkEditRowChanged}
scrollWidth={this.props.scrollWidth}
/>
);
} else {
Expand All @@ -97,6 +99,7 @@ export default class MTableGroupRow extends React.Component {
cellEditable={this.props.cellEditable}
onCellEditStarted={this.props.onCellEditStarted}
onCellEditFinished={this.props.onCellEditFinished}
scrollWidth={this.props.scrollWidth}
/>
);
}
Expand Down Expand Up @@ -189,6 +192,7 @@ MTableGroupRow.propTypes = {
onEditingApproved: PropTypes.func,
options: PropTypes.object,
path: PropTypes.arrayOf(PropTypes.number),
scrollWidth: PropTypes.number.isRequired,
cellEditable: PropTypes.object,
onCellEditStarted: PropTypes.func,
onCellEditFinished: PropTypes.func,
Expand Down
33 changes: 20 additions & 13 deletions src/components/m-table-header.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,14 @@ export class MTableHeader extends React.Component {
return;
}

const additionalWidth =
let additionalWidth =
this.state.lastAdditionalWidth + e.clientX - this.state.lastX;

additionalWidth = Math.min(
this.state.resizingColumnDef.maxWidth || additionalWidth,
additionalWidth
);

if (
this.state.resizingColumnDef.tableData.additionalWidth !== additionalWidth
) {
Expand All @@ -63,16 +68,24 @@ export class MTableHeader extends React.Component {
};

getCellStyle = (columnDef) => {
const width = CommonValues.reducePercentsInCalc(
columnDef.tableData.width,
this.props.scrollWidth
);

const style = {
...this.props.headerStyle,
...columnDef.headerStyle,
boxSizing: "border-box",
width: columnDef.tableData.width,
width,
maxWidth: columnDef.maxWidth,
minWidth: columnDef.minWidth,
};

if (
this.props.options.tableLayout === "resizable" ||
this.props.options.tableLayout === "fixed"
this.props.options.tableLayout === "fixed" &&
this.props.options.columnResizable &&
columnDef.resizable !== false
) {
style.paddingRight = 2;
}
Expand Down Expand Up @@ -153,8 +166,9 @@ export class MTableHeader extends React.Component {
}

if (
this.props.options.tableLayout === "resizable" ||
this.props.options.tableLayout === "fixed"
this.props.options.tableLayout === "fixed" &&
this.props.options.columnResizable &&
columnDef.resizable !== false
) {
content = (
<div style={{ display: "flex", alignItems: "center" }}>
Expand All @@ -163,19 +177,12 @@ export class MTableHeader extends React.Component {
<this.props.icons.Resize
style={{
cursor: "e-resize",
transition: "all ease 400ms",
color:
this.state.resizingColumnDef &&
this.state.resizingColumnDef.tableData.id ===
columnDef.tableData.id
? this.props.theme.palette.primary.main
: "inherit",
transform:
this.state.resizingColumnDef &&
this.state.resizingColumnDef.tableData.id ===
columnDef.tableData.id
? "scale(1.25)"
: "none",
}}
onMouseDown={(e) => this.handleMouseDown(e, columnDef)}
/>
Expand Down
8 changes: 6 additions & 2 deletions src/default-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,12 @@ export const defaultProps = {
</Icon>
)),
Resize: React.forwardRef((props, ref) => (
<Icon {...props} ref={ref}>
more_vert
<Icon
{...props}
ref={ref}
style={{ ...props.style, transform: "rotate(90deg)" }}
>
drag_handle
</Icon>
)),
Search: React.forwardRef((props, ref) => (
Expand Down
2 changes: 2 additions & 0 deletions src/material-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,7 @@ export default class MaterialTable extends React.Component {
treeDataMaxLevel={this.state.treeDataMaxLevel}
options={props.options}
onColumnResized={this.onColumnResized}
scrollWidth={this.state.width}
/>
)}
<props.components.Body
Expand Down Expand Up @@ -903,6 +904,7 @@ export default class MaterialTable extends React.Component {
onCellEditFinished={this.onCellEditFinished}
bulkEditOpen={this.dataManager.bulkEditOpen}
onBulkEditRowChanged={this.dataManager.onBulkEditRowChanged}
scrollWidth={this.state.width}
/>
</Table>
);
Expand Down
21 changes: 21 additions & 0 deletions src/utils/common-values.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,24 @@ export const actionsColumnWidth = (props) =>
rowActions(props).length * baseIconSize(props);
export const selectionMaxWidth = (props, maxTreeLevel) =>
baseIconSize(props) + 9 * maxTreeLevel;

export const reducePercentsInCalc = (calc, fullValue) => {
let index = calc.indexOf("%");
while (index !== -1) {
let leftIndex = index - 1;
while (leftIndex >= 0 && "0123456789.".indexOf(calc[leftIndex]) !== -1) {
leftIndex--;
}
leftIndex++;

const value = Number.parseFloat(calc.substring(leftIndex, index));
calc =
calc.substring(0, leftIndex) +
(value * fullValue) / 100 +
"px" +
calc.substring(index + 1);
index = calc.indexOf("%");
}

return calc;
};
4 changes: 2 additions & 2 deletions src/utils/data-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,8 @@ export default class DataManager {
column.tableData.additionalWidth = additionalWidth;
column.tableData.width = `calc(${column.tableData.initialWidth} + ${column.tableData.additionalWidth}px)`;

nextColumn.tableData.additionalWidth = -1 * additionalWidth;
nextColumn.tableData.width = `calc(${nextColumn.tableData.initialWidth} + ${nextColumn.tableData.additionalWidth}px)`;
// nextColumn.tableData.additionalWidth = -1 * additionalWidth;
// nextColumn.tableData.width = `calc(${nextColumn.tableData.initialWidth} + ${nextColumn.tableData.additionalWidth}px)`;

// console.log("F i: " + column.tableData.initialWidth);
// console.log("F a: " + column.tableData.additionalWidth);
Expand Down
2 changes: 2 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ export interface Column<RowData extends object> {
| "never"
| ((columnDef: Column<RowData>, rowData: RowData) => boolean);
removable?: boolean;
resizable?: boolean;
validate?: (
rowData: RowData
) => { isValid: boolean; helperText?: string } | string | boolean;
Expand Down Expand Up @@ -295,6 +296,7 @@ export interface Options<RowData extends object> {
actionsColumnIndex?: number;
addRowPosition?: "first" | "last";
columnsButton?: boolean;
columnResizable?: boolean;
defaultExpanded?: boolean | ((rowData: any) => boolean);
debounceInterval?: number;
detailPanelType?: "single" | "multiple";
Expand Down

0 comments on commit 3ff5821

Please sign in to comment.