2024-06-29 19:39:25 +00:00
|
|
|
import {
|
|
|
|
|
DataTablaRowActionFunction,
|
|
|
|
|
DataTableRowActions,
|
|
|
|
|
} from "@/components";
|
|
|
|
|
import { Checkbox } from "@/ui";
|
2024-09-02 15:44:49 +00:00
|
|
|
import { ColumnDef, Row } from "@tanstack/react-table";
|
2024-06-29 19:39:25 +00:00
|
|
|
|
|
|
|
|
import { useMemo } from "react";
|
2026-10-07 16:37:46 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2026-09-16 17:13:11 +00:00
|
|
|
import { QuoteItemsSortableDragHandleCell } from "../components/QuoteItemsSortableDragHandleCell";
|
2024-06-29 19:39:25 +00:00
|
|
|
|
2024-07-11 11:08:53 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
2024-08-12 11:26:10 +00:00
|
|
|
export function useDetailColumns<TData = unknown, TValue = unknown>(
|
2024-06-29 19:39:25 +00:00
|
|
|
columns: ColumnDef<TData>[],
|
|
|
|
|
options: {
|
|
|
|
|
enableDragHandleColumn?: boolean;
|
|
|
|
|
enableSelectionColumn?: boolean;
|
|
|
|
|
enableActionsColumn?: boolean;
|
2024-07-11 11:08:53 +00:00
|
|
|
rowActionFn?: DataTablaRowActionFunction<TData>;
|
2024-06-29 19:39:25 +00:00
|
|
|
} = {}
|
2024-08-12 11:26:10 +00:00
|
|
|
): ColumnDef<TData, TValue>[] {
|
2026-10-19 11:55:16 +00:00
|
|
|
const { t } = useTranslation();
|
2024-06-29 19:39:25 +00:00
|
|
|
const {
|
|
|
|
|
enableDragHandleColumn = false,
|
|
|
|
|
enableSelectionColumn = false,
|
|
|
|
|
enableActionsColumn = false,
|
|
|
|
|
rowActionFn = undefined,
|
|
|
|
|
} = options;
|
|
|
|
|
|
|
|
|
|
// const lastSelectedId = "";
|
|
|
|
|
|
|
|
|
|
return useMemo(() => {
|
2026-10-19 11:55:16 +00:00
|
|
|
const result = [...columns];
|
2024-08-25 12:14:21 +00:00
|
|
|
if (enableDragHandleColumn) {
|
2026-10-19 11:55:16 +00:00
|
|
|
result.unshift({
|
2024-08-25 12:14:21 +00:00
|
|
|
id: "row_drag_handle",
|
|
|
|
|
/*header: () => (
|
|
|
|
|
<UnfoldVertical aria-label='Mover fila' className='items-center justify-center w-4 h-4' />
|
|
|
|
|
),*/
|
|
|
|
|
header: () => null,
|
2026-09-16 17:13:11 +00:00
|
|
|
cell: () => <QuoteItemsSortableDragHandleCell />,
|
2024-08-25 12:14:21 +00:00
|
|
|
enableSorting: false,
|
|
|
|
|
enableHiding: false,
|
2024-08-25 20:06:24 +00:00
|
|
|
size: 40,
|
2024-08-25 12:14:21 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-29 19:39:25 +00:00
|
|
|
if (enableSelectionColumn) {
|
2026-10-19 11:55:16 +00:00
|
|
|
result.unshift({
|
2024-06-29 19:39:25 +00:00
|
|
|
id: "select",
|
2024-07-11 16:40:46 +00:00
|
|
|
header: ({ table }) => (
|
2024-06-29 19:39:25 +00:00
|
|
|
<Checkbox
|
|
|
|
|
id='select-all'
|
|
|
|
|
checked={
|
|
|
|
|
table.getIsAllPageRowsSelected() ||
|
|
|
|
|
(table.getIsSomePageRowsSelected() && "indeterminate")
|
|
|
|
|
}
|
|
|
|
|
onCheckedChange={(value) => table.toggleAllPageRowsSelected(!!value)}
|
2026-10-05 11:58:03 +00:00
|
|
|
aria-label={t("common.select_all")}
|
2024-07-10 18:54:33 +00:00
|
|
|
className='translate-y-[0px]'
|
2024-06-29 19:39:25 +00:00
|
|
|
/>
|
2024-07-11 16:40:46 +00:00
|
|
|
),
|
|
|
|
|
|
2024-09-02 15:44:49 +00:00
|
|
|
cell: ({ row }: { row: Row<TData> }) => (
|
2024-06-29 19:39:25 +00:00
|
|
|
<Checkbox
|
|
|
|
|
id={`select-row-${row.id}`}
|
|
|
|
|
checked={row.getIsSelected()}
|
|
|
|
|
disabled={!row.getCanSelect()}
|
2026-10-19 11:55:16 +00:00
|
|
|
onCheckedChange={(value) => row.toggleSelected(Boolean(value))}
|
2026-10-05 11:58:03 +00:00
|
|
|
aria-label={t("common.select_row")}
|
2024-07-10 18:54:33 +00:00
|
|
|
className='mt-2'
|
2024-06-29 19:39:25 +00:00
|
|
|
/>
|
|
|
|
|
),
|
|
|
|
|
enableSorting: false,
|
|
|
|
|
enableHiding: false,
|
2024-08-25 20:06:24 +00:00
|
|
|
size: 40,
|
2024-06-29 19:39:25 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (enableActionsColumn) {
|
2026-10-19 11:55:16 +00:00
|
|
|
result.push({
|
2024-06-29 19:39:25 +00:00
|
|
|
id: "row_actions",
|
2024-07-11 11:08:53 +00:00
|
|
|
/*header: () => (
|
2024-06-29 19:39:25 +00:00
|
|
|
<MoreHorizontalIcon
|
|
|
|
|
aria-label='Acciones'
|
|
|
|
|
className='items-center justify-center w-4 h-4'
|
|
|
|
|
/>
|
2024-07-11 11:08:53 +00:00
|
|
|
),*/
|
2024-06-29 19:39:25 +00:00
|
|
|
cell: (props) => {
|
2024-08-25 20:06:24 +00:00
|
|
|
return <DataTableRowActions rowContext={props} actions={rowActionFn} />;
|
2024-06-29 19:39:25 +00:00
|
|
|
},
|
|
|
|
|
enableSorting: false,
|
|
|
|
|
enableHiding: false,
|
2024-08-25 20:06:24 +00:00
|
|
|
size: 48,
|
2024-06-29 19:39:25 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-19 11:55:16 +00:00
|
|
|
return result;
|
|
|
|
|
}, [columns, enableActionsColumn, enableDragHandleColumn, enableSelectionColumn, rowActionFn, t]);
|
2024-06-29 19:39:25 +00:00
|
|
|
}
|