@linzjs/step-ag-grid 29.1.6 → 29.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.
@@ -2760,7 +2760,7 @@ ModuleRegistry.registerModules([AllCommunityModule]);
2760
2760
  /**
2761
2761
  * Wrapper for AgGrid to add commonly used functionality.
2762
2762
  */
2763
- const Grid = ({ 'data-testid': dataTestId, defaultPostSort = true, rowSelection = 'multiple', suppressColumnVirtualization = true, theme = 'ag-theme-step-default', sizeColumns = 'auto', selectColumnPinned = 'left', contextMenuSelectRow = false, singleClickEdit = false, rowData, rowHeight = theme === 'ag-theme-step-default' ? 40 : theme === 'ag-theme-step-compact' ? 36 : 40, selectable, onCellFocused: paramsOnCellFocused, ...params }) => {
2763
+ const Grid = ({ 'data-testid': dataTestId, defaultPostSort = true, rowSelection = 'multiple', suppressColumnVirtualization = true, theme = 'ag-theme-step-default', sizeColumns = 'auto', selectColumnPinned = 'left', contextMenuSelectRow = false, singleClickEdit = false, rowData, rowHeight = theme === 'ag-theme-step-default' ? 40 : theme === 'ag-theme-step-compact' ? 36 : 40, selectable, allowResizeInStorybook, onCellFocused: paramsOnCellFocused, ...params }) => {
2764
2764
  const { gridReady, gridRenderState, setApis, ensureRowVisible, getFirstRowId, selectRowsById, focusByRowById, ensureSelectedRowIsVisible, autoSizeColumns, sizeColumnsToFit, externallySelectedItemsAreInSync, setExternallySelectedItemsAreInSync, isExternalFilterPresent, doesExternalFilterPass, setOnBulkEditingComplete, getColDef, showNoRowsOverlay, prePopupOps, startCellEditing, } = useGridContext();
2765
2765
  const { updatedDep, updatingCols } = useContext(GridUpdatingContext);
2766
2766
  const gridDivRef = useRef(null);
@@ -2791,7 +2791,11 @@ const Grid = ({ 'data-testid': dataTestId, defaultPostSort = true, rowSelection
2791
2791
  }
2792
2792
  const skipHeader = sizeColumns === 'auto-skip-headers' && gridRendered === 'rows-visible';
2793
2793
  if (sizeColumns === 'auto' || skipHeader) {
2794
- const result = autoSizeColumns({ skipHeader, userSizedColIds: userSizedColIds.current, includeFlex: true });
2794
+ const result = autoSizeColumns({
2795
+ skipHeader,
2796
+ userSizedColIds: new Set(userSizedColIds.current.keys()),
2797
+ includeFlex: true,
2798
+ });
2795
2799
  if (!result) {
2796
2800
  needsAutoSize.current = true;
2797
2801
  return;
@@ -2826,10 +2830,27 @@ const Grid = ({ 'data-testid': dataTestId, defaultPostSort = true, rowSelection
2826
2830
  needsAutoSize.current = false;
2827
2831
  }, [autoSizeColumns, gridRenderState, params, rowData, sizeColumns, sizeColumnsToFit]);
2828
2832
  const lastOwnerDocumentRef = useRef();
2833
+ const wasVisibleRef = useRef(false);
2829
2834
  /**
2830
2835
  * Auto-size windows that had deferred auto-size
2836
+ * Reset focus if panel went from invisible to visible.
2831
2837
  */
2832
2838
  useInterval(() => {
2839
+ // If grid has become visible after previously being hidden, then refocus the last focused cell.
2840
+ const visible = !!gridDivRef.current?.checkVisibility();
2841
+ if (visible && !wasVisibleRef.current) {
2842
+ wasVisibleRef.current = true;
2843
+ const el = window.__stepaggrid_lastfocuseventtarget;
2844
+ if (el) {
2845
+ // Setting this to null will cause a new refocus event
2846
+ window.__stepaggrid_lastfocuseventtarget = null;
2847
+ // Check element is still part of document
2848
+ if (el.checkVisibility()) {
2849
+ el.focus();
2850
+ }
2851
+ }
2852
+ }
2853
+ wasVisibleRef.current = visible;
2833
2854
  // Check if window has been popped out and needs resize
2834
2855
  const currentDocument = gridDivRef.current?.ownerDocument;
2835
2856
  if (currentDocument !== lastOwnerDocumentRef.current) {
@@ -2970,7 +2991,11 @@ const Grid = ({ 'data-testid': dataTestId, defaultPostSort = true, rowSelection
2970
2991
  return;
2971
2992
  const skipHeader = sizeColumns === 'auto-skip-headers';
2972
2993
  if (hasSetContentSize.current) {
2973
- autoSizeColumns({ skipHeader, userSizedColIds: userSizedColIds.current, colIds: colIdsEdited.current });
2994
+ autoSizeColumns({
2995
+ skipHeader,
2996
+ userSizedColIds: new Set(userSizedColIds.current.keys()),
2997
+ colIds: colIdsEdited.current,
2998
+ });
2974
2999
  }
2975
3000
  colIdsEdited.current.clear();
2976
3001
  }, [autoSizeColumns, rowData?.length, setInitialContentSize, sizeColumns, updatedDep, updatingCols]);
@@ -3068,7 +3093,7 @@ const Grid = ({ 'data-testid': dataTestId, defaultPostSort = true, rowSelection
3068
3093
  if (hasSetContentSize.current) {
3069
3094
  autoSizeColumns({
3070
3095
  skipHeader,
3071
- userSizedColIds: userSizedColIds.current,
3096
+ userSizedColIds: new Set(userSizedColIds.current.keys()),
3072
3097
  colIds: colIdsEdited.current,
3073
3098
  });
3074
3099
  }
@@ -3097,25 +3122,36 @@ const Grid = ({ 'data-testid': dataTestId, defaultPostSort = true, rowSelection
3097
3122
  /**
3098
3123
  * Resize columns to fit if required on window/container resize
3099
3124
  */
3100
- const onGridSizeChanged = useCallback(() => {
3101
- if (sizeColumns !== 'none') {
3102
- sizeColumnsToFit();
3125
+ const onGridSizeChanged = useCallback((event) => {
3126
+ if (sizeColumns !== 'none' && (!window.__STORYBOOK_PREVIEW__ || allowResizeInStorybook)) {
3127
+ const columnLimits = [
3128
+ ...userSizedColIds.current.entries().map(([c, w]) => ({
3129
+ key: c,
3130
+ minWidth: w,
3131
+ })),
3132
+ ];
3133
+ event.api.sizeColumnsToFit({ columnLimits });
3103
3134
  }
3104
- }, [sizeColumns, sizeColumnsToFit]);
3135
+ }, [allowResizeInStorybook, sizeColumns]);
3105
3136
  /**
3106
3137
  * Set of column I'd's that are prevented from auto-sizing as they are user set
3107
3138
  */
3108
- const userSizedColIds = useRef(new Set());
3139
+ const userSizedColIds = useRef(new Map());
3109
3140
  /**
3110
3141
  * Lock/unlock column width on user edit/reset.
3111
3142
  */
3112
3143
  const onColumnResized = useCallback((e) => {
3113
3144
  const colId = e.column?.getColId();
3114
- if (colId == null)
3145
+ if (colId == null) {
3146
+ return;
3147
+ }
3148
+ const width = e.column?.getActualWidth();
3149
+ if (width == null) {
3115
3150
  return;
3151
+ }
3116
3152
  switch (e.source) {
3117
- case 'uiColumnDragged':
3118
- userSizedColIds.current.add(colId);
3153
+ case 'uiColumnResized':
3154
+ userSizedColIds.current.set(colId, width);
3119
3155
  break;
3120
3156
  case 'autosizeColumns':
3121
3157
  userSizedColIds.current.delete(colId);
@@ -3156,7 +3192,7 @@ const Grid = ({ 'data-testid': dataTestId, defaultPostSort = true, rowSelection
3156
3192
  }
3157
3193
  }, [clearHighlightRowClasses]);
3158
3194
  const onCellFocused = useCallback((event) => {
3159
- if (!paramsOnCellFocused || event.rowIndex == null) {
3195
+ if (event.rowIndex == null) {
3160
3196
  return;
3161
3197
  }
3162
3198
  const api = event.api;
@@ -3170,15 +3206,16 @@ const Grid = ({ 'data-testid': dataTestId, defaultPostSort = true, rowSelection
3170
3206
  if (!colDef || typeof colDef === 'string') {
3171
3207
  return;
3172
3208
  }
3173
- // Prevent repeated callbacks to cell focus when it focus didn't change
3209
+ // Prevent repeated callbacks to cell focus when focus didn't change
3174
3210
  const { sourceEvent } = event;
3175
3211
  if (sourceEvent) {
3176
- if (window.__stepaggrid_lastfocuseventtarget === sourceEvent.target) {
3212
+ const cell = sourceEvent.target?.closest?.('.ag-cell') ?? null;
3213
+ if (window.__stepaggrid_lastfocuseventtarget === cell) {
3177
3214
  return;
3178
3215
  }
3179
- window.__stepaggrid_lastfocuseventtarget = sourceEvent.target;
3216
+ window.__stepaggrid_lastfocuseventtarget = cell;
3180
3217
  }
3181
- paramsOnCellFocused({ colDef, data });
3218
+ paramsOnCellFocused?.({ colDef, data });
3182
3219
  }, [paramsOnCellFocused]);
3183
3220
  const onRowDragEnd = useCallback((event) => {
3184
3221
  clearHighlightRowClasses();
@@ -5397,8 +5434,9 @@ const GridContextProvider = (props) => {
5397
5434
  * Resize columns to fit container
5398
5435
  */
5399
5436
  const autoSizeColumns = useCallback(({ skipHeader, colIds, userSizedColIds, includeFlex } = {}) => {
5400
- if (!gridApi || !gridApi.getColumnState())
5437
+ if (!gridApi || !gridApi.getColumnState()) {
5401
5438
  return null;
5439
+ }
5402
5440
  const colIdsSet = colIds instanceof Set ? colIds : new Set(colIds);
5403
5441
  const colsToResize = gridApi.getColumnState()?.filter?.((colState) => {
5404
5442
  const colId = colState.colId;