d2coreui 23.0.60 → 23.0.62

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,25 @@
1
1
  # Changelog
2
2
 
3
+ ## 23.0.62
4
+
5
+ - `DataGrid`: the `numberColumn` type takes also `cellRendererParams.minFractionDigits` (capped at `fractionDigits`)
6
+ and `omitGrouping`, in the cells, the export and the filters. `minFractionDigits` needs `d2core` 23.0.22 or newer -
7
+ with an older one, columns of the same `fractionDigits` share the minimum of the first one rendered.
8
+
9
+ ## 23.0.61
10
+
11
+ - `DataGrid` / `ExtendedDataGrid`: `exportData(exportType, options)` exports the grid to a TXT, CSV or PDF file or to
12
+ the clipboard the same way as the toolbar export menu, so that the export can be started from a custom button
13
+ outside the grid; `options.onlySelectedRows` exports only the selected rows and `options.pageOrientation` sets the
14
+ PDF page orientation. `toolbar.hideExportButton` hides the built-in export menu button.
15
+ - `grid/export/contextMenu`: `runExport(exportType, props)` runs one export of the menu built by `getContextMenu`.
16
+ - `grid/export/contextMenu` / `DataGrid`: a failed export no longer leaves the grid exporting with the export menu
17
+ items disabled - `onExport(false)` is called also after a failure, and the progress notification is replaced by an
18
+ error one. For the PDF export `onExport(false)` comes once the file is saved, not when its generation starts. Clicks
19
+ on custom menu items no longer call `onExport`.
20
+ - `DataGrid` / `grid/export/contextMenu`: the CSV export starts with a UTF-8 BOM, so that Excel shows characters with
21
+ diacritics correctly instead of opening the file in the ANSI code page.
22
+
3
23
  ## 23.0.60
4
24
 
5
25
  - `ExtendedDataGrid`: `GridConfiguration.showUtcOffset` and `defaultShowUtcOffset` are no longer `true` by default -
package/README.md CHANGED
@@ -103,7 +103,7 @@ All paths are relative to `d2coreui/components/`.
103
103
  | `grid/cell/tableDefaultRowStyleRules` | `RowStyleRule`, `GridRowStyle` and the default row style rules. |
104
104
  | `grid/config/*` | Table configuration UI: `DataGridEditor`, `ColumnTransfer`, `RowStylePicker`, row height helpers. |
105
105
  | `grid/filter/*` | Column filters and `WildcardQuickFilterEngine` (`*` / `?` quick filter). |
106
- | `grid/export/contextMenu` | `getContextMenu` - row context menu with export (CSV, TXT, PDF, clipboard). |
106
+ | `grid/export/contextMenu` | `getContextMenu` - row context menu with export (CSV, TXT, PDF, clipboard); `runExport` - one export started outside the menu. |
107
107
  | `grid/export/progressPopup` | Export progress notification. |
108
108
  | `grid/header/simpleHeader`, `grid/panel/*`, `grid/detail/*` | Header, pagination, overlay and detail panel components used by the grids. |
109
109
  | `grid/columnUtils` | `ColumnUtils.mergeColumnsState`. |
@@ -2,7 +2,7 @@ import React, { ReactElement } from "react";
2
2
  import { MenuProps } from "antd";
3
3
  import { AgGridReact } from "ag-grid-react";
4
4
  import 'd2coreui/style/ag-grid/aggrid-adaptations.css';
5
- import { ContextMenuProps, PageOrientation } from "d2coreui/components/grid/export/contextMenu";
5
+ import { ContextMenuProps, ExportType, PageOrientation } from "d2coreui/components/grid/export/contextMenu";
6
6
  import { CellClassParams, CellFocusedEvent, CellMouseDownEvent, CellMouseOverEvent, ColDef, ColTypeDef, ColGroupDef, Column, ColumnEvent, NewColumnsLoadedEvent, ColumnState, FilterChangedEvent, FilterModifiedEvent, GridApi, GridOptions, GridReadyEvent, IRowNode, PaginationChangedEvent, RowClassParams, RowPinnedType, RowStyle, ViewportChangedEvent } from "ag-grid-community";
