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

[DataGrid] Move column resizing to XGrid only #257

Merged
merged 19 commits into from
Sep 11, 2020
Merged
Show file tree
Hide file tree
Changes from 8 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { DataGrid } from '@material-ui/data-grid';
import { XGrid } from '@material-ui/x-grid';

const rows = [
{
Expand All @@ -12,7 +12,7 @@ const rows = [
export default function ColumnWidthGrid() {
return (
<div style={{ height: 250, width: '100%' }}>
<DataGrid
<XGrid
columns={[
{ field: 'id', resizable: false },
{ field: 'username' },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { DataGrid } from '@material-ui/data-grid';
import { XGrid } from '@material-ui/x-grid';

const rows = [
{
Expand All @@ -12,7 +12,7 @@ const rows = [
export default function ColumnWidthGrid() {
return (
<div style={{ height: 250, width: '100%' }}>
<DataGrid
<XGrid
columns={[
{ field: 'id', resizable: false },
{ field: 'username' },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import * as React from 'react';
import { DataGrid } from '@material-ui/data-grid';
import { XGrid } from '@material-ui/x-grid';

const rows = [
{
id: 1,
username: 'defunkt',
username: 'material-ui',
age: 38,
},
];

export default function ColumnWidthGrid() {
return (
<div style={{ height: 250, width: '100%' }}>
<DataGrid
<XGrid
columns={[
{ field: 'id' },
{ field: 'username', width: 200 },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { DataGrid } from '@material-ui/data-grid';
import { XGrid } from '@material-ui/x-grid';

const rows = [
{
Expand All @@ -12,7 +12,7 @@ const rows = [
export default function ColumnWidthGrid() {
return (
<div style={{ height: 250, width: '100%' }}>
<DataGrid
<XGrid
dtassone marked this conversation as resolved.
Show resolved Hide resolved
columns={[
{ field: 'id' },
{ field: 'username', width: 200 },
Expand Down
4 changes: 3 additions & 1 deletion docs/src/pages/components/data-grid/columns/columns.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ To change the width of a column, use the `width` property available in `ColDef`.

{{"demo": "pages/components/data-grid/columns/ColumnWidthGrid.js"}}

## Column sizing
## Column sizing ⚡️

By default, all columns can be resized by dragging the right portion of the column.
To prevent the resizing of a column, set `resizable: false` in the `ColDef`.
Expand All @@ -68,6 +68,8 @@ To prevent the resizing of a column, set `resizable: false` in the `ColDef`.
- https://www.jqwidgets.com/react/react-grid/#https://www.jqwidgets.com/react/react-grid/react-grid-columnsresize.htm
-->

Alternatively, to disable all columns resize, set the prop `disableColumnResize` to `true`.
oliviertassinari marked this conversation as resolved.
Show resolved Hide resolved

## Column types

To facilitate configuration of the columns, some column types are predefined.
Expand Down
3 changes: 1 addition & 2 deletions packages/grid/data-grid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"author": "Material-UI Team",
"main": "dist/index-cjs.js",
"module": "dist/index-esm.js",
"types": "dist/data-grid.all.d.ts",
"types": "dist/data-grid.d.ts",
"files": [
"dist/*"
],
Expand All @@ -31,7 +31,6 @@
"@rollup/plugin-node-resolve": "^8.0.1",
"rollup": "^2.6.1",
"rollup-plugin-cleaner": "^1.0.0",
"rollup-plugin-command": "^1.1.3",
"rollup-plugin-dts": "^1.4.7",
"rollup-plugin-sourcemaps": "^0.6.2",
"rollup-plugin-terser": "^5.3.0",
Expand Down
13 changes: 1 addition & 12 deletions packages/grid/data-grid/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import cleaner from 'rollup-plugin-cleaner';
import sourceMaps from 'rollup-plugin-sourcemaps';
import { terser } from 'rollup-plugin-terser';
import dts from 'rollup-plugin-dts';
import command from 'rollup-plugin-command';
import pkg from './package.json';

// dev build if watching, prod build if not
Expand Down Expand Up @@ -42,16 +41,6 @@ export default [
{
input: './dist/index.d.ts',
output: [{ file: 'dist/data-grid.d.ts', format: 'es' }],
plugins: [
dts(),
command(
`cat ../x-grid-modules/dist/x-grid-modules.d.ts ./dist/data-grid.d.ts > ./dist/data-grid.all.d.ts`,
{
exitOnFail: true,
wait: true,
},
),
!production && sourceMaps(),
],
plugins: [dts(), !production && sourceMaps()],
},
];
2 changes: 1 addition & 1 deletion packages/grid/data-grid/src/DataGrid.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { DataGrid } from '@material-ui/data-grid';
import { ColDef, DataGrid } from '@material-ui/data-grid';

function EnterpriseTest() {
return (
Expand Down
14 changes: 12 additions & 2 deletions packages/grid/data-grid/src/DataGrid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import PropTypes from 'prop-types';
// @ts-ignore
import { createClientRender } from 'test/utils';
import { expect } from 'chai';
import { DataGrid } from '@material-ui/data-grid';
import { DataGrid, RowsProp, Columns } from '@material-ui/data-grid';

describe('<DataGrid />', () => {
const render = createClientRender();

const defaultProps = {
const defaultProps: { rows: RowsProp; columns: Columns } = {
rows: [
{
id: 0,
Expand All @@ -32,6 +32,16 @@ describe('<DataGrid />', () => {

// Adapation of describeConformance()
describe('Material-UI component API', () => {
it(`resizable true should throw error`, () => {
defaultProps.columns[0].resizable = true;
dtassone marked this conversation as resolved.
Show resolved Hide resolved
expect(() =>
render(
<div style={{ width: 300, height: 300 }}>
<DataGrid {...defaultProps} />
</div>,
),
).toThrow();
dtassone marked this conversation as resolved.
Show resolved Hide resolved
});
dtassone marked this conversation as resolved.
Show resolved Hide resolved
it(`attaches the ref`, () => {
const ref = React.createRef<HTMLDivElement>();
const { container } = render(
Expand Down
45 changes: 39 additions & 6 deletions packages/grid/data-grid/src/DataGrid.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,51 @@
import * as React from 'react';
import * as PropTypes from 'prop-types';
import { chainPropTypes } from '@material-ui/utils';
import { GridComponent, GridComponentProps, classnames } from '@material-ui/x-grid-modules';
import {
GridComponent,
GridComponentProps,
classnames,
// ColDef as XGridColDef,
dtassone marked this conversation as resolved.
Show resolved Hide resolved
} from '@material-ui/x-grid-modules';

const FORCED_PROPS: Partial<GridComponentProps> = {
pagination: true,
disableMultipleColumnsSorting: true,
disableMultipleSelection: true,
disableColumnResize: true,
};

export type DataGridProps = Omit<
GridComponentProps,
| 'disableMultipleColumnsSorting'
| 'disableMultipleSelection'
| 'disableColumnResize'
| 'licenseStatus'
| 'options'
| 'pagination'
> & {
disableMultipleColumnsSorting?: true;
disableMultipleSelection?: true;
disableColumnResize?: true;
pagination?: true;
};

const MAX_PAGE_SIZE = 100;

const DataGrid2 = React.forwardRef<HTMLDivElement, DataGridProps>(function DataGrid(props, ref) {
const { className, pageSize: pageSizeProp, ...other } = props;
const { className, pageSize: pageSizeProp, columns, ...other } = props;

const hasResizableColumns = columns.some((c) => c.resizable);
if (hasResizableColumns) {
throw new Error(
Copy link
Member

@oliviertassinari oliviertassinari Sep 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about using a custom prop type? This would allow the code to be removed in production (no bundle size implication). This would also be less disruptive to the DX. This would also give a better component error trace

[
`Material-UI: \` column.resizable = true \` is not a valid prop.`,
'Column resizing is not available in the MIT version',
'',
'You need to upgrade to the XGrid component to unlock this feature.',
].join('\n'),
);
}

let pageSize = pageSizeProp;
if (pageSize && pageSize > MAX_PAGE_SIZE) {
Expand All @@ -35,6 +55,7 @@ const DataGrid2 = React.forwardRef<HTMLDivElement, DataGridProps>(function DataG
return (
<GridComponent
ref={ref}
columns={columns}
className={classnames('MuiDataGrid-root', className)}
pageSize={pageSize}
{...other}
Expand All @@ -45,8 +66,20 @@ const DataGrid2 = React.forwardRef<HTMLDivElement, DataGridProps>(function DataG
});

DataGrid2.propTypes = {
disableMultipleColumnsSorting: chainPropTypes(PropTypes.number, (props) => {
if (props.pageSize && props.pageSize > MAX_PAGE_SIZE) {
disableColumnResize: chainPropTypes(PropTypes.bool, (props) => {
if (props.disableColumnResize === false) {
throw new Error(
[
`Material-UI: \`<DataGrid disableColumnResize={false} />\` is not a valid prop.`,
'Column resizing is not available in the MIT version',
'',
'You need to upgrade to the XGrid component to unlock this feature.',
].join('\n'),
);
}
}),
disableMultipleColumnsSorting: chainPropTypes(PropTypes.bool, (props) => {
if (props.disableMultipleColumnsSorting === false) {
throw new Error(
[
`Material-UI: \`<DataGrid disableMultipleColumnsSorting={false} />\` is not a valid prop.`,
Expand All @@ -59,8 +92,8 @@ DataGrid2.propTypes = {

return null;
}),
disableMultipleSelection: chainPropTypes(PropTypes.number, (props) => {
if (props.pageSize && props.pageSize > MAX_PAGE_SIZE) {
disableMultipleSelection: chainPropTypes(PropTypes.bool, (props) => {
if (props.disableMultipleSelection === false) {
throw new Error(
[
`Material-UI: \`<DataGrid disableMultipleSelection={false} />\` is not a valid prop.`,
Expand Down
15 changes: 10 additions & 5 deletions packages/grid/x-grid-modules/src/components/column-header-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ const headerAlignPropToCss = {
export const ColumnHeaderItem = React.memo(
({ column, colIndex, onResizeColumn }: ColumnHeaderItemProps) => {
const api = React.useContext(ApiContext);
const { headerHeight, showColumnRightBorder } = React.useContext(OptionsContext);
const { headerHeight, showColumnRightBorder, disableColumnResize } = React.useContext(
OptionsContext,
);

const cssClass = classnames(
HEADER_CELL_CSS_CLASS,
showColumnRightBorder ? 'MuiDataGrid-withBorder' : '',
column.headerClassName,
column.headerAlign && column.headerAlign !== 'left'
? headerAlignPropToCss[column.headerAlign]
: '',
column.headerAlign &&
column.headerAlign !== 'left' &&
headerAlignPropToCss[column.headerAlign],
{ 'MuiDataGrid-colCellSortable': column.sortable },
);

Expand Down Expand Up @@ -89,7 +91,10 @@ export const ColumnHeaderItem = React.memo(
hide={column.hideSortIcons}
/>
)}
<ColumnHeaderSeparator resizable={column.resizable} onResize={handleResize} />
<ColumnHeaderSeparator
resizable={!disableColumnResize && column.resizable}
onResize={handleResize}
/>
</div>
);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ export const useStyles = makeStyles(
outline: 'none',
display: 'flex',
flexDirection: 'column',
'& *::-webkit-scrollbar-track': {
borderRadius: 10,
},
'& *::-webkit-scrollbar': {
width: 8,
height: 8,
},
'& *::-webkit-scrollbar-thumb': {
borderRadius: 10,
backgroundColor: borderColor,
},
Copy link
Member

@oliviertassinari oliviertassinari Sep 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong pull request? Isn't this one for #260? Could we keep the pull requests isolated :)?

Copy link
Member

@oliviertassinari oliviertassinari Sep 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, what about we only mess around if the theme is dark? Disturbing the UX as little as necessary. Especially, I don't think that we should change the size of the scrollbar.

'& *, & *::before, & *::after': {
boxSizing: 'inherit',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export function useOptionsProp(props: GridComponentProps): [GridOptions, Functio
disableSelectionOnClick: props.disableSelectionOnClick,
disableMultipleColumnsSorting: props.disableMultipleColumnsSorting,
disableMultipleSelection: props.disableMultipleSelection,
disableColumnResize: props.disableColumnResize,
disableExtendRowFullWidth: props.disableExtendRowFullWidth,
headerHeight: props.headerHeight,
hideFooter: props.hideFooter,
Expand Down Expand Up @@ -61,6 +62,7 @@ export function useOptionsProp(props: GridComponentProps): [GridOptions, Functio
props.disableSelectionOnClick,
props.disableMultipleColumnsSorting,
props.disableMultipleSelection,
props.disableColumnResize,
props.disableExtendRowFullWidth,
props.headerHeight,
props.hideFooter,
Expand Down
7 changes: 6 additions & 1 deletion packages/grid/x-grid-modules/src/models/gridOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ export interface GridOptions {
* @default false
*/
disableMultipleColumnsSorting?: boolean;
/**
* If `true`, resizing columns is disabled.
* @default false
*/
disableColumnResize?: boolean;
/**
* If `true`, the right border of the cells are displayed.
* @default false
Expand Down Expand Up @@ -245,7 +250,7 @@ export interface GridOptions {
export const DEFAULT_GRID_OPTIONS: GridOptions = {
rowHeight: 52,
headerHeight: 56,
scrollbarSize: 15,
scrollbarSize: 10,
dtassone marked this conversation as resolved.
Show resolved Hide resolved
columnBuffer: 2,
rowsPerPageOptions: [25, 50, 100],
pageSize: 100,
Expand Down
3 changes: 3 additions & 0 deletions packages/storybook/.storybook/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ module.exports = {
'@storybook/addon-storysource/register',
'@storybook/addon-a11y/register',
],
typescript: {
check: true,
},
webpackFinal: async config => {
config.devtool = __DEV__ ? 'inline-source-map' : undefined;
config.module.rules.push({
Expand Down
26 changes: 26 additions & 0 deletions packages/storybook/src/stories/grid-columns.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,32 @@ export function HeaderComponent() {
);
}

export function ColumnsAlign() {
const data = useData(100, 5);

const transformCols = React.useCallback((cols) => {
if (cols.length > 0) {
cols.forEach((col: ColDef, idx) => {
if (idx > 1 && idx % 2 === 1) {
col.align = 'right';
col.headerAlign = 'right';
} else if (idx > 1 && idx % 2 === 0) {
col.align = 'center';
col.headerAlign = 'center';
}
col.width = 180;
});
}
return cols;
}, []);

return (
<div className="grid-container">
<XGrid rows={data.rows} columns={transformCols(data.columns)} />
</div>
);
}

const priceColumnType: ColTypeDef = {
extendType: 'number',
valueFormatter: ({ value }) => `${value} USD`,
Expand Down
Loading