@tanstack/react-table 8.0.0-beta.3 → 8.0.0-beta.4

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.
@@ -66,6 +66,7 @@ function useTableInstance(table, options) {
66
66
  onStateChange: () => {},
67
67
  // noop
68
68
  render,
69
+ renderFallbackValue: null,
69
70
  ...options
70
71
  }; // Create a new table instance and store it in state
71
72
 
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../../../../src/index.tsx"],"sourcesContent":["import * as React from 'react'\nexport * from '@tanstack/table-core'\n\nimport {\n createTableInstance,\n TableOptions,\n TableInstance,\n Table,\n TableGenerics,\n createTableFactory,\n Overwrite,\n PartialKeys,\n TableOptionsResolved,\n} from '@tanstack/table-core'\n\nexport type Renderable<TProps> =\n | React.ReactNode\n | React.FunctionComponent<TProps>\n | React.Component<TProps>\n\nexport type Render = <TProps extends {}>(\n Comp: Renderable<TProps>,\n props: TProps\n) => React.ReactNode | JSX.Element\n\nexport type ReactTableGenerics = Overwrite<\n TableGenerics,\n { Renderer: Render; Rendered: ReturnType<Render> }\n>\n\n//\n\nexport const render: Render = (Comp, props) =>\n !Comp ? null : isReactComponent(Comp) ? <Comp {...props} /> : Comp\n\nfunction isReactComponent(component: unknown): component is React.FC {\n return (\n isClassComponent(component) ||\n typeof component === 'function' ||\n isExoticComponent(component)\n )\n}\n\nfunction isClassComponent(component: any) {\n return (\n typeof component === 'function' &&\n (() => {\n const proto = Object.getPrototypeOf(component)\n return proto.prototype && proto.prototype.isReactComponent\n })()\n )\n}\n\nfunction isExoticComponent(component: any) {\n return (\n typeof component === 'object' &&\n typeof component.$$typeof === 'symbol' &&\n ['react.memo', 'react.forward_ref'].includes(component.$$typeof.description)\n )\n}\n\nexport const createTable = createTableFactory({ render })\n\n// const useIsomorphicLayoutEffect =\n// typeof document !== 'undefined' ? React.useLayoutEffect : React.useEffect\n\nexport type UseTableInstanceOptions<TGenerics extends ReactTableGenerics> =\n TableOptions<TGenerics>\n\nexport function useTableInstance<TGenerics extends ReactTableGenerics>(\n table: Table<TGenerics>,\n options: UseTableInstanceOptions<TGenerics>\n): TableInstance<TGenerics> {\n // Compose in the generic options to the user options\n const resolvedOptions: TableOptionsResolved<TGenerics> = {\n ...table.options,\n state: {}, // Dummy state\n onStateChange: () => {}, // noop\n render,\n ...options,\n }\n\n // Create a new table instance and store it in state\n const [instanceRef] = React.useState(() => ({\n current: createTableInstance<TGenerics>(resolvedOptions),\n }))\n\n // By default, manage table state here using the instance's initial state\n const [state, setState] = React.useState(\n () => instanceRef.current.initialState\n )\n\n // Compose the default state above with any user state. This will allow the user\n // to only control a subset of the state if desired.\n instanceRef.current.setOptions(prev => ({\n ...prev,\n ...options,\n state: {\n ...state,\n ...options.state,\n },\n // Similarly, we'll maintain both our internal state and any user-provided\n // state.\n onStateChange: updater => {\n setState(updater)\n options.onStateChange?.(updater)\n },\n }))\n\n return instanceRef.current\n}\n"],"names":["render","Comp","props","isReactComponent","React","component","isClassComponent","isExoticComponent","proto","Object","getPrototypeOf","prototype","$$typeof","includes","description","createTable","createTableFactory","useTableInstance","table","options","resolvedOptions","state","onStateChange","instanceRef","useState","current","createTableInstance","setState","initialState","setOptions","prev","updater"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8BA;AAEO,MAAMA,MAAc,GAAG,CAACC,IAAD,EAAOC,KAAP,KAC5B,CAACD,IAAD,GAAQ,IAAR,GAAeE,gBAAgB,CAACF,IAAD,CAAhB,gBAAyBG,gBAAA,CAAA,aAAA,CAAC,IAAD,EAAUF,KAAV,CAAzB,GAA+CD,KADzD;;AAGP,SAASE,gBAAT,CAA0BE,SAA1B,EAAqE;AACnE,EAAA,OACEC,gBAAgB,CAACD,SAAD,CAAhB,IACA,OAAOA,SAAP,KAAqB,UADrB,IAEAE,iBAAiB,CAACF,SAAD,CAHnB,CAAA;AAKD,CAAA;;AAED,SAASC,gBAAT,CAA0BD,SAA1B,EAA0C;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,CAAoBI,UAApB,CAA+BC,IAAI,KAAK,EACtC,GAAGA,IADmC;AAEtC,IAAA,GAAGX,OAFmC;AAGtCE,IAAAA,KAAK,EAAE,EACL,GAAGA,KADE;AAEL,MAAA,GAAGF,OAAO,CAACE,KAAAA;AAFN,KAH+B;AAOtC;AACA;AACAC,IAAAA,aAAa,EAAES,OAAO,IAAI;AACxBJ,MAAAA,QAAQ,CAACI,OAAD,CAAR,CAAA;AACAZ,MAAAA,OAAO,CAACG,aAAR,IAAA,IAAA,GAAA,KAAA,CAAA,GAAAH,OAAO,CAACG,aAAR,CAAwBS,OAAxB,CAAA,CAAA;AACD,KAAA;AAZqC,GAAL,CAAnC,CAAA,CAAA;AAeA,EAAOR,OAAAA,WAAW,CAACE,OAAnB,CAAA;AACD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.js","sources":["../../../../src/index.tsx"],"sourcesContent":["import * as React from 'react'\nexport * from '@tanstack/table-core'\n\nimport {\n createTableInstance,\n TableOptions,\n TableInstance,\n Table,\n TableGenerics,\n createTableFactory,\n Overwrite,\n PartialKeys,\n TableOptionsResolved,\n} from '@tanstack/table-core'\n\nexport type Renderable<TProps> =\n | React.ReactNode\n | React.FunctionComponent<TProps>\n | React.Component<TProps>\n\nexport type Render = <TProps extends {}>(\n Comp: Renderable<TProps>,\n props: TProps\n) => React.ReactNode | JSX.Element\n\nexport type ReactTableGenerics = Overwrite<\n TableGenerics,\n { Renderer: Render; Rendered: ReturnType<Render> }\n>\n\n//\n\nexport const render: Render = (Comp, props) =>\n !Comp ? null : isReactComponent(Comp) ? <Comp {...props} /> : Comp\n\nfunction isReactComponent(component: unknown): component is React.FC {\n return (\n isClassComponent(component) ||\n typeof component === 'function' ||\n isExoticComponent(component)\n )\n}\n\nfunction isClassComponent(component: any) {\n return (\n typeof component === 'function' &&\n (() => {\n const proto = Object.getPrototypeOf(component)\n return proto.prototype && proto.prototype.isReactComponent\n })()\n )\n}\n\nfunction isExoticComponent(component: any) {\n return (\n typeof component === 'object' &&\n typeof component.$$typeof === 'symbol' &&\n ['react.memo', 'react.forward_ref'].includes(component.$$typeof.description)\n )\n}\n\nexport const createTable = createTableFactory({ render })\n\n// const useIsomorphicLayoutEffect =\n// typeof document !== 'undefined' ? React.useLayoutEffect : React.useEffect\n\nexport type UseTableInstanceOptions<TGenerics extends ReactTableGenerics> =\n TableOptions<TGenerics>\n\nexport function useTableInstance<TGenerics extends ReactTableGenerics>(\n table: Table<TGenerics>,\n options: UseTableInstanceOptions<TGenerics>\n): TableInstance<TGenerics> {\n // Compose in the generic options to the user options\n const resolvedOptions: TableOptionsResolved<TGenerics> = {\n ...table.options,\n state: {}, // Dummy state\n onStateChange: () => {}, // noop\n render,\n renderFallbackValue: null,\n ...options,\n }\n\n // Create a new table instance and store it in state\n const [instanceRef] = React.useState(() => ({\n current: createTableInstance<TGenerics>(resolvedOptions),\n }))\n\n // By default, manage table state here using the instance's initial state\n const [state, setState] = React.useState(\n () => instanceRef.current.initialState\n )\n\n // Compose the default state above with any user state. This will allow the user\n // to only control a subset of the state if desired.\n instanceRef.current.setOptions(prev => ({\n ...prev,\n ...options,\n state: {\n ...state,\n ...options.state,\n },\n // Similarly, we'll maintain both our internal state and any user-provided\n // state.\n onStateChange: updater => {\n setState(updater)\n options.onStateChange?.(updater)\n },\n }))\n\n return instanceRef.current\n}\n"],"names":["render","Comp","props","isReactComponent","React","component","isClassComponent","isExoticComponent","proto","Object","getPrototypeOf","prototype","$$typeof","includes","description","createTable","createTableFactory","useTableInstance","table","options","resolvedOptions","state","onStateChange","renderFallbackValue","instanceRef","useState","current","createTableInstance","setState","initialState","setOptions","prev","updater"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8BA;AAEO,MAAMA,MAAc,GAAG,CAACC,IAAD,EAAOC,KAAP,KAC5B,CAACD,IAAD,GAAQ,IAAR,GAAeE,gBAAgB,CAACF,IAAD,CA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AACK,WAAD,CAAA,GAAgBpB,gBAAK,CAACqB,QAAN,CAAe,OAAO;AAC1CC,IAAAA,OAAO,EAAEC,yBAAmB,CAAYP,eAAZ,CAAA;AADc,GAAP,CAAf,CAAtB,CAZ0B;;AAiB1B,EAAA,MAAM,CAACC,KAAD,EAAQO,QAAR,CAAA,GAAoBxB,gBAAK,CAACqB,QAAN,CACxB,MAAMD,WAAW,CAACE,OAAZ,CAAoBG,YADF,CAA1B,CAjB0B;AAsB1B;;AACAL,EAAAA,WAAW,CAACE,OAAZ,CAAoBI,UAApB,CAA+BC,IAAI,KAAK,EACtC,GAAGA,IADmC;AAEtC,IAAA,GAAGZ,OAFmC;AAGtCE,IAAAA,KAAK,EAAE,EACL,GAAGA,KADE;AAEL,MAAA,GAAGF,OAAO,CAACE,KAAAA;AAFN,KAH+B;AAOtC;AACA;AACAC,IAAAA,aAAa,EAAEU,OAAO,IAAI;AACxBJ,MAAAA,QAAQ,CAACI,OAAD,CAAR,CAAA;AACAb,MAAAA,OAAO,CAACG,aAAR,IAAA,IAAA,GAAA,KAAA,CAAA,GAAAH,OAAO,CAACG,aAAR,CAAwBU,OAAxB,CAAA,CAAA;AACD,KAAA;AAZqC,GAAL,CAAnC,CAAA,CAAA;AAeA,EAAOR,OAAAA,WAAW,CAACE,OAAnB,CAAA;AACD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -1535,6 +1535,12 @@ const Grouping = {
1535
1535
  };
