Skip to content

Commit 95d8093

Browse files
committed
fix: review fixes
1 parent 14446a3 commit 95d8093

File tree

2 files changed

+19
-81
lines changed

2 files changed

+19
-81
lines changed

src/components/QueryResultTable/Cell/Cell.tsx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,34 @@ import {b} from '../QueryResultTable';
77
interface CellProps {
88
className?: string;
99
value: string;
10-
isActive: boolean;
11-
onToggle: () => void;
1210
}
1311

1412
export const Cell = React.memo(function Cell(props: CellProps) {
15-
const {className, value, isActive, onToggle} = props;
13+
const {className, value} = props;
1614

15+
const [open, setOpen] = React.useState(false);
1716
const anchorRef = React.useRef<HTMLSpanElement | null>(null);
1817

18+
const handleToggle = React.useCallback(() => {
19+
setOpen((prevOpen) => !prevOpen);
20+
}, []);
21+
22+
const handleClose = React.useCallback(() => {
23+
setOpen(false);
24+
}, []);
25+
1926
return (
2027
<React.Fragment>
2128
<Popup
22-
open={isActive}
29+
open={open}
2330
hasArrow
2431
placement={['top', 'bottom']}
2532
anchorRef={anchorRef}
26-
onOutsideClick={onToggle}
33+
onOutsideClick={handleClose}
2734
>
2835
<div className={b('cell-popup')}>{value}</div>
2936
</Popup>
30-
<span ref={anchorRef} className={b('cell', className)} onClick={onToggle}>
37+
<span ref={anchorRef} className={b('cell', className)} onClick={handleToggle}>
3138
{value}
3239
</span>
3340
</React.Fragment>

src/components/QueryResultTable/QueryResultTable.tsx

Lines changed: 6 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -28,67 +28,7 @@ export const b = cn('ydb-query-result-table');
2828

2929
const WIDTH_PREDICTION_ROWS_COUNT = 100;
3030

31-
type ActiveCellState = {
32-
row: KeyValueRow;
33-
columnName: string;
34-
} | null;
35-
36-
type ActiveCellAction = {
37-
type: 'toggle';
38-
payload: {
39-
row: KeyValueRow;
40-
columnName: string;
41-
};
42-
};
43-
44-
function activeCellReducer(state: ActiveCellState, action: ActiveCellAction): ActiveCellState {
45-
switch (action.type) {
46-
case 'toggle':
47-
if (
48-
state &&
49-
state.row === action.payload.row &&
50-
state.columnName === action.payload.columnName
51-
) {
52-
return null;
53-
}
54-
return action.payload;
55-
default:
56-
return state;
57-
}
58-
}
59-
60-
type RenderCellArgs = {row: KeyValueRow; columnName: string};
61-
type RenderCell = (args: RenderCellArgs) => React.ReactNode;
62-
63-
interface CreateRenderCellParams {
64-
activeCell: ActiveCellState;
65-
dispatch: React.Dispatch<ActiveCellAction>;
66-
}
67-
68-
function createRenderCell({activeCell, dispatch}: CreateRenderCellParams): RenderCell {
69-
return ({row, columnName}: RenderCellArgs) => {
70-
const isActive = Boolean(
71-
activeCell && activeCell.row === row && activeCell.columnName === columnName,
72-
);
73-
74-
const value = row[columnName];
75-
76-
const onToggle = () => {
77-
dispatch({
78-
type: 'toggle',
79-
payload: {row, columnName},
80-
});
81-
};
82-
83-
return <Cell value={String(value)} isActive={isActive} onToggle={onToggle} />;
84-
};
85-
}
86-
87-
const prepareTypedColumns = (
88-
columns: ColumnType[],
89-
data: KeyValueRow[] | undefined,
90-
renderCell: RenderCell,
91-
) => {
31+
const prepareTypedColumns = (columns: ColumnType[], data: KeyValueRow[] | undefined) => {
9232
if (!columns.length) {
9333
return [];
9434
}
@@ -102,14 +42,14 @@ const prepareTypedColumns = (
10242
name,
10343
width: getColumnWidth({data: dataSlice, name}),
10444
align: columnType === 'number' ? DataTable.RIGHT : DataTable.LEFT,
105-
render: ({row}) => renderCell({row, columnName: name}),
45+
render: ({row}) => <Cell value={String(row[name])} />,
10646
};
10747

10848
return column;
10949
});
11050
};
11151

112-
const prepareGenericColumns = (data: KeyValueRow[] | undefined, renderCell: RenderCell) => {
52+
const prepareGenericColumns = (data: KeyValueRow[] | undefined) => {
11353
if (!data?.length) {
11454
return [];
11555
}
@@ -121,7 +61,7 @@ const prepareGenericColumns = (data: KeyValueRow[] | undefined, renderCell: Rend
12161
name,
12262
width: getColumnWidth({data: dataSlice, name}),
12363
align: isNumeric(data[0][name]) ? DataTable.RIGHT : DataTable.LEFT,
124-
render: ({row}) => renderCell({row, columnName: name}),
64+
render: ({row}) => <Cell value={String(row[name])} />,
12565
};
12666

12767
return column;
@@ -143,18 +83,9 @@ interface QueryResultTableProps
14383
export const QueryResultTable = (props: QueryResultTableProps) => {
14484
const {columns, data, settings: propsSettings} = props;
14585

146-
const [activeCell, dispatch] = React.useReducer(activeCellReducer, null);
147-
148-
const renderCell = React.useMemo(
149-
() => createRenderCell({activeCell, dispatch}),
150-
[activeCell, dispatch],
151-
);
152-
15386
const preparedColumns = React.useMemo(() => {
154-
return columns
155-
? prepareTypedColumns(columns, data, renderCell)
156-
: prepareGenericColumns(data, renderCell);
157-
}, [columns, data, renderCell]);
87+
return columns ? prepareTypedColumns(columns, data) : prepareGenericColumns(data);
88+
}, [columns, data]);
15889

15990
const settings = React.useMemo(() => {
16091
return {

0 commit comments

Comments
 (0)