@equinor/eds-data-grid-react 0.3.0-rc → 0.3.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.
@@ -386,6 +386,32 @@ const StyledTableRow = styled__default.default(edsCoreReact.Table.Row).withConfi
386
386
  componentId: "sc-1vjfq5p-0"
387
387
  })(["background-color:inherit;"]);
388
388
 
389
+ /**
390
+ * Function returning wether a string only contains number. Allows leading or trailing spaces.
391
+ *
392
+ * Examples:
393
+ *
394
+ * ```
395
+ * isNumberOnlyString("10") // true
396
+ * isNumberOnlyString("10.10") // true
397
+ * isNumberOnlyString("10px") // false
398
+ * isNumberOnlyString("10%") // false
399
+ * isNumberOnlyString("10 ") // true
400
+ * ```
401
+ *
402
+ * @param number
403
+ * @returns
404
+ */
405
+ function isNumberOnlyString(number) {
406
+ return !isNaN(Number(number)) && !isNaN(parseFloat(number));
407
+ }
408
+ function addPxSuffixIfInputHasNoPrefix(size) {
409
+ if (typeof size === 'number' || isNumberOnlyString(size)) {
410
+ return `${size}px`;
411
+ }
412
+ return size;
413
+ }
414
+
389
415
  /* eslint-disable @typescript-eslint/no-non-null-assertion */