1536
1536
  },
1537
1537
  createCell: (cell, column, row, instance) => {
1538
+ const getRenderValue = () => {
1539
+ var _cell$getValue;
1540
+
1541
+ return (_cell$getValue = cell.getValue()) != null ? _cell$getValue : instance.options.renderFallbackValue;
1542
+ };
1543
+
1538
1544
  return {
1539
1545
  getIsGrouped: () => column.getIsGrouped() && column.id === row.groupingColumnId,
1540
1546
  getIsPlaceholder: () => !cell.getIsGrouped() && column.getIsGrouped(),
@@ -1544,15 +1550,19 @@ const Grouping = {
1544
1550
  return !cell.getIsGrouped() && !cell.getIsPlaceholder() && ((_row$subRows = row.subRows) == null ? void 0 : _row$subRows.length) > 1;
1545
1551
  },
1546
1552
  renderAggregatedCell: () => {
1547
- var _column$columnDef$agg;
1553
+ if (process.env.NODE_ENV === 'development') {
1554
+ if (!column.columnDef.aggregatedCell) {
1555
+ console.warn('A columnDef.aggregatedCell template is recommended for displaying aggregated values.');
1556
+ }
1557
+ }
1548
1558
 
1549
- const template = (_column$columnDef$agg = column.columnDef.aggregatedCell) != null ? _column$columnDef$agg : column.columnDef.cell;
1559
+ const template = column.columnDef.aggregatedCell || column.columnDef.cell;
1550
1560
  return template ? instance._render(template, {
1551
1561
  instance,
1552
1562
  column,
1553
1563
  row,
1554
1564
  cell,
1555
- getValue: cell.getValue
1565
+ getValue: getRenderValue
1556
1566
  }) : null;
1557
1567
  }
1558
1568
  };
@@ -2535,8 +2545,10 @@ const Sorting = {
2535
2545
  // })
2536
2546
  // return
2537
2547
  // }
2548
+ // this needs to be outside of instance.setSorting to be in sync with rerender
2549
+ const nextSortingOrder = column.getNextSortingOrder();
2538
2550
  instance.setSorting(old => {
2539
- var _ref2, _column$columnDef$sor, _instance$options$ena, _instance$options$ena2;
2551
+ var _instance$options$ena, _instance$options$ena2;
2540
2552
 
2541
2553
  // Find any existing sorting for this column
2542
2554
  const existingSorting = old == null ? void 0 : old.find(d => d.id === column.id);
@@ -2561,30 +2573,29 @@ const Sorting = {
2561
2573
  } else {
2562
2574
  sortAction = 'replace';
2563
2575
  }
2564
- }
2576
+ } // Handle toggle states that will remove the sorting
2565
2577
 
2566
- const sortDescFirst = (_ref2 = (_column$columnDef$sor = column.columnDef.sortDescFirst) != null ? _column$columnDef$sor : instance.options.sortDescFirst) != null ? _ref2 : column.getAutoSortDir() === 'desc'; // Handle toggle states that will remove the sorting
2567
2578
 
2568
2579
  if (sortAction === 'toggle' && ( // Must be toggling
2569
2580
  (_instance$options$ena = instance.options.enableSortingRemoval) != null ? _instance$options$ena : true) && // If enableSortRemove, enable in general
2570
2581
  !hasDescDefined && ( // Must not be setting desc
2571
- multi ? (_instance$options$ena2 = instance.options.enableMultiRemove) != null ? _instance$options$ena2 : true : true) && ( // If multi, don't allow if enableMultiRemove
2572
- existingSorting != null && existingSorting.desc // Finally, detect if it should indeed be removed
2573
- ? !sortDescFirst : sortDescFirst)) {
2582
+ multi ? (_instance$options$ena2 = instance.options.enableMultiRemove) != null ? _instance$options$ena2 : true : true) && // If multi, don't allow if enableMultiRemove
2583
+ !nextSortingOrder // Finally, detect if it should indeed be removed
2584
+ ) {
2574
2585
  sortAction = 'remove';
2575
2586
  }
2576
2587
 
2577
2588
  if (sortAction === 'replace') {
2578
2589
  newSorting = [{
2579
2590
  id: column.id,
2580
- desc: hasDescDefined ? desc : !!sortDescFirst
2591
+ desc: hasDescDefined ? desc : nextSortingOrder === 'desc'
2581
2592
  }];
2582
2593
  } else if (sortAction === 'add' && old != null && old.length) {
2583
2594
  var _instance$options$max;
2584
2595
 
2585
2596
  newSorting = [...old, {
2586
2597
  id: column.id,
2587
- desc: hasDescDefined ? desc : !!sortDescFirst
2598
+ desc: hasDescDefined ? desc : nextSortingOrder === 'desc'
2588
2599
  }]; // Take latest n columns
2589
2600
 
2590
2601
  newSorting.splice(0, newSorting.length - ((_instance$options$max = instance.options.maxMultiSortColCount) != null ? _instance$options$max : Number.MAX_SAFE_INTEGER));
@@ -2593,7 +2604,7 @@ const Sorting = {
2593
2604
  newSorting = old.map(d => {
2594
2605
  if (d.id === column.id) {
2595
2606
  return { ...d,
2596
- desc: hasDescDefined ? desc : !(existingSorting != null && existingSorting.desc)
2607
+ desc: hasDescDefined ? desc : nextSortingOrder === 'desc'
2597
2608
  };
2598
2609
  }
2599
2610
 
@@ -2606,6 +2617,23 @@ const Sorting = {
2606
2617
  return newSorting;
2607
2618
  });
2608
2619
  },
2620
+ getNextSortingOrder: () => {
2621
+ var _ref2, _column$columnDef$sor;
2622
+
2623
+ const sortDescFirst = (_ref2 = (_column$columnDef$sor = column.columnDef.sortDescFirst) != null ? _column$columnDef$sor : instance.options.sortDescFirst) != null ? _ref2 : column.getAutoSortDir() === 'desc';
2624
+ const firstSortDirection = sortDescFirst ? 'desc' : 'asc';
2625
+ const isSorted = column.getIsSorted();
2626
+
2627
+ if (!isSorted) {
2628
+ return firstSortDirection;
2629
+ }
2630
+
2631
+ if (isSorted === firstSortDirection) {
2632
+ return isSorted === 'desc' ? 'asc' : 'desc';
2633
+ } else {
2634
+ return false;
2635
+ }
2636
+ },
2609
2637
  getCanSort: () => {
2610
2638
  var _column$columnDef$ena, _instance$options$ena3;
2611
2639
 
@@ -3055,6 +3083,12 @@ function createTable(_, __, options) {
3055
3083
  }
3056
3084
 
3057
3085
  function createCell(instance, row, column, columnId) {
3086
+ const getRenderValue = () => {
3087
+ var _cell$getValue;
3088
+
3089
+ return (_cell$getValue = cell.getValue()) != null ? _cell$getValue : instance.options.renderFallbackValue;
3090
+ };
3091
+
3058
3092
  const cell = {
3059
3093
  id: row.id + "_" + column.id,
3060
3094
  row,
@@ -3066,7 +3100,7 @@ function createCell(instance, row, column, columnId) {
3066
3100
  column,
3067
3101
  row,
3068
3102
  cell: cell,
3069
- getValue: cell.getValue
3103
+ getValue: getRenderValue
3070
3104
  }) : null;
3071
3105
  }
3072
3106
  };