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 18 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,9 +12,9 @@ const rows = [
export default function ColumnWidthGrid() {
return (
<div style={{ height: 250, width: '100%' }}>
<DataGrid
<XGrid
columns={[
{ field: 'id', resizable: false },
{ field: 'id' },
{ field: 'username' },
{ field: 'age', resizable: false },
]}
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,9 +12,9 @@ const rows = [
export default function ColumnWidthGrid() {
return (
<div style={{ height: 250, width: '100%' }}>
<DataGrid
<XGrid
columns={[
{ field: 'id', resizable: false },
{ field: 'id' },
{ field: 'username' },
{ field: 'age', resizable: false },
]}
Expand Down
6 changes: 4 additions & 2 deletions docs/src/pages/components/data-grid/columns/columns.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ 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 resizing ⚡️

By default, `XGrid` allows all columns to be resized by dragging the right portion of the column separator.

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`.
Alternatively, to disable all columns resize, set the prop `disableColumnResize={true}`.

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ The data grid comes in 2 versions:
- `DataGrid` **MIT licensed** as part of the community edition. It's an extension of `@material-ui/core`.
- `XGrid` **Commercially licensed** as part of the X product line offering.

The features only available in the commercial version are suffixed with a <span style="font-size: 28px">⚡️</span> icon.
The features only available in the commercial version are suffixed with a <span style="font-size: 26px">⚡️</span> icon.

<img src="/static/x/header-icon.png" width="454" height="239" alt="">

Expand All @@ -128,7 +128,7 @@ The following table summarizes the features available in the community `DataGrid
| | Community | Enterprise ⚡️ |
| --------------------------------------------------------------------------------------- | --------- | -------------- |
| **Column** | | |
| [Column resizing](/components/data-grid/columns/#column-sizing) | ❌ | ✅ |
| [Column resizing](/components/data-grid/columns/#column-resizing) | ❌ | ✅ |
| [Column groups](/components/data-grid/columns/#column-groups) | 🚧 | 🚧 |
| [Column reorder](/components/data-grid/columns/#column-reorder) | ❌ | 🚧 |
| [Column sticky](/components/data-grid/columns/#column-sticky) | ❌ | 🚧 |
Expand Down
5 changes: 3 additions & 2 deletions docs/src/pages/components/data-grid/overview/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ The data grid comes in 2 versions:

- `DataGrid` **MIT licensed** as part of the community edition. It's an extension of `@material-ui/core`.
- `XGrid` **Commercially licensed** as part of the X product line offering.
The features only available in the commercial version are suffixed with a ⚡️ icon.

The features only available in the commercial version are suffixed with a <span style="font-size: 26px">⚡️</span> icon.

### MIT version

Expand Down Expand Up @@ -49,7 +50,7 @@ import { XGrid } from '@material-ui/x-grid';
- [Sorting](/components/data-grid/rows/#row-sorting) and [multi-sort](/components/data-grid/rows/#multi-column-sorting) ⚡️
- [Selection](/components/data-grid/selection/)
- Rows and column virtualization ⚡️
- [Resizable columns](/components/data-grid/columns/)
- [Resizable columns](/components/data-grid/columns/#column-resizing) ⚡️
- 100% customizable
- Server side data
- Column hiding
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: 0 additions & 2 deletions packages/grid/data-grid/src/DataGrid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { DataGrid } from '@material-ui/data-grid';

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

const defaultProps = {
rows: [
{
Expand All @@ -21,7 +20,6 @@ describe('<DataGrid />', () => {
{ field: 'brand', width: 100 },
],
};

describe('layout', () => {
before(function beforeHook() {
if (/jsdom/.test(window.navigator.userAgent)) {
Expand Down
38 changes: 33 additions & 5 deletions packages/grid/data-grid/src/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,28 @@ 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;

let pageSize = pageSizeProp;
if (pageSize && pageSize > MAX_PAGE_SIZE) {
Expand All @@ -35,6 +38,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 +49,32 @@ const DataGrid2 = React.forwardRef<HTMLDivElement, DataGridProps>(function DataG
});

DataGrid2.propTypes = {
disableMultipleColumnsSorting: chainPropTypes(PropTypes.number, (props) => {
if (props.pageSize && props.pageSize > MAX_PAGE_SIZE) {
columns: chainPropTypes(PropTypes.any, (props) => {
if (props.columns && props.columns.some((c) => c.resizable)) {
throw new Error(
[
`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'),
);
}
}),
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 +87,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 @@ -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
5 changes: 5 additions & 0 deletions 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
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: __DEV__, // Netlify is breaking the deploy with this settings on. So deactivate on release
},
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