7
7
  import { UtcOffsetMode } from "d2core/i18n/localeHolder";
8
8
  import { GridRowStyle, RowStyleRule } from "d2coreui/components/grid/cell/tableDefaultRowStyleRules";
@@ -70,8 +70,10 @@ export type FilterRenderer = (value: string, changeValue: (newValue: string) =>
70
70
  * search is visible, and takes `hide` from the current column state when the column is already known.
71
71
  * ag-grid's `lockVisible` is honoured by the grid's own column menu and columns configuration dialog.
72
72
  *
73
- * Besides ag-grid's own, these values can be used in `type`: `numberColumn` (localized number, fraction digits from
74
- * `cellRendererParams.fractionDigits`), `dateColumn`, `dateTimeColumn`, `fromNowDateTimeColumn` (D2000 unix time values,
73
+ * Besides ag-grid's own, these values can be used in `type`: `numberColumn` (localized number - in the cells, the export
74
+ * and the filters - with the maximum of fraction digits from `cellRendererParams.fractionDigits`, the minimum (capped
75
+ * at the maximum) from `cellRendererParams.minFractionDigits` and without grouping separators by
76
+ * `cellRendererParams.omitGrouping`), `dateColumn`, `dateTimeColumn`, `fromNowDateTimeColumn` (D2000 unix time values,
75
77
  * formatted according to `hideYear`/`showMilliseconds`/`showUtcOffset`), `localizedArrayColumn` (array of i18n keys),
76
78
  * `escapedValueColumn` (line breaks shown as spaces), `alignRight`, and the filter types `textColumnFilter`
