@mui/x-data-grid-generator 7.7.1 → 7.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,197 @@
1
+ import _extends from "@babel/runtime/helpers/esm/extends";
2
+ import * as React from 'react';
3
+ import LRUCache from 'lru-cache';
4
+ import { getGridDefaultColumnTypes } from '@mui/x-data-grid-pro';
5
+ import { getColumnsFromOptions, extrapolateSeed, deepFreeze } from './useDemoData';
6
+ import { getRealGridData } from '../services/real-data-service';
7
+ import { addTreeDataOptionsToDemoData } from '../services/tree-data-generator';
8
+ import { loadServerRows, processTreeDataRows, DEFAULT_DATASET_OPTIONS, DEFAULT_SERVER_OPTIONS } from './serverUtils';
9
+ import { randomInt } from '../services';
10
+ const dataCache = new LRUCache({
11
+ max: 10,
12
+ ttl: 60 * 5 * 1e3 // 5 minutes
13
+ });
14
+ export const BASE_URL = 'https://mui.com/x/api/data-grid';
15
+ function decodeParams(url) {
16
+ const params = new URL(url).searchParams;
17
+ const decodedParams = {};
18
+ const array = Array.from(params.entries());
19
+ for (const [key, value] of array) {
20
+ try {
21
+ decodedParams[key] = JSON.parse(decodeURIComponent(value));
22
+ } catch (e) {
23
+ decodedParams[key] = value;
24
+ }
25
+ }
26
+ return decodedParams;
27
+ }
28
+ const getInitialState = (columns, groupingField) => {
29
+ const columnVisibilityModel = {};
30
+ columns.forEach(col => {
31
+ if (col.hide) {
32
+ columnVisibilityModel[col.field] = false;
33
+ }
34
+ });
35
+ if (groupingField) {
36
+ columnVisibilityModel[groupingField] = false;
37
+ }
38
+ return {
39
+ columns: {
40
+ columnVisibilityModel
41
+ }
42
+ };
43
+ };
44
+ const defaultColDef = getGridDefaultColumnTypes();
45
+ export const useMockServer = (dataSetOptions, serverOptions, shouldRequestsFail) => {
46
+ const [data, setData] = React.useState();
47
+ const [index, setIndex] = React.useState(0);
48
+ const shouldRequestsFailRef = React.useRef(shouldRequestsFail ?? false);
49
+ React.useEffect(() => {
50
+ if (shouldRequestsFail !== undefined) {
51
+ shouldRequestsFailRef.current = shouldRequestsFail;
52
+ }
53
+ }, [shouldRequestsFail]);
54
+ const options = _extends({}, DEFAULT_DATASET_OPTIONS, dataSetOptions);
55
+ const columns = React.useMemo(() => {
56
+ return getColumnsFromOptions({
57
+ dataSet: options.dataSet,
58
+ editable: options.editable,
59
+ maxColumns: options.maxColumns,
60
+ visibleFields: options.visibleFields
61
+ });
62
+ }, [options.dataSet, options.editable, options.maxColumns, options.visibleFields]);
63
+ const initialState = React.useMemo(() => getInitialState(columns, options.treeData?.groupingField), [columns, options.treeData?.groupingField]);
64
+ const columnsWithDefaultColDef = React.useMemo(() => columns.map(column => _extends({}, defaultColDef[column.type || 'string'], column)), [columns]);
65
+ const isTreeData = options.treeData?.groupingField != null;
66
+ const getGroupKey = React.useMemo(() => {
67
+ if (isTreeData) {
68
+ return row => row[options.treeData.groupingField];
69
+ }
70
+ return undefined;
71
+ // eslint-disable-next-line react-hooks/exhaustive-deps
72
+ }, [options.treeData?.groupingField, isTreeData]);
73
+ const getChildrenCount = React.useMemo(() => {
74
+ if (isTreeData) {
75
+ return row => row.descendantCount;
76
+ }
77
+ return undefined;
78
+ }, [isTreeData]);
79
+ React.useEffect(() => {
80
+ const cacheKey = `${options.dataSet}-${options.rowLength}-${index}-${options.maxColumns}`;
81
+
82
+ // Cache to allow fast switch between the JavaScript and TypeScript version
83
+ // of the demos.
84
+ if (dataCache.has(cacheKey)) {
85
+ const newData = dataCache.get(cacheKey);
86
+ setData(newData);
87
+ return undefined;
88
+ }
89
+ let active = true;
90
+ (async () => {
91
+ let rowData;
92
+ const rowLength = options.rowLength;
93
+ if (rowLength > 1000) {
94
+ rowData = await getRealGridData(1000, columns);
95
+ rowData = await extrapolateSeed(rowLength, rowData);
96
+ } else {
97
+ rowData = await getRealGridData(rowLength, columns);
98
+ }
99
+ if (!active) {
100
+ return;
101
+ }
102
+ if (isTreeData) {
103
+ rowData = addTreeDataOptionsToDemoData(rowData, {
104
+ maxDepth: options.treeData?.maxDepth,
105
+ groupingField: options.treeData?.groupingField,
106
+ averageChildren: options.treeData?.averageChildren
107
+ });
108
+ }
109
+ if (process.env.NODE_ENV !== 'production') {
110
+ deepFreeze(rowData);
111
+ }
112
+ dataCache.set(cacheKey, rowData);
113
+ setData(rowData);
114
+ })();
115
+ return () => {
116
+ active = false;
117
+ };
118
+ }, [columns, isTreeData, options.rowLength, options.treeData?.maxDepth, options.treeData?.groupingField, options.treeData?.averageChildren, options.dataSet, options.maxColumns, index]);
119
+ const fetchRows = React.useCallback(async requestUrl => {
120
+ if (!data || !requestUrl) {
121
+ return new Promise(resolve => {
122
+ resolve({
123
+ rows: [],
124
+ rowCount: 0
125
+ });
126
+ });
127
+ }
128
+ const params = decodeParams(requestUrl);
129
+ const verbose = serverOptions?.verbose ?? true;
130
+ // eslint-disable-next-line no-console
131
+ const print = console.info;
132
+ if (verbose) {
133
+ print('MUI X: DATASOURCE REQUEST', params);
134
+ }
135
+ let getRowsResponse;
136
+ const serverOptionsWithDefault = {
137
+ minDelay: serverOptions?.minDelay ?? DEFAULT_SERVER_OPTIONS.minDelay,
138
+ maxDelay: serverOptions?.maxDelay ?? DEFAULT_SERVER_OPTIONS.maxDelay,
139
+ useCursorPagination: serverOptions?.useCursorPagination ?? DEFAULT_SERVER_OPTIONS.useCursorPagination
140
+ };
141
+ if (shouldRequestsFailRef.current) {
142
+ const {
143
+ minDelay,
144
+ maxDelay
145
+ } = serverOptionsWithDefault;
146
+ const delay = randomInt(minDelay, maxDelay);
147
+ return new Promise((_, reject) => {
148
+ if (verbose) {
149
+ print('MUI X: DATASOURCE REQUEST FAILURE', params);
150
+ }
151
+ setTimeout(() => reject(new Error('Could not fetch the data')), delay);
152
+ });
153
+ }
154
+ if (isTreeData /* || TODO: `isRowGrouping` */) {
155
+ const {
156
+ rows,
157
+ rootRowCount
158
+ } = await processTreeDataRows(data.rows, params, serverOptionsWithDefault, columnsWithDefaultColDef);
159
+ getRowsResponse = {
160
+ rows: rows.slice().map(row => _extends({}, row, {
161
+ path: undefined
162
+ })),
163
+ rowCount: rootRowCount
164
+ };
165
+ } else {
166
+ // plain data
167
+ const {
168
+ returnedRows,
169
+ nextCursor,
170
+ totalRowCount
171
+ } = await loadServerRows(data.rows, _extends({}, params, params.paginationModel), serverOptionsWithDefault, columnsWithDefaultColDef);
172
+ getRowsResponse = {
173
+ rows: returnedRows,
174
+ rowCount: totalRowCount,
175
+ pageInfo: {
176
+ nextCursor
177
+ }
178
+ };
179
+ }
180
+ return new Promise(resolve => {
181
+ if (verbose) {
182
+ print('MUI X: DATASOURCE RESPONSE', params, getRowsResponse);
183
+ }
184
+ resolve(getRowsResponse);
185
+ });
186
+ }, [data, serverOptions?.verbose, serverOptions?.minDelay, serverOptions?.maxDelay, serverOptions?.useCursorPagination, isTreeData, columnsWithDefaultColDef]);
187
+ return {
188
+ columns: columnsWithDefaultColDef,
189
+ initialState,
190
+ getGroupKey,
191
+ getChildrenCount,
192
+ fetchRows,
193
+ loadNewData: () => {
194
+ setIndex(oldIndex => oldIndex + 1);
195
+ }
196
+ };
197
+ };
@@ -1,184 +1,154 @@
1
1
  import * as React from 'react';