390
416
  function EdsDataGrid({
391
417
  rows,
@@ -423,12 +449,15 @@ function EdsDataGrid({
423
449
  minWidth,
424
450
  height,
425
451
  getRowId,
426
- rowVirtualizerInstanceRef
452
+ rowVirtualizerInstanceRef,
453
+ columnSizing,
454
+ onColumnResize
427
455
  }) {
428
456
  const [sorting, setSorting] = react.useState(sortingState ?? []);
429
457
  const [selection, setSelection] = react.useState(selectedRows ?? {});
430
458
  const [columnPin, setColumnPin] = react.useState(columnPinState ?? {});
431
459
  const [columnFilters, setColumnFilters] = react.useState([]);
460
+ const [internalColumnSize, setInternalColumnSize] = react.useState(columnSizing ?? {});
432
461
  const [visible, setVisible] = react.useState(columnVisibility ?? {});
433
462
  const [globalFilter, setGlobalFilter] = react.useState('');
434
463
  const [columnOrderState, setColumnOrderState] = react.useState([]);
@@ -492,6 +521,7 @@ function EdsDataGrid({
492
521
  data: rows,
493
522
  columns: _columns,
494
523
  defaultColumn: {
524
+ size: 150,
495
525
  cell: context => {
496
526
  return /*#__PURE__*/jsxRuntime.jsx(edsCoreReact.Typography, {
497
527
  style: {
@@ -506,11 +536,22 @@ function EdsDataGrid({
506
536
  }
507
537
  },
508
538
  columnResizeMode: columnResizeMode,
539
+ onColumnSizingChange: change => {
540
+ if (typeof change === 'function') {
541
+ setInternalColumnSize(change(internalColumnSize));
542
+ } else {
543
+ setInternalColumnSize(change);
544
+ }
545
+ if (onColumnResize) {
546
+ onColumnResize(internalColumnSize);
547
+ }
548
+ },
509
549
  state: {
510
550
  sorting,
511
551
  columnPinning: columnPin,
512
552
  rowSelection: selection,
513
- columnOrder: columnOrderState
553
+ columnOrder: columnOrderState,
554
+ columnSizing: columnSizing ?? internalColumnSize
514
555
  },
515
556
  onSortingChange: changes => {
516
557
  if (onSortingChange) {
@@ -596,14 +637,14 @@ function EdsDataGrid({
596
637
  }));
597
638
  }, [pageSize]);
598
639
  const table = reactTable.useReactTable(options);
599
- let parentRefStyle = {};
640
+ let tableWrapperStyle = {};
600
641
 
601
642
  /**
602
643
  * Style the parent container to enable virtualization.
603
644
  * By not setting this, the virtual-scroll will always render every row, reducing computational overhead if turned off.
604
645
  */
605
646
  if (enableVirtual) {
606
- parentRefStyle = {
647
+ tableWrapperStyle = {
607
648
  height: height ?? virtualHeight ?? 500,
608
649
  overflow: 'auto',
609
650
  position: 'relative'
@@ -647,16 +688,13 @@ function EdsDataGrid({
647
688
  enableSorting: !!enableSorting,
648
689
  enableColumnFiltering: !!enableColumnFiltering,
649
690
  stickyHeader: !!stickyHeader,
650
- children: [/*#__PURE__*/jsxRuntime.jsxs("div", {
691
+ children: [/*#__PURE__*/jsxRuntime.jsxs(TableWrapper, {
651
692
  className: "table-wrapper",
652
- style: {
653
- height: height ?? 'auto',
654
- ...parentRefStyle,
655
- width: scrollbarHorizontal ? width ?? '100%' : 'auto',
656
- overflow: 'auto',
657
- contain: 'layout paint style'
658
- },
693
+ style: tableWrapperStyle,
659
694
  ref: parentRef,
695
+ $height: height,
696
+ $width: width,
697
+ $scrollbarHorizontal: scrollbarHorizontal,
660
698
  children: [/*#__PURE__*/jsxRuntime.jsxs(edsCoreReact.Table, {
661
699
  className: Object.entries(classList).filter(([, k]) => k).map(([k]) => k).join(' '),
662
700
  style: {
@@ -733,5 +771,17 @@ function EdsDataGrid({
733
771
  })]
734
772
  });
735
773
  }
774
+ const TableWrapper = styled__default.default.div.withConfig({
775
+ displayName: "EdsDataGrid__TableWrapper",
776
+ componentId: "sc-82fj3f-0"
777
+ })(["height:", ";width:", ";overflow:auto;contain:", ";"], ({
778
+ $height
779
+ }) => addPxSuffixIfInputHasNoPrefix($height) ?? 'auto', ({
780
+ $scrollbarHorizontal,
781
+ $width
782
+ }) => $scrollbarHorizontal ? addPxSuffixIfInputHasNoPrefix($width) ?? '100%' : 'auto', ({
783
+ $height,
784
+ $width
785
+ }) => Boolean($height) && Boolean($width) ? 'strict' : 'unset');
736
786
 
737
787
  exports.EdsDataGrid = EdsDataGrid;
@@ -5,6 +5,8 @@ import { useState, useEffect, useMemo, useRef, useCallback } from 'react';
5
5
  import { TableHeaderRow } from './components/TableHeaderRow.js';
6
6
  import { TableRow } from './components/TableRow.js';
7
7
  import { TableProvider } from './EdsDataGridContext.js';
8
+ import styled from 'styled-components';
9
+ import { addPxSuffixIfInputHasNoPrefix } from './utils.js';
8
10
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
9
11
 
10
12
  /* eslint-disable @typescript-eslint/no-non-null-assertion */
@@ -44,12 +46,15 @@ function EdsDataGrid({
44
46
  minWidth,
45
47
  height,
46
48
  getRowId,
47
- rowVirtualizerInstanceRef
49
+ rowVirtualizerInstanceRef,
50
+ columnSizing,
51
+ onColumnResize
48
52
  }) {
49
53
  const [sorting, setSorting] = useState(sortingState ?? []);
50
54
  const [selection, setSelection] = useState(selectedRows ?? {});
51
55
  const [columnPin, setColumnPin] = useState(columnPinState ?? {});
52
56
  const [columnFilters, setColumnFilters] = useState([]);
57
+ const [internalColumnSize, setInternalColumnSize] = useState(columnSizing ?? {});
53
58
  const [visible, setVisible] = useState(columnVisibility ?? {});
54
59
  const [globalFilter, setGlobalFilter] = useState('');
55
60
  const [columnOrderState, setColumnOrderState] = useState([]);
@@ -113,6 +118,7 @@ function EdsDataGrid({
113
118
  data: rows,
114
119
  columns: _columns,
115
120
  defaultColumn: {
121
+ size: 150,
116
122
  cell: context => {
117
123
  return /*#__PURE__*/jsx(Typography, {
118
124
  style: {
@@ -127,11 +133,22 @@ function EdsDataGrid({
127
133
  }
128
134
  },
129
135
  columnResizeMode: columnResizeMode,
136
+ onColumnSizingChange: change => {
137
+ if (typeof change === 'function') {
138
+ setInternalColumnSize(change(internalColumnSize));
139
+ } else {
140
+ setInternalColumnSize(change);
141
+ }
142
+ if (onColumnResize) {
143
+ onColumnResize(internalColumnSize);
144
+ }
145
+ },
130
146
  state: {
131
147
  sorting,
132
148
  columnPinning: columnPin,
133
149
  rowSelection: selection,
134
- columnOrder: columnOrderState
150
+ columnOrder: columnOrderState,
151
+ columnSizing: columnSizing ?? internalColumnSize
135
152
  },
136
153
  onSortingChange: changes => {
137
154
  if (onSortingChange) {
@@ -217,14 +234,14 @@ function EdsDataGrid({
217
234
  }));
218
235
  }, [pageSize]);
219
236
  const table = useReactTable(options);
220
- let parentRefStyle = {};
237
+ let tableWrapperStyle = {};
221
238
 
222
239
  /**
223
240
  * Style the parent container to enable virtualization.
224
241
  * By not setting this, the virtual-scroll will always render every row, reducing computational overhead if turned off.
225
242
  */
226
243
  if (enableVirtual) {
227
- parentRefStyle = {
244
+ tableWrapperStyle = {
228
245
  height: height ?? virtualHeight ?? 500,
229
246
  overflow: 'auto',
230
247
  position: 'relative'
@@ -268,16 +285,13 @@ function EdsDataGrid({
268
285
  enableSorting: !!enableSorting,
269
286
  enableColumnFiltering: !!enableColumnFiltering,
270
287
  stickyHeader: !!stickyHeader,
271
- children: [/*#__PURE__*/jsxs("div", {
288
+ children: [/*#__PURE__*/jsxs(TableWrapper, {
272
289
  className: "table-wrapper",
273
- style: {
274
- height: height ?? 'auto',
275
- ...parentRefStyle,
276
- width: scrollbarHorizontal ? width ?? '100%' : 'auto',
277
- overflow: 'auto',
278
- contain: 'layout paint style'
279
- },
290
+ style: tableWrapperStyle,
280
291
  ref: parentRef,
292
+ $height: height,
293
+ $width: width,
294
+ $scrollbarHorizontal: scrollbarHorizontal,
281
295
  children: [/*#__PURE__*/jsxs(Table, {
282
296
  className: Object.entries(classList).filter(([, k]) => k).map(([k]) => k).join(' '),
283
297
  style: {
@@ -354,5 +368,17 @@ function EdsDataGrid({
354
368
  })]
355
369
  });
356
370
  }
371
+ const TableWrapper = styled.div.withConfig({
372
+ displayName: "EdsDataGrid__TableWrapper",
373
+ componentId: "sc-82fj3f-0"
374
+ })(["height:", ";width:", ";overflow:auto;contain:", ";"], ({
375
+ $height
376
+ }) => addPxSuffixIfInputHasNoPrefix($height) ?? 'auto', ({
377
+ $scrollbarHorizontal,
378
+ $width
379
+ }) => $scrollbarHorizontal ? addPxSuffixIfInputHasNoPrefix($width) ?? '100%' : 'auto', ({
380
+ $height,
381
+ $width
382
+ }) => Boolean($height) && Boolean($width) ? 'strict' : 'unset');
357
383
 
358
384
  export { EdsDataGrid };
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Function returning wether a string only contains number. Allows leading or trailing spaces.
3
+ *
4
+ * Examples:
5
+ *
6
+ * ```
7
+ * isNumberOnlyString("10") // true
8
+ * isNumberOnlyString("10.10") // true
9
+ * isNumberOnlyString("10px") // false
10
+ * isNumberOnlyString("10%") // false
11
+ * isNumberOnlyString("10 ") // true
12
+ * ```
13
+ *
14
+ * @param number
15
+ * @returns
16
+ */
17
+ function isNumberOnlyString(number) {
18
+ return !isNaN(Number(number)) && !isNaN(parseFloat(number));
19
+ }
20
+ function addPxSuffixIfInputHasNoPrefix(size) {
21
+ if (typeof size === 'number' || isNumberOnlyString(size)) {
22
+ return `${size}px`;
23
+ }
24
+ return size;
25
+ }
26
+
27
+ export { addPxSuffixIfInputHasNoPrefix, isNumberOnlyString };
@@ -1,2 +1,2 @@
1
1
  import { EdsDataGridProps } from './EdsDataGridProps';
2
- export declare function EdsDataGrid<T>({ rows, columns, columnResizeMode, pageSize, rowSelection, selectedRows, enableColumnFiltering, debug, enablePagination, enableSorting, stickyHeader, onSelectRow, caption, enableVirtual, virtualHeight, columnVisibility, columnVisibilityChange, emptyMessage, columnOrder, cellClass, cellStyle, rowClass, rowStyle, headerClass, headerStyle, externalPaginator, onSortingChange, manualSorting, sortingState, columnPinState, scrollbarHorizontal, width, minWidth, height, getRowId, rowVirtualizerInstanceRef, }: EdsDataGridProps<T>): import("react/jsx-runtime").JSX.Element;
2
+ export declare function EdsDataGrid<T>({ rows, columns, columnResizeMode, pageSize, rowSelection, selectedRows, enableColumnFiltering, debug, enablePagination, enableSorting, stickyHeader, onSelectRow, caption, enableVirtual, virtualHeight, columnVisibility, columnVisibilityChange, emptyMessage, columnOrder, cellClass, cellStyle, rowClass, rowStyle, headerClass, headerStyle, externalPaginator, onSortingChange, manualSorting, sortingState, columnPinState, scrollbarHorizontal, width, minWidth, height, getRowId, rowVirtualizerInstanceRef, columnSizing, onColumnResize, }: EdsDataGridProps<T>): import("react/jsx-runtime").JSX.Element;
@@ -1,4 +1,4 @@
1
- import { Column, ColumnDef, ColumnPinningState, ColumnResizeMode, OnChangeFn, Row, RowSelectionState, SortingState, TableOptions } from '@tanstack/react-table';
1
+ import { Column, ColumnDef, ColumnPinningState, ColumnResizeMode, ColumnSizingState, OnChangeFn, Row, RowSelectionState, SortingState, TableOptions } from '@tanstack/react-table';
2
2
  import { Virtualizer } from '@tanstack/react-virtual';
3
3
  import { CSSProperties, MutableRefObject, ReactElement } from 'react';
4
4
  type BaseProps<T> = {
@@ -181,6 +181,8 @@ type SortProps = {
181
181
  };
182
182
  type ColumnProps = {
183
183
  columnPinState?: ColumnPinningState;
184
+ columnSizing?: ColumnSizingState;
185
+ onColumnResize?: (e: ColumnSizingState) => void;
184
186
  };
185
187
  type RefProps = {
186
188
  rowVirtualizerInstanceRef?: MutableRefObject<Virtualizer<HTMLDivElement, Element> | null>;
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Function returning wether a string only contains number. Allows leading or trailing spaces.
3
+ *
4
+ * Examples:
5
+ *
6
+ * ```
7
+ * isNumberOnlyString("10") // true
8
+ * isNumberOnlyString("10.10") // true
9
+ * isNumberOnlyString("10px") // false
10
+ * isNumberOnlyString("10%") // false
11
+ * isNumberOnlyString("10 ") // true
12
+ * ```
13
+ *
14
+ * @param number
15
+ * @returns
16
+ */
17
+ export declare function isNumberOnlyString(number: string): boolean;
18
+ export declare function addPxSuffixIfInputHasNoPrefix(size: number | string): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@equinor/eds-data-grid-react",
3
- "version": "0.3.0-rc",
3
+ "version": "0.3.0",
4
4
  "description": "A feature-rich data-grid written in React, implementing the Equinor Design System",
5
5
  "license": "MIT",
6
6
  "types": "dist/types/index.d.ts",
@@ -14,17 +14,17 @@
14
14
  "email": "fg_eds@equinor.com"
15
15
  },
16
16
  "peerDependencies": {
17
+ "@equinor/eds-core-react": ">=0.36.0",
17
18
  "react": ">=16.8",
18
19
  "react-dom": ">=16.8",
19
- "styled-components": ">=4.2",
20
- "@equinor/eds-core-react": "^0.35.1"
20
+ "styled-components": ">=4.2"
21
21
  },
22
22
  "dependencies": {
23
23
  "@tanstack/react-table": "^8.10.7",
24
- "@tanstack/react-virtual": "^3.0.1",
24
+ "@tanstack/react-virtual": "^3.0.4",
25
25
  "@equinor/eds-icons": "^0.21.0",
26
- "@equinor/eds-tokens": "0.9.2",
27
- "@equinor/eds-utils": "^0.8.3"
26
+ "@equinor/eds-utils": "^0.8.4",
27
+ "@equinor/eds-tokens": "0.9.2"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@mdx-js/react": "2.3.0",