77
79
  * (client-side text filter), `textColumnServerFilter` and `customColumnServerFilter` (filter applied explicitly, the latter
@@ -255,6 +257,11 @@ export interface DataGridProps<TData = any> extends Omit<GridOptions<TData>, "co
255
257
  toolbarButtons?: ReactElement;
256
258
  /** Hides the "first-last of total" record range shown when paging is disabled (`pageSize` -1). */
257
259
  hideRecordCount?: boolean;
260
+ /**
261
+ * Hides the export menu button, e.g. when the export is started by a custom button through
262
+ * {@link DataGrid.exportData}. The row context menu keeps its export items.
263
+ */
264
+ hideExportButton?: boolean;
258
265
  /** Extra content rendered at the end of the toolbar, after the pager or record count. */
259
266
  extraContent?: React.ReactNode;
260
267
  /**
@@ -442,6 +449,11 @@ export default class DataGrid<TData extends {
442
449
  lastNavigationEvent?: NavigationEventEnum;
443
450
  /** Pagination event received before the grid API was available, replayed once the grid is ready. */
444
451
  lastPaginationEvent?: PaginationChangedEvent | null;
452
+ /**
453
+ * Whether an export is running; unlike `state.exporting` it is set synchronously, so two {@link exportData}
454
+ * calls in one tick do not both start an export.
455
+ */
456
+ private exportRunning;
445
457
  /**
446
458
  * Object passed to ag-grid as `context`; the grid's overlays, header and filter components read the overlay
447
459
  * renderers, `onFilterConfirmed`, `fontSize` and `lineHeight` from it.
@@ -449,6 +461,7 @@ export default class DataGrid<TData extends {
449
461
  table_context: any;
450
462
  private readonly getTheme;
451
463
  constructor(props: Readonly<DataGridProps<TData>>);
464
+ private static formatNumberCell;
452
465
  private static getTimeCellValue;
453
466
  private static getCellValue;
454
467
  /**
@@ -598,6 +611,28 @@ export default class DataGrid<TData extends {
598
611
  private getCustomComponents;
599
612
  private onPageOrientationChange;
600
613
  private onExport;
614
+ /**
615
+ * Properties of an export menu or of {@link runExport}: the date-time format of the grid, its export content and
616
+ * export state, with `overrides` applied.
617
+ */
618
+ private getExportProps;
619
+ /**
620
+ * Exports the grid the same way as the toolbar export menu, for an export started from a custom button (hide the
621
+ * built-in one with `toolbar.hideExportButton`). Rows are exported with the displayed columns, filter and sort, and
622
+ * the date-time format of the grid; `exportMenu.onExported` is called after a successful export.
623
+ *
624
+ * Nothing is exported before the grid is ready, while it is loading or while another export is running.
625
+ *
626
+ * @param exportType file type, or `"clipboard"` to copy the rows to the clipboard
627
+ * @param options.onlySelectedRows export only the selected rows, as the row context menu does
628
+ * @param options.pageOrientation page orientation of the PDF export; defaults to the one last chosen in the export menu
629
+ * @returns resolved once the content is saved or copied, rejected when the export fails (an error notification is
630
+ * shown as well)
631
+ */
632
+ exportData(exportType: ExportType, options?: {
633
+ onlySelectedRows?: boolean;
634
+ pageOrientation?: PageOrientation;
635
+ }): Promise<void>;
601
636
  /** Renders the toolbar row: toolbar with search, pager (or record range when paging is disabled) and `toolbar.extraContent`. */
602
637
  renderNavigationPanel(): ReactElement;
603
638
  /**
@@ -21,7 +21,7 @@ import DataGridEditor from "d2coreui/components/grid/config/dataGridEditor";
21
21
  import i18n from "d2core/i18n/i18n";
22
22
  import AutoCompleteInput from "d2coreui/components/input/autoCompleteInput";
23
23
  import isEqual from "lodash/isEqual";
24
- import getContextMenu from "d2coreui/components/grid/export/contextMenu";
24
+ import getContextMenu, { runExport } from "d2coreui/components/grid/export/contextMenu";
25
25
  import LoadingOverlay from "d2coreui/components/grid/panel/loadingOverlay";
26
26
  import NoRecordsOverlay from "d2coreui/components/grid/panel/noRecordsOverlay";
27
27
  import at from "lodash/at";
@@ -153,6 +153,11 @@ class DataGrid extends React.Component {
153
153
  this.firstLoadDone = false;
154
154
  /** Pagination event received before the grid API was available, replayed once the grid is ready. */
155
155
  this.lastPaginationEvent = null;
156
+ /**
157
+ * Whether an export is running; unlike `state.exporting` it is set synchronously, so two {@link exportData}
158
+ * calls in one tick do not both start an export.
159
+ */
160
+ this.exportRunning = false;
156
161
  this.state = {
157
162
  pageIndex: 0,
158
163
  pageSize: null,
@@ -249,20 +254,18 @@ class DataGrid extends React.Component {
249
254
  },
250
255
  "numberColumn": {
251
256
  cellRenderer: (params) => {
252
- var _a, _b;
253
257
  const value = DataGrid.getCellValue(params);
254
258
  if (value !== undefined && value !== null) {
255
- return LocaleHolder.formatNumber(value, (_b = (_a = params.colDef) === null || _a === void 0 ? void 0 : _a.cellRendererParams) === null || _b === void 0 ? void 0 : _b.fractionDigits);
259
+ return DataGrid.formatNumberCell(value, params.colDef);
256
260
  }
257
261
  else {
258
262
  return "";
259
263
  }
260
264
  },
261
265
  valueFormatter: (params) => {
262
- var _a, _b;
263
266
  const value = DataGrid.getCellValue(params);
264
267
  if (value !== undefined && value !== null) {
265
- return LocaleHolder.formatNumber(value, (_b = (_a = params.colDef) === null || _a === void 0 ? void 0 : _a.cellRendererParams) === null || _b === void 0 ? void 0 : _b.fractionDigits);
268
+ return DataGrid.formatNumberCell(value, params.colDef);
266
269
  }
267
270
  else {
268
271
  return "";
@@ -272,20 +275,18 @@ class DataGrid extends React.Component {
272
275
  textCustomComparator: this.textCustomComparator
273
276
  },
274
277
  filterValueGetter: function (params) {
275
- var _a, _b;
276
278
  const value = DataGrid.getCellValue(params);
277
279
  if (value) {
278
- return LocaleHolder.formatNumber(value, (_b = (_a = params.colDef) === null || _a === void 0 ? void 0 : _a.cellRendererParams) === null || _b === void 0 ? void 0 : _b.fractionDigits);
280
+ return DataGrid.formatNumberCell(value, params.colDef);
279
281
  }
280
282
  else {
281
283
  return "";
282
284
  }
283
285
  },
284
286
  getQuickFilterText: function (params) {
285
- var _a, _b;
286
287
  const value = DataGrid.getCellValue(params);
287
288
  if (value !== undefined) {
288
- return LocaleHolder.formatNumber(value, (_b = (_a = params.colDef) === null || _a === void 0 ? void 0 : _a.cellRendererParams) === null || _b === void 0 ? void 0 : _b.fractionDigits);
289
+ return DataGrid.formatNumberCell(value, params.colDef);
289
290
  }
290
291
  else {
291
292
  return "";
@@ -457,6 +458,13 @@ class DataGrid extends React.Component {
457
458
  "alignRight": {}
458
459
  };
459
460
  }
461
+ static formatNumberCell(value, colDef) {
462
+ var _a;
463
+ const { fractionDigits, minFractionDigits, omitGrouping } = (_a = colDef === null || colDef === void 0 ? void 0 : colDef.cellRendererParams) !== null && _a !== void 0 ? _a : {};
464
+ // Intl throws a RangeError when the minimum exceeds the maximum
465
+ const minimum = fractionDigits !== undefined && minFractionDigits !== undefined ? Math.min(minFractionDigits, fractionDigits) : minFractionDigits;
466
+ return LocaleHolder.getNumberFormatter(fractionDigits, omitGrouping, minimum).format(value);
467
+ }
460
468
  static getTimeCellValue(props) {
461
469
  const value = DataGrid.getCellValue(props);
462
470
  return value === undefined || value === null || value === D2000UnixTimeNullValue ? undefined : value;
@@ -1687,8 +1695,40 @@ class DataGrid extends React.Component {
1687
1695
  this.setState({ pageOrientation: pageOrientation });
1688
1696
  }
1689
1697
  onExport(start) {
1698
+ this.exportRunning = start;
1690
1699
  this.setState({ exporting: start });
1691
1700
  }
1701
+ /**
1702
+ * Properties of an export menu or of {@link runExport}: the date-time format of the grid, its export content and
1703
+ * export state, with `overrides` applied.
1704
+ */
1705
+ getExportProps(overrides) {
1706
+ return Object.assign({ showMilliseconds: !!this.props.showMilliseconds, showYear: !this.props.hideYear, showUtcOffset: LocaleHolder.resolveUtcOffsetMode(this.props.showUtcOffset), getExportContent: this.getExportContent, pageOrientation: this.state.pageOrientation, exporting: this.state.exporting, onPageOrientationChange: this.onPageOrientationChange, onExport: this.onExport }, overrides);
1707
+ }
1708
+ /**
1709
+ * Exports the grid the same way as the toolbar export menu, for an export started from a custom button (hide the
1710
+ * built-in one with `toolbar.hideExportButton`). Rows are exported with the displayed columns, filter and sort, and
1711
+ * the date-time format of the grid; `exportMenu.onExported` is called after a successful export.
1712
+ *
1713
+ * Nothing is exported before the grid is ready, while it is loading or while another export is running.
1714
+ *
1715
+ * @param exportType file type, or `"clipboard"` to copy the rows to the clipboard
1716
+ * @param options.onlySelectedRows export only the selected rows, as the row context menu does
1717
+ * @param options.pageOrientation page orientation of the PDF export; defaults to the one last chosen in the export menu
1718
+ * @returns resolved once the content is saved or copied, rejected when the export fails (an error notification is
1719
+ * shown as well)
1720
+ */
1721
+ async exportData(exportType, options) {
1722
+ var _a, _b;
1723
+ if (!this.isActive() || this.state.loading || this.exportRunning) {
1724
+ return;
1725
+ }
1726
+ await runExport(exportType, this.getExportProps({
1727
+ onlySelectedNodes: options === null || options === void 0 ? void 0 : options.onlySelectedRows,
1728
+ onExported: (_a = this.props.exportMenu) === null || _a === void 0 ? void 0 : _a.onExported,
1729
+ pageOrientation: (_b = options === null || options === void 0 ? void 0 : options.pageOrientation) !== null && _b !== void 0 ? _b : this.state.pageOrientation,
1730
+ }));
1731
+ }
1692
1732
  /** Renders the toolbar row: toolbar with search, pager (or record range when paging is disabled) and `toolbar.extraContent`. */
1693
1733
  renderNavigationPanel() {
1694
1734
  var _a, _b, _c, _d;
@@ -1713,16 +1753,16 @@ class DataGrid extends React.Component {
1713
1753
  * With `toolbar.compact` the export and settings buttons move into a popover opened by one button.
1714
1754
  */
1715
1755
  renderToolBarWithSearch() {
1716
- var _a, _b, _c, _d;
1756
+ var _a, _b, _c, _d, _e;
1717
1757
  const compact = (_a = this.props.toolbar) === null || _a === void 0 ? void 0 : _a.compact;
1718
1758
  const search = this.props.search;
1719
1759
  return (React.createElement(Space.Compact, { key: "filter-panel", className: "datagrid-autocomplete-input-group", style: { whiteSpace: "nowrap", flexGrow: 1, display: "flex" } }, (_b = this.props.toolbar) === null || _b === void 0 ? void 0 :
1720
1760
  _b.toolbarButtons,
1721
1761
  compact ? this.renderCompactMenu() : React.createElement(React.Fragment, null,
1722
- this.renderExportButton(),
1762
+ !((_c = this.props.toolbar) === null || _c === void 0 ? void 0 : _c.hideExportButton) && this.renderExportButton(),
1723
1763
  this.renderSettingsButton()),
1724
- !(search === null || search === void 0 ? void 0 : search.hidden) && !(search === null || search === void 0 ? void 0 : search.searchRenderer) && React.createElement(AutoCompleteInput, { style: { minWidth: 100, flexGrow: 1 }, placeholder: search === null || search === void 0 ? void 0 : search.placeholder, stringFormatter: search === null || search === void 0 ? void 0 : search.stringFormatter, value: search === null || search === void 0 ? void 0 : search.searchString, options: search === null || search === void 0 ? void 0 : search.searchHistory, onChange: search === null || search === void 0 ? void 0 : search.onChange, onSearch: search === null || search === void 0 ? void 0 : search.onSearch, disabled: this.state.loading || ((_c = this.props.filter) === null || _c === void 0 ? void 0 : _c.columnSearchVisible) }), (_d = search === null || search === void 0 ? void 0 : search.searchRenderer) === null || _d === void 0 ? void 0 :
1725
- _d.call(search),
1764
+ !(search === null || search === void 0 ? void 0 : search.hidden) && !(search === null || search === void 0 ? void 0 : search.searchRenderer) && React.createElement(AutoCompleteInput, { style: { minWidth: 100, flexGrow: 1 }, placeholder: search === null || search === void 0 ? void 0 : search.placeholder, stringFormatter: search === null || search === void 0 ? void 0 : search.stringFormatter, value: search === null || search === void 0 ? void 0 : search.searchString, options: search === null || search === void 0 ? void 0 : search.searchHistory, onChange: search === null || search === void 0 ? void 0 : search.onChange, onSearch: search === null || search === void 0 ? void 0 : search.onSearch, disabled: this.state.loading || ((_d = this.props.filter) === null || _d === void 0 ? void 0 : _d.columnSearchVisible) }), (_e = search === null || search === void 0 ? void 0 : search.searchRenderer) === null || _e === void 0 ? void 0 :
1765
+ _e.call(search),
1726
1766
  !(search === null || search === void 0 ? void 0 : search.searchButtonHidden) && this.renderSearchButton()));
1727
1767
  }
1728
1768
  /**
@@ -1733,27 +1773,22 @@ class DataGrid extends React.Component {
1733
1773
  const close = () => this.setState({ compactMenuOpen: false });
1734
1774
  return (React.createElement(Popover, { open: this.state.compactMenuOpen, onOpenChange: open => this.setState({ compactMenuOpen: open }), trigger: "click", placement: "bottomLeft",
1735
1775
  // the menus are built only while the popover is open - the grid renders often, e.g. while scrolling
1736
- destroyOnHidden: true, content: React.createElement(RenderLater, null, () => React.createElement(Space.Compact, null,
1737
- this.renderExportButton(close),
1738
- this.renderSettingsButton(close))) },
1776
+ destroyOnHidden: true, content: React.createElement(RenderLater, null, () => {
1777
+ var _a;
1778
+ return React.createElement(Space.Compact, null,
1779
+ !((_a = this.props.toolbar) === null || _a === void 0 ? void 0 : _a.hideExportButton) && this.renderExportButton(close),
1780
+ this.renderSettingsButton(close));
1781
+ }) },
1739
1782
  React.createElement(Button, { title: i18n("Extended table features") }, this.state.loading ? React.createElement(LoadingOutlined, null) : React.createElement(EllipsisOutlined, null))));
1740
1783
  }
1741
1784
  /** Export button with the export menu; `onUsed` is called after a menu item is clicked. */
1742
1785
  renderExportButton(onUsed) {
1743
1786
  var _a, _b, _c;
1744
- const exportMenu = getContextMenu({
1745
- showMilliseconds: !!this.props.showMilliseconds,
1746
- showYear: !this.props.hideYear,
1747
- showUtcOffset: LocaleHolder.resolveUtcOffsetMode(this.props.showUtcOffset),
1787
+ const exportMenu = getContextMenu(this.getExportProps({
1748
1788
  renderer: (_a = this.props.exportMenu) === null || _a === void 0 ? void 0 : _a.renderer,
1749
1789
  onClick: (_b = this.props.exportMenu) === null || _b === void 0 ? void 0 : _b.onClick,
1750
1790
  onExported: (_c = this.props.exportMenu) === null || _c === void 0 ? void 0 : _c.onExported,
1751
- getExportContent: this.getExportContent,
1752
- pageOrientation: this.state.pageOrientation,
1753
- exporting: this.state.exporting,
1754
- onPageOrientationChange: this.onPageOrientationChange,
1755
- onExport: this.onExport
1756
- });
1791
+ }));
1757
1792
  return (React.createElement(Dropdown, { trigger: ["click"], menu: withOnUsed(exportMenu, onUsed), disabled: this.state.loading },
1758
1793
  React.createElement(Button, { title: i18n("Export") },
1759
1794
  React.createElement(DownloadOutlined, null))));
@@ -1945,20 +1980,12 @@ class DataGrid extends React.Component {
1945
1980
  // very nasty trick how remove error of ResizeObservers infinite cycling - because popover (with dynamic size) is placed in in resizable panel
1946
1981
  const parentNode = ((_a = triggerNode === null || triggerNode === void 0 ? void 0 : triggerNode.parentNode) !== null && _a !== void 0 ? _a : document.body);
1947
1982
  return parentNode;
1948
- }, menu: getContextMenu({
1983
+ }, menu: getContextMenu(this.getExportProps({
1949
1984
  onlySelectedNodes: true,
1950
- showMilliseconds: !!this.props.showMilliseconds,
1951
- showYear: !this.props.hideYear,
1952
- showUtcOffset: LocaleHolder.resolveUtcOffsetMode(this.props.showUtcOffset),
1953
1985
  renderer: (_a = this.props.contextMenu) === null || _a === void 0 ? void 0 : _a.renderer,
1954
1986
  onClick: (_b = this.props.contextMenu) === null || _b === void 0 ? void 0 : _b.onClick,
1955
- getExportContent: this.getExportContent,
1956
1987
  hideNotificationOnClipboardCopy: true,
1957
- pageOrientation: this.state.pageOrientation,
1958
- exporting: this.state.exporting,
1959
- onPageOrientationChange: this.onPageOrientationChange,
1960
- onExport: this.onExport
1961
- }), open: this.state.contextMenuVisible, onOpenChange: (visible) => {
1988
+ })), open: this.state.contextMenuVisible, onOpenChange: (visible) => {
1962
1989
  if (!visible) {
1963
1990
  this.setState({ contextMenuVisible: false });
1964
1991
  }