2
- import { GridRowModel, GridFilterModel, GridSortModel, GridRowId, GridFilterOperator, GridColDef } from '@mui/x-data-grid-pro';
2
+ import { GridRowModel } from '@mui/x-data-grid-pro';
3
3
  import { UseDemoDataOptions } from './useDemoData';
4
- /**
5
- * Simulates server data loading
6
- */
7
- export declare const loadServerRows: (rows: GridRowModel[], queryOptions: QueryOptions, serverOptions: ServerOptions, columnsWithDefaultColDef: GridColDef[]) => Promise<FakeServerResponse>;
8
- interface FakeServerResponse {
9
- returnedRows: GridRowModel[];
10
- nextCursor?: string;
11
- hasNextPage: boolean;
12
- totalRowCount: number;
13
- }
14
- interface PageInfo {
15
- totalRowCount?: number;
16
- nextCursor?: string;
17
- hasNextPage?: boolean;
18
- pageSize?: number;
19
- }
20
- interface DefaultServerOptions {
21
- minDelay: number;
22
- maxDelay: number;
23
- useCursorPagination?: boolean;
24
- }
25
- type ServerOptions = Partial<DefaultServerOptions>;
26
- export interface QueryOptions {
27
- cursor?: GridRowId;
28
- page?: number;
29
- pageSize?: number;
30
- filterModel?: GridFilterModel;
31
- sortModel?: GridSortModel;
32
- firstRowToRender?: number;
33
- lastRowToRender?: number;
34
- }
4
+ import type { ServerOptions, QueryOptions, PageInfo } from './serverUtils';
35
5
  export declare const createFakeServer: (dataSetOptions?: Partial<UseDemoDataOptions>, serverOptions?: ServerOptions) => {
36
6
  columns: import("..").GridColDefGenerator[];
37
7
  columnsWithDefaultColDef: ({
38
8
  field: string;
39
- headerName?: string | undefined;
40
- description?: string | undefined;
41
- width?: number | undefined;
42
- flex?: number | undefined;
43
- minWidth?: number | undefined;
44
- maxWidth?: number | undefined;
45
- hideable?: boolean | undefined;
46
- sortable?: boolean | undefined;
47
- sortingOrder?: import("@mui/x-data-grid-pro").GridSortDirection[] | undefined;
48
- resizable?: boolean | undefined;
49
- editable?: boolean | undefined;
50
- groupable?: boolean | undefined;
51
- pinnable?: boolean | undefined;
9
+ headerName?: string;
10
+ description?: string;
11
+ width?: number;
12
+ flex?: number;
13
+ minWidth?: number;
14
+ maxWidth?: number;
15
+ hideable?: boolean;
16
+ sortable?: boolean;
17
+ sortingOrder?: import("@mui/x-data-grid-pro").GridSortDirection[];
18
+ resizable?: boolean;
19
+ editable?: boolean;
20
+ groupable?: boolean;
21
+ pinnable?: boolean;
52
22
  sortComparator?: import("@mui/x-data-grid-pro").GridComparatorFn<any> | undefined;
53
23
  getSortComparator?: ((sortDirection: import("@mui/x-data-grid-pro").GridSortDirection) => import("@mui/x-data-grid-pro").GridComparatorFn<any> | undefined) | undefined;
54
- type?: import("@mui/x-data-grid-pro").GridColType | undefined;
55
- align?: import("@mui/x-data-grid-pro").GridAlignment | undefined;
24
+ type?: import("@mui/x-data-grid-pro").GridColType;
25
+ align?: import("@mui/x-data-grid-pro").GridAlignment;
56
26
  valueGetter?: import("@mui/x-data-grid-pro").GridValueGetter<any, any, any, never> | undefined;
57
27
  valueSetter?: import("@mui/x-data-grid-pro").GridValueSetter<any, any, any> | undefined;
58
28
  valueFormatter?: import("@mui/x-data-grid-pro").GridValueFormatter<any, any, any, never> | undefined;
59
29
  valueParser?: import("@mui/x-data-grid-pro").GridValueParser<any, any, any> | undefined;
60
30
  cellClassName?: import("@mui/x-data-grid-pro").GridCellClassNamePropType<any, any> | undefined;
61
- display?: "text" | "flex" | undefined;
31
+ display?: "text" | "flex";
62
32
  renderCell?: ((params: import("@mui/x-data-grid-pro").GridRenderCellParams<any, any, any, import("@mui/x-data-grid-pro").GridTreeNodeWithRender>) => React.ReactNode) | undefined;
63
33
  renderEditCell?: ((params: import("@mui/x-data-grid-pro").GridRenderEditCellParams<any, any, any, import("@mui/x-data-grid-pro").GridTreeNodeWithRender>) => React.ReactNode) | undefined;
64
- preProcessEditCellProps?: ((params: import("@mui/x-data-grid-pro").GridPreProcessEditCellProps<any, any>) => import("@mui/x-data-grid-pro").GridEditCellProps<any> | Promise<import("@mui/x-data-grid-pro").GridEditCellProps<any>>) | undefined;
65
- headerClassName?: import("@mui/x-data-grid-pro").GridColumnHeaderClassNamePropType | undefined;
34
+ preProcessEditCellProps?: ((params: import("@mui/x-data-grid-pro").GridPreProcessEditCellProps) => import("@mui/x-data-grid-pro").GridEditCellProps | Promise<import("@mui/x-data-grid-pro").GridEditCellProps>) | undefined;
35
+ headerClassName?: import("@mui/x-data-grid-pro").GridColumnHeaderClassNamePropType;
66
36
  renderHeader?: ((params: import("@mui/x-data-grid-pro").GridColumnHeaderParams<any, any, any>) => React.ReactNode) | undefined;
67
- headerAlign?: import("@mui/x-data-grid-pro").GridAlignment | undefined;
68
- hideSortIcons?: boolean | undefined;
69
- disableColumnMenu?: boolean | undefined;
70
- filterable?: boolean | undefined;
71
- filterOperators?: GridFilterOperator<any, any, any>[] | undefined;
37
+ headerAlign?: import("@mui/x-data-grid-pro").GridAlignment;
38
+ hideSortIcons?: boolean;
39
+ disableColumnMenu?: boolean;
40
+ filterable?: boolean;
41
+ filterOperators?: import("@mui/x-data-grid-pro").GridFilterOperator<any, any, any>[] | undefined;
72
42
  getApplyQuickFilterFn?: import("@mui/x-data-grid-pro").GetApplyQuickFilterFn<any, any> | undefined;
73
- disableReorder?: boolean | undefined;
74
- disableExport?: boolean | undefined;
43
+ disableReorder?: boolean;
44
+ disableExport?: boolean;
75
45
  colSpan?: number | import("@mui/x-data-grid-pro").GridColSpanFn<any, any, any> | undefined;
76
- renderHeaderFilter?: ((params: import("@mui/x-data-grid-pro").GridRenderHeaderFilterProps) => React.ReactNode) | undefined;
77
- aggregable?: boolean | undefined;
78
- availableAggregationFunctions?: string[] | undefined;
46
+ renderHeaderFilter?: (params: import("@mui/x-data-grid-pro").GridRenderHeaderFilterProps) => React.ReactNode;
47
+ aggregable?: boolean;
48
+ availableAggregationFunctions?: string[];
79
49
  groupingValueGetter?: import("@mui/x-data-grid-premium").GridGroupingValueGetter<any> | undefined;
80
50
  pastedValueParser?: import("@mui/x-data-grid-premium").GridPastedValueParser<any, any, any> | undefined;
81
- generateData?: ((row: any, context: import("..").GridDataGeneratorContext) => any) | undefined;
82
- dataGeneratorUniquenessEnabled?: boolean | undefined;
83
- hide?: boolean | undefined;
51
+ generateData?: (row: any, context: import("..").GridDataGeneratorContext) => any;
52
+ dataGeneratorUniquenessEnabled?: boolean;
53
+ hide?: boolean;
84
54
  } | {
85
55
  type: "actions";
86
- getActions: (params: import("@mui/x-data-grid-pro").GridRowParams<any>) => React.ReactElement<import("@mui/x-data-grid-pro").GridActionsCellItemProps, string | React.JSXElementConstructor<any>>[];
56
+ getActions: (params: import("@mui/x-data-grid-pro").GridRowParams<any>) => React.ReactElement<import("@mui/x-data-grid-pro").GridActionsCellItemProps>[];
87
57
  field: string;
88
- headerName?: string | undefined;
89
- description?: string | undefined;
90
- width?: number | undefined;
91
- flex?: number | undefined;
92
- minWidth?: number | undefined;
93
- maxWidth?: number | undefined;
94
- hideable?: boolean | undefined;
95
- sortable?: boolean | undefined;
96
- sortingOrder?: import("@mui/x-data-grid-pro").GridSortDirection[] | undefined;
97
- resizable?: boolean | undefined;
98
- editable?: boolean | undefined;
99
- groupable?: boolean | undefined;
100
- pinnable?: boolean | undefined;
58
+ headerName?: string;
59
+ description?: string;
60
+ width?: number;
61
+ flex?: number;
62
+ minWidth?: number;
63
+ maxWidth?: number;
64
+ hideable?: boolean;
65
+ sortable?: boolean;
66
+ sortingOrder?: import("@mui/x-data-grid-pro").GridSortDirection[];
67
+ resizable?: boolean;
68
+ editable?: boolean;
69
+ groupable?: boolean;
70
+ pinnable?: boolean;
101
71
  sortComparator?: import("@mui/x-data-grid-pro").GridComparatorFn<any> | undefined;
102
72
  getSortComparator?: ((sortDirection: import("@mui/x-data-grid-pro").GridSortDirection) => import("@mui/x-data-grid-pro").GridComparatorFn<any> | undefined) | undefined;
103
- align?: import("@mui/x-data-grid-pro").GridAlignment | undefined;
73
+ align?: import("@mui/x-data-grid-pro").GridAlignment;
104
74
  valueGetter?: import("@mui/x-data-grid-pro").GridValueGetter<any, any, any, never> | undefined;
105
75
  valueSetter?: import("@mui/x-data-grid-pro").GridValueSetter<any, any, any> | undefined;
106
76
  valueFormatter?: import("@mui/x-data-grid-pro").GridValueFormatter<any, any, any, never> | undefined;
107
77
  valueParser?: import("@mui/x-data-grid-pro").GridValueParser<any, any, any> | undefined;
108
78
  cellClassName?: import("@mui/x-data-grid-pro").GridCellClassNamePropType<any, any> | undefined;
109
- display?: "text" | "flex" | undefined;
79
+ display?: "text" | "flex";
110
80
  renderCell?: ((params: import("@mui/x-data-grid-pro").GridRenderCellParams<any, any, any, import("@mui/x-data-grid-pro").GridTreeNodeWithRender>) => React.ReactNode) | undefined;
111
81
  renderEditCell?: ((params: import("@mui/x-data-grid-pro").GridRenderEditCellParams<any, any, any, import("@mui/x-data-grid-pro").GridTreeNodeWithRender>) => React.ReactNode) | undefined;
112
- preProcessEditCellProps?: ((params: import("@mui/x-data-grid-pro").GridPreProcessEditCellProps<any, any>) => import("@mui/x-data-grid-pro").GridEditCellProps<any> | Promise<import("@mui/x-data-grid-pro").GridEditCellProps<any>>) | undefined;
113
- headerClassName?: import("@mui/x-data-grid-pro").GridColumnHeaderClassNamePropType | undefined;
82
+ preProcessEditCellProps?: ((params: import("@mui/x-data-grid-pro").GridPreProcessEditCellProps) => import("@mui/x-data-grid-pro").GridEditCellProps | Promise<import("@mui/x-data-grid-pro").GridEditCellProps>) | undefined;
83
+ headerClassName?: import("@mui/x-data-grid-pro").GridColumnHeaderClassNamePropType;
114
84
  renderHeader?: ((params: import("@mui/x-data-grid-pro").GridColumnHeaderParams<any, any, any>) => React.ReactNode) | undefined;
115
- headerAlign?: import("@mui/x-data-grid-pro").GridAlignment | undefined;
116
- hideSortIcons?: boolean | undefined;
117
- disableColumnMenu?: boolean | undefined;
118
- filterable?: boolean | undefined;
119
- filterOperators?: GridFilterOperator<any, any, any>[] | undefined;
85
+ headerAlign?: import("@mui/x-data-grid-pro").GridAlignment;
86
+ hideSortIcons?: boolean;
87
+ disableColumnMenu?: boolean;
88
+ filterable?: boolean;
89
+ filterOperators?: import("@mui/x-data-grid-pro").GridFilterOperator<any, any, any>[] | undefined;
120
90
  getApplyQuickFilterFn?: import("@mui/x-data-grid-pro").GetApplyQuickFilterFn<any, any> | undefined;
121
- disableReorder?: boolean | undefined;
122
- disableExport?: boolean | undefined;
91
+ disableReorder?: boolean;
92
+ disableExport?: boolean;
123
93
  colSpan?: number | import("@mui/x-data-grid-pro").GridColSpanFn<any, any, any> | undefined;
124
- renderHeaderFilter?: ((params: import("@mui/x-data-grid-pro").GridRenderHeaderFilterProps) => React.ReactNode) | undefined;
125
- aggregable?: boolean | undefined;
126
- availableAggregationFunctions?: string[] | undefined;
94
+ renderHeaderFilter?: (params: import("@mui/x-data-grid-pro").GridRenderHeaderFilterProps) => React.ReactNode;
95
+ aggregable?: boolean;
96
+ availableAggregationFunctions?: string[];
127
97
  groupingValueGetter?: import("@mui/x-data-grid-premium").GridGroupingValueGetter<any> | undefined;
128
98
  pastedValueParser?: import("@mui/x-data-grid-premium").GridPastedValueParser<any, any, any> | undefined;
129
- generateData?: ((row: any, context: import("..").GridDataGeneratorContext) => any) | undefined;
130
- dataGeneratorUniquenessEnabled?: boolean | undefined;
131
- hide?: boolean | undefined;
99
+ generateData?: (row: any, context: import("..").GridDataGeneratorContext) => any;
100
+ dataGeneratorUniquenessEnabled?: boolean;
101
+ hide?: boolean;
132
102
  } | {
133
103
  type: "singleSelect";
134
- valueOptions?: import("@mui/x-data-grid-pro").ValueOptions[] | ((params: import("@mui/x-data-grid-pro").GridValueOptionsParams<any>) => import("@mui/x-data-grid-pro").ValueOptions[]) | undefined;
135
- getOptionLabel?: ((value: import("@mui/x-data-grid-pro").ValueOptions) => string) | undefined;
136
- getOptionValue?: ((value: import("@mui/x-data-grid-pro").ValueOptions) => any) | undefined;
104
+ valueOptions?: import("@mui/x-data-grid-pro").ValueOptions[] | ((params: import("@mui/x-data-grid-pro").GridValueOptionsParams<any>) => Array<import("@mui/x-data-grid-pro").ValueOptions>) | undefined;
105
+ getOptionLabel?: (value: import("@mui/x-data-grid-pro").ValueOptions) => string;
106
+ getOptionValue?: (value: import("@mui/x-data-grid-pro").ValueOptions) => any;
137
107
  field: string;
138
- headerName?: string | undefined;
139
- description?: string | undefined;
140
- width?: number | undefined;
141
- flex?: number | undefined;
142
- minWidth?: number | undefined;
143
- maxWidth?: number | undefined;
144
- hideable?: boolean | undefined;
145
- sortable?: boolean | undefined;
146
- sortingOrder?: import("@mui/x-data-grid-pro").GridSortDirection[] | undefined;
147
- resizable?: boolean | undefined;
148
- editable?: boolean | undefined;
149
- groupable?: boolean | undefined;
150
- pinnable?: boolean | undefined;
108
+ headerName?: string;
109
+ description?: string;
110
+ width?: number;
111
+ flex?: number;
112
+ minWidth?: number;
113
+ maxWidth?: number;
114
+ hideable?: boolean;
115
+ sortable?: boolean;
116
+ sortingOrder?: import("@mui/x-data-grid-pro").GridSortDirection[];
117
+ resizable?: boolean;
118
+ editable?: boolean;
119
+ groupable?: boolean;
120
+ pinnable?: boolean;
151
121
  sortComparator?: import("@mui/x-data-grid-pro").GridComparatorFn<any> | undefined;
152
122
  getSortComparator?: ((sortDirection: import("@mui/x-data-grid-pro").GridSortDirection) => import("@mui/x-data-grid-pro").GridComparatorFn<any> | undefined) | undefined;
153
- align?: import("@mui/x-data-grid-pro").GridAlignment | undefined;
123
+ align?: import("@mui/x-data-grid-pro").GridAlignment;
154
124
  valueGetter?: import("@mui/x-data-grid-pro").GridValueGetter<any, any, any, never> | undefined;
155
125
  valueSetter?: import("@mui/x-data-grid-pro").GridValueSetter<any, any, any> | undefined;
156
126
  valueFormatter?: import("@mui/x-data-grid-pro").GridValueFormatter<any, any, any, never> | undefined;
157
127
  valueParser?: import("@mui/x-data-grid-pro").GridValueParser<any, any, any> | undefined;
158
128
  cellClassName?: import("@mui/x-data-grid-pro").GridCellClassNamePropType<any, any> | undefined;
159
- display?: "text" | "flex" | undefined;
129
+ display?: "text" | "flex";
160
130
  renderCell?: ((params: import("@mui/x-data-grid-pro").GridRenderCellParams<any, any, any, import("@mui/x-data-grid-pro").GridTreeNodeWithRender>) => React.ReactNode) | undefined;
161
131
  renderEditCell?: ((params: import("@mui/x-data-grid-pro").GridRenderEditCellParams<any, any, any, import("@mui/x-data-grid-pro").GridTreeNodeWithRender>) => React.ReactNode) | undefined;
162
- preProcessEditCellProps?: ((params: import("@mui/x-data-grid-pro").GridPreProcessEditCellProps<any, any>) => import("@mui/x-data-grid-pro").GridEditCellProps<any> | Promise<import("@mui/x-data-grid-pro").GridEditCellProps<any>>) | undefined;
163
- headerClassName?: import("@mui/x-data-grid-pro").GridColumnHeaderClassNamePropType | undefined;
132
+ preProcessEditCellProps?: ((params: import("@mui/x-data-grid-pro").GridPreProcessEditCellProps) => import("@mui/x-data-grid-pro").GridEditCellProps | Promise<import("@mui/x-data-grid-pro").GridEditCellProps>) | undefined;
133
+ headerClassName?: import("@mui/x-data-grid-pro").GridColumnHeaderClassNamePropType;
164
134
  renderHeader?: ((params: import("@mui/x-data-grid-pro").GridColumnHeaderParams<any, any, any>) => React.ReactNode) | undefined;
165
- headerAlign?: import("@mui/x-data-grid-pro").GridAlignment | undefined;
166
- hideSortIcons?: boolean | undefined;
167
- disableColumnMenu?: boolean | undefined;
168
- filterable?: boolean | undefined;
169
- filterOperators?: GridFilterOperator<any, any, any>[] | undefined;
135
+ headerAlign?: import("@mui/x-data-grid-pro").GridAlignment;
136
+ hideSortIcons?: boolean;
137
+ disableColumnMenu?: boolean;
138
+ filterable?: boolean;
139
+ filterOperators?: import("@mui/x-data-grid-pro").GridFilterOperator<any, any, any>[] | undefined;
170
140
  getApplyQuickFilterFn?: import("@mui/x-data-grid-pro").GetApplyQuickFilterFn<any, any> | undefined;
171
- disableReorder?: boolean | undefined;
172
- disableExport?: boolean | undefined;
141
+ disableReorder?: boolean;
142
+ disableExport?: boolean;
173
143
  colSpan?: number | import("@mui/x-data-grid-pro").GridColSpanFn<any, any, any> | undefined;
174
- renderHeaderFilter?: ((params: import("@mui/x-data-grid-pro").GridRenderHeaderFilterProps) => React.ReactNode) | undefined;
175
- aggregable?: boolean | undefined;
176
- availableAggregationFunctions?: string[] | undefined;
144
+ renderHeaderFilter?: (params: import("@mui/x-data-grid-pro").GridRenderHeaderFilterProps) => React.ReactNode;
145
+ aggregable?: boolean;
146
+ availableAggregationFunctions?: string[];
177
147
  groupingValueGetter?: import("@mui/x-data-grid-premium").GridGroupingValueGetter<any> | undefined;
178
148
  pastedValueParser?: import("@mui/x-data-grid-premium").GridPastedValueParser<any, any, any> | undefined;
179
- generateData?: ((row: any, context: import("..").GridDataGeneratorContext) => any) | undefined;
180
- dataGeneratorUniquenessEnabled?: boolean | undefined;
181
- hide?: boolean | undefined;
149
+ generateData?: (row: any, context: import("..").GridDataGeneratorContext) => any;
150
+ dataGeneratorUniquenessEnabled?: boolean;
151
+ hide?: boolean;
182
152
  })[];
183
153
  initialState: {
184
154
  columns: {
@@ -191,4 +161,3 @@ export declare const createFakeServer: (dataSetOptions?: Partial<UseDemoDataOpti
191
161
  isLoading: boolean;
192
162
  };
193
163
  };
194
- export {};
package/hooks/useQuery.js CHANGED
@@ -1,145 +1,9 @@
1
1
  import _extends from "@babel/runtime/helpers/esm/extends";
2
2
  import * as React from 'react';
3
- import { getGridDefaultColumnTypes, GridLogicOperator } from '@mui/x-data-grid-pro';
3
+ import { getGridDefaultColumnTypes } from '@mui/x-data-grid-pro';
4
4
  import { isDeepEqual } from '@mui/x-data-grid/internals';
5
5
  import { useDemoData, getColumnsFromOptions, getInitialState } from './useDemoData';
6
- import { randomInt } from '../services/random-generator';
7
- const apiRef = {};
8
- const simplifiedValueGetter = (field, colDef) => row => {
9
- return colDef.valueGetter?.(row[row.id], row, colDef, apiRef) || row[field];
10
- };
11
- const getRowComparator = (sortModel, columnsWithDefaultColDef) => {
12
- if (!sortModel) {
13
- const comparator = () => 0;
14
- return comparator;
15
- }
16
- const sortOperators = sortModel.map(sortItem => {
17
- const columnField = sortItem.field;
18
- const colDef = columnsWithDefaultColDef.find(({
19
- field
20
- }) => field === columnField);
21
- return _extends({}, sortItem, {
22
- valueGetter: simplifiedValueGetter(columnField, colDef),
23
- sortComparator: colDef.sortComparator
24
- });
25
- });
26
- const comparator = (row1, row2) => sortOperators.reduce((acc, {
27
- valueGetter,
28
- sort,
29
- sortComparator
30
- }) => {
31
- if (acc !== 0) {
32
- return acc;
33
- }
34
- const v1 = valueGetter(row1);
35
- const v2 = valueGetter(row2);
36
- return sort === 'desc' ? -1 * sortComparator(v1, v2) : sortComparator(v1, v2);
37
- }, 0);
38
- return comparator;
39
- };
40
- const getFilteredRows = (rows, filterModel, columnsWithDefaultColDef) => {
41
- if (filterModel === undefined || filterModel.items.length === 0) {
42
- return rows;
43
- }
44
- const valueGetters = filterModel.items.map(({
45
- field
46
- }) => simplifiedValueGetter(field, columnsWithDefaultColDef.find(column => column.field === field)));
47
- const filterFunctions = filterModel.items.map(filterItem => {
48
- const {
49
- field,
50
- operator
51
- } = filterItem;
52
- const colDef = columnsWithDefaultColDef.find(column => column.field === field);
53
- const filterOperator = colDef.filterOperators.find(({
54
- value
55
- }) => operator === value);
56
- let parsedValue = filterItem.value;
57
- if (colDef.valueParser) {
58
- const parser = colDef.valueParser;
59
- parsedValue = Array.isArray(filterItem.value) ? filterItem.value?.map(x => parser(x)) : parser(filterItem.value);
60
- }
61
- return filterOperator?.getApplyFilterFn({
62
- filterItem,
63
- value: parsedValue
64
- }, colDef);
65
- });
66
- if (filterModel.logicOperator === GridLogicOperator.Or) {
67
- return rows.filter(row => filterModel.items.some((_, index) => {
68
- const value = valueGetters[index](row);
69
- return filterFunctions[index] === null ? true : filterFunctions[index]({
70
- value
71
- });
72
- }));
73
- }
74
- return rows.filter(row => filterModel.items.every((_, index) => {
75
- const value = valueGetters[index](row);
76
- return filterFunctions[index] === null ? true : filterFunctions[index](value);
77
- }));
78
- };
79
-
80
- /**
81
- * Simulates server data loading
82
- */
83
- export const loadServerRows = (rows, queryOptions, serverOptions, columnsWithDefaultColDef) => {
84
- const {
85
- minDelay = 100,
86
- maxDelay = 300,
87
- useCursorPagination
88
- } = serverOptions;
89
- if (maxDelay < minDelay) {
90
- throw new Error('serverOptions.minDelay is larger than serverOptions.maxDelay ');
91
- }
92
- const delay = randomInt(minDelay, maxDelay);
93
- const {
94
- cursor,
95
- page = 0,
96
- pageSize
97
- } = queryOptions;
98
- let nextCursor;
99
- let firstRowIndex;
100
- let lastRowIndex;
101
- let filteredRows = getFilteredRows(rows, queryOptions.filterModel, columnsWithDefaultColDef);
102
- const rowComparator = getRowComparator(queryOptions.sortModel, columnsWithDefaultColDef);
103
- filteredRows = [...filteredRows].sort(rowComparator);
104
- const totalRowCount = filteredRows.length;
105
- if (!pageSize) {
106
- firstRowIndex = 0;
107
- lastRowIndex = filteredRows.length;
108
- } else if (useCursorPagination) {
109
- firstRowIndex = cursor ? filteredRows.findIndex(({
110
- id
111
- }) => id === cursor) : 0;
112
- firstRowIndex = Math.max(firstRowIndex, 0); // if cursor not found return 0
113
- lastRowIndex = firstRowIndex + pageSize;
114
- nextCursor = lastRowIndex >= filteredRows.length ? undefined : filteredRows[lastRowIndex].id;
115
- } else {
116
- firstRowIndex = page * pageSize;
117
- lastRowIndex = (page + 1) * pageSize;
118
- }
119
- const hasNextPage = lastRowIndex < filteredRows.length - 1;
120
- const response = {
121
- returnedRows: filteredRows.slice(firstRowIndex, lastRowIndex),
122
- nextCursor,
123
- hasNextPage,
124
- totalRowCount
125
- };
126
- return new Promise(resolve => {
127
- setTimeout(() => {
128
- resolve(response);
129
- }, delay); // simulate network latency
130
- });
131
- };
132
- const DEFAULT_DATASET_OPTIONS = {
133
- dataSet: 'Commodity',
134
- rowLength: 100,
135
- maxColumns: 6
136
- };
137
- const disableDelay = typeof DISABLE_CHANCE_RANDOM !== 'undefined' && DISABLE_CHANCE_RANDOM;
138
- const DEFAULT_SERVER_OPTIONS = {
139
- minDelay: disableDelay ? 0 : 100,
140
- maxDelay: disableDelay ? 0 : 300,
141
- useCursorPagination: true
142
- };
6
+ import { DEFAULT_DATASET_OPTIONS, DEFAULT_SERVER_OPTIONS, loadServerRows } from './serverUtils';
143
7
  export const createFakeServer = (dataSetOptions, serverOptions) => {
144
8
  const dataSetOptionsWithDefault = _extends({}, DEFAULT_DATASET_OPTIONS, dataSetOptions);
145
9
  const serverOptionsWithDefault = _extends({}, DEFAULT_SERVER_OPTIONS, serverOptions);
package/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @mui/x-data-grid-generator v7.7.1
2
+ * @mui/x-data-grid-generator v7.9.0
3
3
  *
4
4
  * @license MUI X Commercial
5
5
  * This source code is licensed under the commercial license found in the
@@ -1,4 +1,6 @@
1
1
  export * from './useDemoData';
2
2
  export * from './useBasicDemoData';
3
3
  export * from './useMovieData';
4
- export * from './useQuery';
4
+ export * from './useQuery';
5
+ export * from './useMockServer';
6
+ export { loadServerRows } from './serverUtils';