Skip to content

Commit

Permalink
Merge branch 'feat/add-options-button-in-table' into alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
mguellsegarra committed Oct 2, 2024
2 parents 59ef0f9 + 3f1ecc3 commit e1b8aee
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 39 deletions.
73 changes: 43 additions & 30 deletions src/components/InfiniteTable/InfiniteTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ const InfiniteTableComp = forwardRef<InfiniteTableRef, InfiniteTableProps>(
const datasourceRef = useRef<{
getRows: (params: IGetRowsParams) => void;
}>();
const notifyColumnChanges = useRef(false);
const firstTimeResized = useRef(false);

useDeepCompareEffect(() => {
gridRef.current?.api?.forEachNode((node) => {
Expand Down Expand Up @@ -146,34 +148,35 @@ const InfiniteTableComp = forwardRef<InfiniteTableRef, InfiniteTableProps>(
onGetColumnsState,
});

const onColumnChanged = useCallback(() => {
const state = gridRef?.current?.api.getColumnState();
if (!state) {
return;
}
if (areStatesEqual(state, columnsPersistedStateRef.current)) {
return;
}
applyAndUpdateNewState(state);
onColumnsChangedProps?.(state);
}, [
applyAndUpdateNewState,
columnsPersistedStateRef,
onColumnsChangedProps,
]);

const onColumnMoved = useCallback(() => {
onColumnChanged();
}, [onColumnChanged]);
const debouncedOnColumnChanged = useMemo(
() =>
debounce(() => {
const state = gridRef?.current?.api.getColumnState();
if (!state) {
return;
}
if (areStatesEqual(state, columnsPersistedStateRef.current)) {
return;
}
if (!notifyColumnChanges.current) {
notifyColumnChanges.current = true;
return;
}
applyAndUpdateNewState(state);
onColumnsChangedProps?.(state);
}, 300),
[applyAndUpdateNewState, columnsPersistedStateRef, onColumnsChangedProps],
);

const onColumnResized = useCallback(
(event: ColumnResizedEvent) => {
if (!event.finished) {
return;
}
onColumnChanged();
},
[onColumnChanged],
const debouncedOnColumnResized = useMemo(
() =>
debounce((event: ColumnResizedEvent) => {
if (!event.finished) {
return;
}
debouncedOnColumnChanged();
}, 300),
[debouncedOnColumnChanged],
);

const getSortedFields = useCallback(():
Expand Down Expand Up @@ -258,9 +261,11 @@ const InfiniteTableComp = forwardRef<InfiniteTableRef, InfiniteTableProps>(
strings?.["resetTableViewLabel"] || "resetTableViewLabel"
}
onResetTableView={async () => {
notifyColumnChanges.current = false;
applyAndUpdateNewState([]);
gridRef.current?.api.resetColumnState();
applyAutoFitState();
onColumnsChangedProps?.([]);
}}
/>
),
Expand All @@ -282,6 +287,7 @@ const InfiniteTableComp = forwardRef<InfiniteTableRef, InfiniteTableProps>(
strings,
applyAndUpdateNewState,
applyAutoFitState,
onColumnsChangedProps,
]);

const scrollToSavedPosition = useCallback(() => {
Expand Down Expand Up @@ -353,6 +359,11 @@ const InfiniteTableComp = forwardRef<InfiniteTableRef, InfiniteTableProps>(
});
}

if (!columnsPersistedStateRef.current && !firstTimeResized.current) {
firstTimeResized.current = true;
applyAutoFitState();
}

dataIsLoading.current = false;
gridRef.current?.api.hideOverlay();
if (firstTimeDataLoaded.current) {
Expand All @@ -369,8 +380,10 @@ const InfiniteTableComp = forwardRef<InfiniteTableRef, InfiniteTableProps>(
onRequestData,
getSortedFields,
hasStatusColumn,
memoizedOnRowStatus,
selectedRowKeys,
columnsPersistedStateRef,
memoizedOnRowStatus,
applyAutoFitState,
scrollToSavedPosition,
],
);
Expand Down Expand Up @@ -473,8 +486,8 @@ const InfiniteTableComp = forwardRef<InfiniteTableRef, InfiniteTableProps>(
suppressRowClickSelection={true}
rowBuffer={5}
rowSelection={"multiple"}
onDragStopped={onColumnMoved}
onColumnResized={onColumnResized}
onDragStopped={debouncedOnColumnChanged}
onColumnResized={debouncedOnColumnResized}
rowModelType={"infinite"}
cacheBlockSize={30}
onSelectionChanged={onSelectionChanged}
Expand Down
2 changes: 1 addition & 1 deletion src/components/InfiniteTable/columnStateHelper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const STATUS_COLUMN = "$status";
export const CHECKBOX_COLUMN = "$checkbox";
export const FIXED_COLUMNS_TO_IGNORE = [CHECKBOX_COLUMN];
export const FIXED_COLUMNS_TO_IGNORE = [CHECKBOX_COLUMN, STATUS_COLUMN];
export const ALL_COLUMNS_TO_IGNORE = [...FIXED_COLUMNS_TO_IGNORE];

export const getPersistedColumnState = ({
Expand Down
9 changes: 1 addition & 8 deletions src/components/InfiniteTable/useColumnState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export const useColumnState = ({
columns: TableColumn[];
onGetColumnsState?: () => ColumnState[] | undefined;
}) => {
const firstTimeResized = useRef(false);
const columnsPersistedStateRef = useRef<ColumnState[]>();

const columnsToIgnore = FIXED_COLUMNS_TO_IGNORE;
Expand Down Expand Up @@ -95,14 +94,8 @@ export const useColumnState = ({

if (columnsPersistedStateRef.current) {
applyPersistedState();
return;
}

if (!columnsPersistedStateRef.current && !firstTimeResized.current) {
firstTimeResized.current = true;
applyAutoFitState();
}
}, [applyAutoFitState, applyPersistedState, columns, onGetColumnsState]);
}, [applyPersistedState, columns, onGetColumnsState]);

return {
loadPersistedColumnState,
Expand Down

0 comments on commit e1b8aee

Please sign in to comment.