Skip to content

Commit

Permalink
add improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ivy-edp committed Dec 10, 2024
1 parent 6edfa73 commit 454eb50
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 28 deletions.
49 changes: 22 additions & 27 deletions packages/components/src/components/common/table/hooks/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export const useMultiSelectRow = <TData,>(table: Table<TData>) => {
interface KeyHandlerOptions<TData> {
multiSelect?: boolean;
reorder?: { updateOrder?: (moveIndexes: number[], toIndex: number) => void; getRowId?: (row: TData) => string };
lazyLoadChildren?: (row: Row<TData>) => void;
}

interface TableKeyboardHandlerProps<TData> {
Expand All @@ -131,7 +132,7 @@ interface TableKeyboardHandlerProps<TData> {
options?: KeyHandlerOptions<TData>;
}

export const useTableKeyHandler = <TData,>(props: TableKeyboardHandlerProps<TData>) => {
export const useTableKeyHandler = <TData,>({ table, data, options }: TableKeyboardHandlerProps<TData>) => {
const [rootIndex, setRootIndex] = React.useState<number | undefined>(undefined);

const handleKeyDown = (event: React.KeyboardEvent<HTMLTableElement>, onEnterAction?: (row: Row<TData>) => void) => {
Expand All @@ -140,34 +141,27 @@ export const useTableKeyHandler = <TData,>(props: TableKeyboardHandlerProps<TDat
switch (event.key) {
case 'ArrowUp':
case 'ArrowDown': {
onArrowKeyDownUpTableHandler({ ...props, event });
handleArrowKeyUpDown(event);
break;
}
case 'ArrowRight':
toggleExpand(props.table.getSelectedRowModel().flatRows, true);
toggleExpand(table.getSelectedRowModel().flatRows, true, options?.lazyLoadChildren);
break;
case 'ArrowLeft':
toggleExpand(props.table.getSelectedRowModel().flatRows, false);
toggleExpand(table.getSelectedRowModel().flatRows, false, options?.lazyLoadChildren);
break;
case 'Tab':
props.table.resetRowSelection();
table.resetRowSelection();
break;
case 'Enter':
if (props.table.getSelectedRowModel().flatRows.length === 1 && onEnterAction) {
onEnterAction(props.table.getSelectedRowModel().flatRows[0]);
}
handleEnterKey(table.getSelectedRowModel().flatRows, onEnterAction);
break;
default:
break;
}
};

const onArrowKeyDownUpTableHandler = <TData,>({
table,
data,
event,
options
}: TableKeyboardHandlerProps<TData> & { event: React.KeyboardEvent<HTMLTableElement> }) => {
const handleArrowKeyUpDown = (event: React.KeyboardEvent<HTMLTableElement>) => {
const { multiSelect = false, reorder } = options || {};
const allRows = table.getRowModel().rows;
const selectedRows = table.getSelectedRowModel().flatRows;
Expand All @@ -188,15 +182,15 @@ export const useTableKeyHandler = <TData,>(props: TableKeyboardHandlerProps<TDat
resetAndSetRowSelection(table, data, moveIds, reorder.getRowId);
setRootIndex(newReorderIndex);
} else if (multiSelect && event.shiftKey) {
toggleShiftRow(direction, newSelectIndex, allRows, selectedRows);
toggleRowWithShift(direction, newSelectIndex, allRows, selectedRows);
} else {
table.resetRowSelection();
allRows[newReorderIndex].toggleSelected();
setRootIndex(newReorderIndex);
}
};

const toggleShiftRow = <TData,>(
const toggleRowWithShift = <TData,>(
direction: -1 | 1,
newIndex: number | undefined,
allRows: Array<Row<TData>>,
Expand All @@ -209,26 +203,27 @@ export const useTableKeyHandler = <TData,>(props: TableKeyboardHandlerProps<TDat
setRootIndex(selectedRows[0].index);
}
if (direction === 1) {
if (rootIndex === allRows.indexOf(selectedRows[0]) || selectedRows.length === 1) {
allRows[newIndex].toggleSelected();
} else {
selectedRows[0].toggleSelected();
}
const shouldToggleFirst = rootIndex === allRows.indexOf(selectedRows[0]) || selectedRows.length === 1;
shouldToggleFirst ? allRows[newIndex].toggleSelected() : selectedRows[0].toggleSelected();

Check warning on line 207 in packages/components/src/components/common/table/hooks/hooks.tsx

View check run for this annotation

axonivy/jenkins.ivyteam.io / ESLint

@typescript-eslint/no-unused-expressions

ERROR: Expected an assignment or function call and instead saw an expression. (@typescript-eslint/no-unused-expressions)
} else {
if (rootIndex === allRows.indexOf(selectedRows[selectedRows.length - 1]) || selectedRows.length === 1) {
allRows[newIndex].toggleSelected();
} else {
selectedRows[selectedRows.length - 1].toggleSelected();
}
const shouldToggleLast = rootIndex === allRows.indexOf(selectedRows[selectedRows.length - 1]) || selectedRows.length === 1;
shouldToggleLast ? allRows[newIndex].toggleSelected() : selectedRows[selectedRows.length - 1].toggleSelected();

Check warning on line 210 in packages/components/src/components/common/table/hooks/hooks.tsx

View check run for this annotation

axonivy/jenkins.ivyteam.io / ESLint

@typescript-eslint/no-unused-expressions

ERROR: Expected an assignment or function call and instead saw an expression. (@typescript-eslint/no-unused-expressions)
}
};

return { handleKeyDown };
};

const toggleExpand = <TData,>(rows: Row<TData>[], expand: boolean) => {
const handleEnterKey = <TData,>(selectedRows: Array<Row<TData>>, onEnterAction?: (row: Row<TData>) => void) => {
if (selectedRows.length === 1 && onEnterAction) {
onEnterAction(selectedRows[0]);
}
};

const toggleExpand = <TData,>(rows: Row<TData>[], expand: boolean, loadChildren?: (row: Row<TData>) => void) => {
if (rows.length === 1) {
if (expand && !rows[0].getIsExpanded()) {
loadChildren && loadChildren(rows[0]);

Check warning on line 226 in packages/components/src/components/common/table/hooks/hooks.tsx

View check run for this annotation

axonivy/jenkins.ivyteam.io / ESLint

@typescript-eslint/no-unused-expressions

ERROR: Expected an assignment or function call and instead saw an expression. (@typescript-eslint/no-unused-expressions)
rows[0].toggleExpanded();
} else if (!expand && rows[0].getIsExpanded()) {
rows[0].toggleExpanded();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const useBrowser = (
...select.tableState
}
});
const { handleKeyDown } = useTableKeyHandler({ table, data });
const { handleKeyDown } = useTableKeyHandler({ table, data, options: { lazyLoadChildren: options?.loadChildren } });
return { table, globalFilter: { filter, setFilter }, handleKeyDown };
};

Expand Down

0 comments on commit 454eb50

Please sign in to comment.