Skip to content

Commit c219751

Browse files
committed
fix: better code
1 parent d5935a4 commit c219751

File tree

5 files changed

+38
-33
lines changed

5 files changed

+38
-33
lines changed

src/components/PaginatedTable/PaginatedTableWithLayout.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type {PaginatedTableState} from './types';
99
export interface PaginatedTableWithLayoutProps {
1010
controls?: React.ReactNode;
1111
table: React.ReactNode;
12-
tableWrapperProps?: TableWrapperProps;
12+
tableWrapperProps?: Omit<TableWrapperProps, 'children'>;
1313
error?: React.ReactNode;
1414
initialState?: Partial<PaginatedTableState>;
1515
fullHeight?: boolean;
@@ -21,7 +21,7 @@ const TableWrapper = ({
2121
tableWrapperProps,
2222
}: {
2323
table: React.ReactNode;
24-
tableWrapperProps?: TableWrapperProps;
24+
tableWrapperProps?: Omit<TableWrapperProps, 'children'>;
2525
}) => {
2626
const {tableState} = usePaginatedTableState();
2727
const {sortParams} = tableState;

src/components/ResizeableDataTable/ResizeableDataTable.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import React from 'react';
2+
13
import type {Column, DataTableProps, Settings} from '@gravity-ui/react-data-table';
24
import DataTable, {updateColumnsWidth} from '@gravity-ui/react-data-table';
35
import {Skeleton} from '@gravity-ui/uikit';
@@ -13,6 +15,7 @@ export interface ResizeableDataTableProps<T> extends Omit<DataTableProps<T>, 'th
1315
columnsWidthLSKey?: string;
1416
wrapperClassName?: string;
1517
loading?: boolean;
18+
onSortChange?: (params: any) => void;
1619
}
1720

1821
export function ResizeableDataTable<T>({
@@ -21,10 +24,20 @@ export function ResizeableDataTable<T>({
2124
settings,
2225
wrapperClassName,
2326
loading,
27+
onSort,
28+
onSortChange,
2429
...props
2530
}: ResizeableDataTableProps<T>) {
2631
const [tableColumnsWidth, setTableColumnsWidth] = useTableResize(columnsWidthLSKey);
2732

33+
const handleSort = React.useCallback(
34+
(params: any) => {
35+
onSort?.(params); // Original onSort if provided
36+
onSortChange?.(params); // Expose sort params to parent
37+
},
38+
[onSort, onSortChange],
39+
);
40+
2841
// If loading is true, override the render method of each column to display a Skeleton
2942
const processedColumns = loading
3043
? columns.map((column: Column<T>) => ({
@@ -46,6 +59,7 @@ export function ResizeableDataTable<T>({
4659
theme="yandex-cloud"
4760
columns={updatedColumns}
4861
onResize={setTableColumnsWidth}
62+
onSort={handleSort}
4963
settings={newSettings}
5064
{...props}
5165
/>

src/components/TableWithControlsLayout/TableWithControlsLayout.tsx

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import './TableWithControlsLayout.scss';
1212
const b = cn('ydb-table-with-controls-layout');
1313

1414
interface TableWithControlsLayoutItemProps {
15-
children?: React.ReactNode;
15+
children: React.ReactNode;
1616
renderExtraControls?: () => React.ReactNode;
1717
className?: string;
1818
fullHeight?: boolean;
@@ -22,10 +22,7 @@ export interface TableWrapperProps extends Omit<TableWithControlsLayoutItemProps
2222
loading?: boolean;
2323
scrollContainerRef?: React.RefObject<HTMLElement>;
2424
scrollDependencies?: any[];
25-
26-
// onSort is called with the table's sort parameters and triggers auto-scrolling.
27-
onSort?: (params: any) => void;
28-
children?: React.ReactNode | ((props: {onSort: (params: any) => void}) => React.ReactNode);
25+
children: React.ReactNode;
2926
}
3027

3128
export const TableWithControlsLayout = ({
@@ -60,34 +57,24 @@ TableWithControlsLayout.Table = function Table({
6057
className,
6158
scrollContainerRef,
6259
scrollDependencies = [],
63-
onSort,
6460
}: TableWrapperProps) {
6561
// Create an internal ref for the table container
6662
const tableContainerRef = React.useRef<HTMLDivElement>(null);
6763

6864
// Use the internal ref for scrolling
69-
const {handleTableScroll} = useTableScroll({
65+
useTableScroll({
7066
tableContainerRef,
7167
scrollContainerRef,
7268
dependencies: scrollDependencies,
7369
});
7470

75-
// Create a wrapper function that triggers scroll on sort
76-
const handleSort = React.useCallback(
77-
(params: any) => {
78-
onSort?.(params); // Call original callback if provided
79-
handleTableScroll(); // Trigger scroll to top
80-
},
81-
[onSort, handleTableScroll],
82-
);
83-
8471
if (loading) {
8572
return <TableSkeleton className={b('loader')} />;
8673
}
8774

8875
return (
8976
<div ref={tableContainerRef} className={b('table', className)}>
90-
{typeof children === 'function' ? children({onSort: handleSort}) : children}
77+
{children}
9178
</div>
9279
);
9380
};

src/containers/Tablets/TabletsTable.tsx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ export function TabletsTable({
183183
tabletsSearch: StringParam,
184184
});
185185

186+
// Track sort state for scroll dependencies
187+
const [sortParams, setSortParams] = React.useState<any>();
188+
186189
const columns = React.useMemo(() => getColumns({database}), [database]);
187190

188191
const filteredTablets = React.useMemo(() => {
@@ -214,18 +217,16 @@ export function TabletsTable({
214217
{error ? <ResponseError error={error} /> : null}
215218
<TableWithControlsLayout.Table
216219
scrollContainerRef={scrollContainerRef}
217-
scrollDependencies={[tabletsSearch]}
220+
scrollDependencies={[tabletsSearch, sortParams]}
218221
loading={loading}
219222
>
220-
{({onSort}) => (
221-
<ResizeableDataTable
222-
columns={columns}
223-
data={filteredTablets}
224-
settings={DEFAULT_TABLE_SETTINGS}
225-
emptyDataMessage={i18n('noTabletsData')}
226-
onSort={onSort}
227-
/>
228-
)}
223+
<ResizeableDataTable
224+
columns={columns}
225+
data={filteredTablets}
226+
settings={DEFAULT_TABLE_SETTINGS}
227+
emptyDataMessage={i18n('noTabletsData')}
228+
onSortChange={setSortParams}
229+
/>
229230
</TableWithControlsLayout.Table>
230231
</TableWithControlsLayout>
231232
);

src/containers/Tenants/Tenants.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ export const Tenants = ({additionalTenantsProps, scrollContainerRef}: TenantsPro
7070
);
7171
const loading = isFetching && currentData === undefined;
7272

73+
// Track sort state for scroll dependencies
74+
const [sortParams, setSortParams] = React.useState<any>();
75+
7376
const isCreateDBAvailable =
7477
useCreateDatabaseFeatureAvailable() && uiFactory.onCreateDB !== undefined;
7578
const isEditDBAvailable = useEditDatabaseFeatureAvailable() && uiFactory.onEditDB !== undefined;
@@ -128,7 +131,7 @@ export const Tenants = ({additionalTenantsProps, scrollContainerRef}: TenantsPro
128131
);
129132
};
130133

131-
const renderTable = (onSort?: (params: any) => void) => {
134+
const renderTable = () => {
132135
const columns: Column<PreparedTenant>[] = [
133136
{
134137
name: 'Name',
@@ -261,7 +264,7 @@ export const Tenants = ({additionalTenantsProps, scrollContainerRef}: TenantsPro
261264
columns={columns}
262265
settings={DEFAULT_TABLE_SETTINGS}
263266
emptyDataMessage="No such tenants"
264-
onSort={onSort}
267+
onSortChange={setSortParams}
265268
/>
266269
);
267270
};
@@ -276,9 +279,9 @@ export const Tenants = ({additionalTenantsProps, scrollContainerRef}: TenantsPro
276279
<TableWithControlsLayout.Table
277280
scrollContainerRef={scrollContainerRef}
278281
loading={loading}
279-
scrollDependencies={[searchValue, problemFilter]}
282+
scrollDependencies={[searchValue, problemFilter, sortParams]}
280283
>
281-
{({onSort}) => (currentData ? renderTable(onSort) : null)}
284+
{currentData ? renderTable() : null}
282285
</TableWithControlsLayout.Table>
283286
</TableWithControlsLayout>
284287
</div>

0 commit comments

Comments
 (0)