Skip to content

Commit e22314a

Browse files
committed
fix(frontend): tablesearch
1 parent 7cbdcdd commit e22314a

File tree

2 files changed

+34
-32
lines changed

2 files changed

+34
-32
lines changed

frontend/src/components/sidebar/sidebar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ export const Sidebar: FC = () => {
231231
onError(error) {
232232
notify(`Error signing you in: ${error.message}`, "error")
233233
},
234-
})
234+
});
235235
}, [dispatch, login, navigate, profiles]);
236236

237237
const handleNavigateToLogin = useCallback(() => {

frontend/src/components/table.tsx

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { useExportToCSV, useLongPress } from "./hooks";
1212
import { Icons } from "./icons";
1313
import { SearchInput } from "./search";
1414
import { Loading } from "./loading";
15-
import { clone } from "lodash";
15+
import { clone, values } from "lodash";
1616

1717
type IPaginationProps = {
1818
pageCount: number;
@@ -183,7 +183,7 @@ const TData: FC<ITDataProps> = ({ cell, onCellUpdate, disableEdit }) => {
183183
"bg-gray-200 dark:bg-white/10 blur-[2px]": editable || preview,
184184
})}
185185
>
186-
<span className="hidden">{editedData}</span>
186+
<span className="cell-data hidden">{editedData}</span>
187187
<div
188188
className={classNames("w-full h-full p-2 leading-tight focus:outline-none focus:shadow-outline appearance-none transition-all duration-300 border-solid border-gray-200 dark:border-white/5 overflow-hidden whitespace-nowrap select-none text-gray-600 dark:text-neutral-300", {
189189
"group-even/row:bg-gray-100 hover:bg-gray-300 group-even/row:hover:bg-gray-300 dark:group-even/row:bg-white/10 dark:group-odd/row:bg-white/5 dark:group-even/row:hover:bg-white/15 dark:group-odd/row:hover:bg-white/15": !editable,
@@ -305,7 +305,7 @@ const TableRow: FC<ITableRow> = ({ row, style, onRowUpdate, disableEdit }) => {
305305
<div className="table-row-group text-xs group/row" {...props} key={props.key}>
306306
{
307307
row.cells.map((cell) => (
308-
<TData key={cell.getCellProps().key} cell={cell} onCellUpdate={handleCellUpdate} disableEdit={disableEdit} />
308+
<TData key={cell.getCellProps().key} cell={cell} onCellUpdate={handleCellUpdate} disableEdit={disableEdit || cell.column.id === "#"} />
309309
))
310310
}
311311
</div>
@@ -325,6 +325,7 @@ type ITableProps = {
325325
}
326326

327327
export const Table: FC<ITableProps> = ({ className, columns: actualColumns, rows: actualRows, columnTags, totalPages, currentPage, onPageChange, onRowUpdate, disableEdit }) => {
328+
const fixedTableRef = useRef<FixedSizeList>(null);
328329
const containerRef = useRef<HTMLDivElement>(null);
329330
const operationsRef = useRef<HTMLDivElement>(null);
330331
const tableRef = useRef<HTMLTableElement>(null);
@@ -413,46 +414,46 @@ export const Table: FC<ITableProps> = ({ className, columns: actualColumns, rows
413414
}
414415
let interval: NodeJS.Timeout;
415416
if (e.key === "Enter") {
416-
let newSearchIndex = (searchIndex + 1) % rowCount;
417-
setSearchIndex(newSearchIndex);
418417
const searchText = search.toLowerCase();
419-
let index = 0;
420-
const tbody = tableRef.current.querySelector(".tbody");
421-
if (tbody == null) {
422-
return;
423-
}
424-
for (const childNode of tbody.childNodes) {
425-
if (childNode instanceof HTMLTableRowElement) {
426-
const text = childNode.textContent?.toLowerCase();
418+
const filteredToOriginalIndex = [];
419+
for (const [index, row] of rows.entries()) {
420+
for (const value of values(row.values)) {
421+
const text = value.toLowerCase();
427422
if (text != null && searchText != null && text.includes(searchText)) {
428-
if (index === newSearchIndex) {
429-
childNode.scrollIntoView({
430-
behavior: "smooth",
431-
block: "center",
432-
inline: "center",
433-
});
434-
for (const cell of childNode.querySelectorAll("input")) {
435-
if (cell instanceof HTMLInputElement) {
436-
cell.classList.add("!bg-yellow-100");
437-
interval = setTimeout(() => {
438-
cell.classList.remove("!bg-yellow-100");
439-
}, 3000);
440-
}
423+
filteredToOriginalIndex.push(index);
424+
}
425+
}
426+
}
427+
428+
if (rows.length > 0 && filteredToOriginalIndex.length > 0) {
429+
const newSearchIndex = (searchIndex + 1) % filteredToOriginalIndex.length;
430+
setSearchIndex(newSearchIndex);
431+
const originalIndex = filteredToOriginalIndex[newSearchIndex] + 1;
432+
fixedTableRef.current?.scrollToItem(originalIndex, "center");
433+
setTimeout(() => {
434+
const currentVisibleRows = tableRef.current?.querySelectorAll(".table-row-group") ?? [];
435+
for (const currentVisibleRow of currentVisibleRows) {
436+
const text = currentVisibleRow.querySelector("div > span")?.textContent ?? "";
437+
if (isNumeric(text)) {
438+
const id = parseInt(text);
439+
if (id === originalIndex) {
440+
currentVisibleRow.classList.add("!bg-yellow-100", "dark:!bg-yellow-800");
441+
interval = setTimeout(() => {
442+
currentVisibleRow.classList.remove("!bg-yellow-100", "dark:!bg-yellow-800");
443+
}, 3000);
441444
}
442-
return;
443445
}
444-
index++;
445446
}
446-
}
447-
};
447+
}, 100);
448+
}
448449
}
449450

450451
return () => {
451452
if (interval != null) {
452453
clearInterval(interval);
453454
}
454455
}
455-
}, [search, rowCount, searchIndex]);
456+
}, [rows, search, searchIndex]);
456457

457458
const handleSearchChange = useCallback((newValue: string) => {
458459
setSearchIndex(-1);
@@ -541,6 +542,7 @@ export const Table: FC<ITableProps> = ({ className, columns: actualColumns, rows
541542
</div>
542543
<div className="tbody" {...getTableBodyProps()}>
543544
<FixedSizeList
545+
ref={fixedTableRef}
544546
height={height}
545547
itemCount={sortedRows.length}
546548
itemSize={31}

0 commit comments

Comments
 (0)