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

Add defaultColumnOptions #2117

Merged
merged 7 commits into from
Aug 14, 2020
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
- `rows`
- ⚠️ This replace the `rowGetter` and `rowsCount` props
- `rowClass`
- `defaultColumnOptions`
- ⚠️ This replaces the `minColumnWidth` and `defaultFormatter` props
- `column.cellClass(row)` function support:
- `column = { ..., cellClass(row) { return string; } }`
- `column.minWidth`
Expand Down Expand Up @@ -68,6 +70,7 @@
- ⚠️ `column.getCellActions`
- Check [#1845](https://github.com/adazzle/react-data-grid/pull/1845) on how to migrate
- ⚠️ `column.getRowMetaData`
- ⚠️ `column.minColumnWidth`
- ⚠️ `column.filterable`
- ⚠️ `column.draggable`
- ⚠️ `cellRangeSelection.{onStart,onUpdate,onEnd}`
Expand Down
2 changes: 2 additions & 0 deletions src/Cell.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const defaultColumn: CalculatedColumn<Row> = {
name: 'Desciption',
width: 100,
left: 0,
resizable: false,
sortable: false,
formatter: SimpleCellFormatter
};

Expand Down
1 change: 1 addition & 0 deletions src/Columns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const SelectColumn: Column<any, any> = {
name: '',
width: 35,
maxWidth: 35,
resizable: false,
frozen: true,
headerRenderer(props) {
return (
Expand Down
21 changes: 11 additions & 10 deletions src/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import HeaderRow from './HeaderRow';
import FilterRow from './FilterRow';
import Row from './Row';
import SummaryRow from './SummaryRow';
import { ValueFormatter } from './formatters';
import { legacyCellInput } from './editors';
import {
assertIsValidKey,
Expand All @@ -34,7 +33,6 @@ import {
CheckCellIsEditableEvent,
Column,
Filters,
FormatterProps,
Position,
RowRendererProps,
RowsUpdateEvent,
Expand All @@ -53,6 +51,13 @@ interface EditCellState extends Position {
key: string | null;
}

type DefaultColumnOptions<R, SR> = Pick<Column<R, SR>,
| 'formatter'
| 'minWidth'
| 'resizable'
| 'sortable'
>;

export interface DataGridHandle {
scrollToColumn: (colIdx: number) => void;
scrollToRow: (rowIdx: number) => void;
Expand Down Expand Up @@ -97,8 +102,6 @@ export interface DataGridProps<R, K extends keyof R, SR = unknown> extends Share
width?: number;
/** The height of the grid in pixels */
height?: number;
/** Minimum column width in pixels */
minColumnWidth?: number;
/** The height of each row in pixels */
rowHeight?: number;
/** The height of the header row in pixels */
Expand All @@ -121,11 +124,11 @@ export interface DataGridProps<R, K extends keyof R, SR = unknown> extends Share
onSort?: (columnKey: string, direction: SortDirection) => void;
filters?: Filters;
onFiltersChange?: (filters: Filters) => void;
defaultColumnOptions?: DefaultColumnOptions<R, SR>;

/**
* Custom renderers
*/
defaultFormatter?: React.ComponentType<FormatterProps<R, SR>>;
rowRenderer?: React.ComponentType<RowRendererProps<R, SR>>;
emptyRowsRenderer?: React.ComponentType;

Expand Down Expand Up @@ -177,7 +180,6 @@ function DataGrid<R, K extends keyof R, SR>({
// Dimensions props
width,
height = 350,
minColumnWidth = 80,
rowHeight = 35,
headerRowHeight = rowHeight,
headerFiltersHeight = 45,
Expand All @@ -189,8 +191,8 @@ function DataGrid<R, K extends keyof R, SR>({
onSort,
filters,
onFiltersChange,
defaultColumnOptions,
// Custom renderers
defaultFormatter = ValueFormatter,
rowRenderer: RowRenderer = Row,
emptyRowsRenderer,
// Event props
Expand Down Expand Up @@ -248,11 +250,10 @@ function DataGrid<R, K extends keyof R, SR>({

const { columns, viewportColumns, totalColumnWidth, lastFrozenColumnIndex } = useViewportColumns({
columns: rawColumns,
minColumnWidth,
columnWidths,
defaultFormatter,
scrollLeft,
viewportWidth
viewportWidth,
defaultColumnOptions
});

const totalHeaderHeight = headerRowHeight + (enableFilters ? headerFiltersHeight : 0);
Expand Down
2 changes: 2 additions & 0 deletions src/HeaderCell.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ describe('HeaderCell', () => {
name: 'bla',
width: 150,
left: 300,
resizable: false,
sortable: false,
formatter: ValueFormatter,
...columnProps
},
Expand Down
2 changes: 2 additions & 0 deletions src/editors/EditorContainer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ const fakeColumn: CalculatedColumn<Row> = {
key: 'col1',
width: 100,
left: 0,
resizable: false,
sortable: false,
formatter: ValueFormatter
};

Expand Down
2 changes: 2 additions & 0 deletions src/editors/SimpleTextEditor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ describe('SimpleTextEditor', () => {
name: 'name',
width: 0,
left: 0,
resizable: false,
sortable: false,
formatter: ValueFormatter
};
const fakeBlurCb = jest.fn();
Expand Down
2 changes: 2 additions & 0 deletions src/headerCells/SortableHeaderCell.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ describe('SortableHeaderCell', () => {
key: 'col1',
width: 100,
left: 0,
resizable: false,
sortable: false,
formatter: ValueFormatter,
...overrideColumn
},
Expand Down
19 changes: 12 additions & 7 deletions src/hooks/useViewportColumns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import { useMemo } from 'react';
import { CalculatedColumn } from '../types';
import { getColumnMetrics, getHorizontalRangeToRender, getViewportColumns } from '../utils';
import { DataGridProps } from '../DataGrid';
import { ValueFormatter } from '../formatters';

type SharedDataGridProps<R, K extends keyof R, SR> =
Pick<DataGridProps<R, K, SR>, 'columns'> &
Required<Required<Pick<DataGridProps<R, K, SR>, | 'minColumnWidth' | 'defaultFormatter'>>>;
type SharedDataGridProps<R, K extends keyof R, SR> = Pick<DataGridProps<R, K, SR>, 'columns' | 'defaultColumnOptions'>;

interface ViewportColumnsArgs<R, K extends keyof R, SR> extends SharedDataGridProps<R, K, SR> {
viewportWidth: number;
Expand All @@ -16,21 +15,27 @@ interface ViewportColumnsArgs<R, K extends keyof R, SR> extends SharedDataGridPr

export function useViewportColumns<R, K extends keyof R, SR>({
columns: rawColumns,
minColumnWidth,
columnWidths,
viewportWidth,
defaultFormatter,
scrollLeft
scrollLeft,
defaultColumnOptions
}: ViewportColumnsArgs<R, K, SR>) {
const minColumnWidth = defaultColumnOptions?.minWidth ?? 80;
const defaultFormatter = defaultColumnOptions?.formatter ?? ValueFormatter;
const defaultSortable = defaultColumnOptions?.sortable ?? false;
const defaultResizable = defaultColumnOptions?.resizable ?? false;

const { columns, lastFrozenColumnIndex, totalColumnWidth } = useMemo(() => {
return getColumnMetrics<R, SR>({
columns: rawColumns,
minColumnWidth,
viewportWidth,
columnWidths,
defaultSortable,
defaultResizable,
defaultFormatter
});
}, [columnWidths, rawColumns, defaultFormatter, minColumnWidth, viewportWidth]);
}, [columnWidths, defaultFormatter, defaultResizable, defaultSortable, minColumnWidth, rawColumns, viewportWidth]);

const [colOverscanStartIdx, colOverscanEndIdx] = useMemo((): [number, number] => {
return getHorizontalRangeToRender(
Expand Down
6 changes: 6 additions & 0 deletions src/test/GridPropHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,26 @@ const columns: CalculatedColumn<Row>[] = [{
name: 'ID',
width: 100,
left: 0,
resizable: false,
sortable: false,
formatter: SimpleCellFormatter
}, {
idx: 1,
key: 'title',
name: 'Title',
width: 100,
left: 100,
resizable: false,
sortable: false,
formatter: ValueFormatter
}, {
idx: 2,
key: 'count',
name: 'Count',
width: 100,
left: 200,
resizable: false,
sortable: false,
formatter: ValueFormatter
}];

Expand Down
2 changes: 2 additions & 0 deletions src/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ function createColumn(index: number): CalculatedColumn<{ [key: string]: React.Re
idx: index,
width: 100,
left: 100 * index,
resizable: false,
sortable: false,
formatter: ValueFormatter
};
}
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export interface CalculatedColumn<TRow, TSummaryRow = unknown> extends Column<TR
idx: number;
width: number;
left: number;
resizable: boolean;
sortable: boolean;
formatter: React.ComponentType<FormatterProps<TRow, TSummaryRow>>;
}

Expand Down
8 changes: 8 additions & 0 deletions src/utils/columnUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ describe('getColumnMetrics', () => {
viewportWidth,
minColumnWidth: 50,
columnWidths: new Map(),
defaultResizable: false,
defaultSortable: false,
defaultFormatter: ValueFormatter
});

Expand All @@ -48,6 +50,8 @@ describe('getColumnMetrics', () => {
viewportWidth,
minColumnWidth: 50,
columnWidths: new Map(),
defaultResizable: false,
defaultSortable: false,
defaultFormatter: ValueFormatter
});

Expand All @@ -67,6 +71,8 @@ describe('getColumnMetrics', () => {
viewportWidth,
minColumnWidth: 50,
columnWidths: new Map(),
defaultResizable: false,
defaultSortable: false,
defaultFormatter: ValueFormatter
});
expect(metrics.columns[0]).toMatchObject(firstFrozenColumn);
Expand Down Expand Up @@ -132,6 +138,8 @@ describe('canEdit', () => {
name: 'ID',
left: 460,
width: 150,
resizable: false,
sortable: false,
formatter: ValueFormatter
};

Expand Down
4 changes: 4 additions & 0 deletions src/utils/columnUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ interface Metrics<R, SR> {
columnWidths: ReadonlyMap<string, number>;
minColumnWidth: number;
viewportWidth: number;
defaultResizable: boolean;
defaultSortable: boolean;
defaultFormatter: React.ComponentType<FormatterProps<R, SR>>;
}

Expand Down Expand Up @@ -57,6 +59,8 @@ export function getColumnMetrics<R, SR>(metrics: Metrics<R, SR>): ColumnMetrics<
idx,
width,
left,
sortable: column.resizable ?? metrics.defaultSortable,
resizable: column.resizable ?? metrics.defaultResizable,
formatter: column.formatter ?? metrics.defaultFormatter
};
totalWidth += width;
Expand Down
2 changes: 2 additions & 0 deletions src/utils/viewportUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ describe('getHorizontalRangeToRender', () => {
name: `col${i}`,
width: 100,
left: i * 100,
resizable: false,
sortable: false,
formatter: ValueFormatter
}));
}
Expand Down
Loading