drf-react-by-schema 0.1.0 → 0.2.1

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,118 @@
1
+ import {
2
+ GridApi,
3
+ getGridNumericOperators,
4
+ getGridDateOperators,
5
+ GridFilterOperator
6
+ } from "@mui/x-data-grid";
7
+ import { GridEnrichedBySchemaColDef } from "../../utils";
8
+ import {
9
+ InputNumberInterval,
10
+ InputDateInterval,
11
+ InputFloatInterval
12
+ } from "./InputInterval";
13
+
14
+ /**
15
+ * Get the largest width in a column, looking at all rows
16
+ * @param colIndex the column index
17
+ * @returns width of the biggest row inside a column
18
+ */
19
+ function maxOfCol (colIndex: number): number {
20
+ const invisibleContainer = document.createElement('div');
21
+
22
+ invisibleContainer.style.visibility = 'hidden';
23
+ invisibleContainer.style.zIndex = '-9999999999';
24
+ invisibleContainer.style.position = 'absolute';
25
+ invisibleContainer.style.fontSize = '14px';
26
+ invisibleContainer.style.top = '0';
27
+ invisibleContainer.style.left = '0';
28
+ document.body.append(invisibleContainer);
29
+ const widths: any[] = [];
30
+ document.querySelectorAll(
31
+ `[aria-colindex='${colIndex}']`
32
+ ).forEach(cell => {
33
+ const invisibleCell = document.createElement('div');
34
+ invisibleCell.innerHTML = cell.innerHTML;
35
+ invisibleCell.style.width = 'max-content';
36
+ invisibleCell.style.maxWidth = 'none';
37
+ invisibleCell.style.minWidth = 'none';
38
+ invisibleContainer.append(invisibleCell);
39
+ widths.push(Math.ceil(invisibleCell.clientWidth));
40
+ });
41
+ let max = Math.max(...widths);
42
+ if (max !== 0 && max < 50) {
43
+ max = 50;
44
+ }
45
+ invisibleContainer.remove();
46
+ return max;
47
+ }
48
+
49
+ export type ResizeType = 'condense' | 'maxContent' | 'fitScreen';
50
+
51
+ /**
52
+ *
53
+ * @param columns
54
+ * @param resizeType
55
+ * @param apiRef
56
+ * @returns columns resized to the chosen resizeType
57
+ */
58
+ export function resizeColumns (
59
+ columns: GridEnrichedBySchemaColDef[],
60
+ resizeType: ResizeType,
61
+ apiRef: React.MutableRefObject<GridApi>
62
+ ): GridEnrichedBySchemaColDef[] {
63
+ const cols = [...columns];
64
+ cols.forEach((col, index: number) => {
65
+ if (resizeType === 'fitScreen') {
66
+ delete col.width;
67
+ col.minWidth = 80;
68
+ if (col.isIndexField) {
69
+ col.flex = 1;
70
+ }
71
+ } else if (resizeType === 'maxContent') {
72
+ const maxColWidth = maxOfCol(index);
73
+ delete col.flex;
74
+ delete col.minWidth;
75
+ col.width = maxColWidth + 22;
76
+ } else {
77
+ col.width = 0;
78
+ }
79
+ });
80
+ return cols;
81
+ }
82
+
83
+ export const quantityOnlyOperators = ({ type }:{ type: string }) => {
84
+ const builtInFilters = (type === 'date')
85
+ ? getGridDateOperators()
86
+ : getGridNumericOperators();
87
+ let InputComponent = InputNumberInterval;
88
+ if (type === 'date') {
89
+ InputComponent = InputDateInterval;
90
+ }
91
+ if (type === 'float') {
92
+ InputComponent = InputFloatInterval;
93
+ }
94
+ return [
95
+ ...builtInFilters,
96
+ {
97
+ label: 'entre',
98
+ value: 'entre',
99
+ getApplyFilterFn: (filterItem: any) => {
100
+ if (!Array.isArray(filterItem.value) || filterItem.value.length !== 2) {
101
+ return null;
102
+ }
103
+ if (filterItem.value[0] === null || filterItem.value[1] === null) {
104
+ return null;
105
+ }
106
+
107
+ return ({ value }: { value: any }) => {
108
+ return (
109
+ value !== null &&
110
+ filterItem.value[0] <= value &&
111
+ value <= filterItem.value[1]
112
+ );
113
+ };
114
+ },
115
+ InputComponent
116
+ }
117
+ ] as GridFilterOperator[];
118
+ };
@@ -0,0 +1,50 @@
1
+ DataGridBySchemaEditable example:
2
+
3
+ ```js
4
+ import DRFReactBySchemaProvider from '../context/DRFReactBySchemaProvider';
5
+ const data = {
6
+ data: [],
7
+ columns: [],
8
+ schema: {}
9
+ };
10
+ const model = 'example';
11
+ const indexField = null;
12
+ const indexFieldBasePath = null;
13
+ const isAutoHeight = true;
14
+ const hideFooterPagination = false;
15
+ const finalCustomColumnOperations = null;
16
+ const customLinkDestination = null;
17
+ const setVisibleRows = null;
18
+ const setData = (newData) => {
19
+ return '';
20
+ };
21
+ const Link = null;
22
+ <DRFReactBySchemaProvider
23
+ serverEndPoint={{
24
+ url: 'https://icv.eita.org.br/api',
25
+ apiTokenUrl: 'https://icv.eita.org.br/api-auth/token'
26
+ }}
27
+ >
28
+ <DataGridBySchemaEditable
29
+ data={data.data}
30
+ columns={data.columns}
31
+ schema={data.schema}
32
+ model={model}
33
+ indexField={indexField}
34
+ indexFieldBasePath={indexFieldBasePath}
35
+ isEditable={false}
36
+ isAutoHeight={isAutoHeight}
37
+ hideFooterPagination={hideFooterPagination}
38
+ customColumnOperations={finalCustomColumnOperations}
39
+ customLinkDestination={customLinkDestination}
40
+ setVisibleRows={setVisibleRows}
41
+ onDataChange={newData => {
42
+ setData({
43
+ ...data,
44
+ data: newData
45
+ });
46
+ }}
47
+ LinkComponent={Link}
48
+ />
49
+ </DRFReactBySchemaProvider>
50
+ ```