import { useMemo } from "react" import { Controller } from "react-hook-form" import { Column, useTable } from "react-table" import { useTranslation } from "react-i18next" import { FormImage } from "../../../types/shared" import { NestedForm } from "../../../utils/nested-form" import Button from "../../fundamentals/button" import TrashIcon from "../../fundamentals/icons/trash-icon" import IconTooltip from "../../molecules/icon-tooltip" import Table from "../../molecules/table" import RadioGroup from "../../organisms/radio-group" export type ImageTableDataType = { id?: string } & FormImage type ImageTableProps = { data: ImageTableDataType[] form: NestedForm onDelete: (index: number) => void } const ImageTable = ({ data, form, onDelete }: ImageTableProps) => { const { t } = useTranslation() const { control, register, path } = form const columns = useMemo< Column<{ id?: string | undefined } & FormImage>[] >(() => { return [ { Header: () => (
Image
), maxWidth: 140, width: 140, collapse: true, accessor: "url", Cell: ({ cell: { value } }) => { return (
) }, }, { Header: () => {t("image-table-file-name", "File name")}, accessor: "nativeFile", Cell: ({ cell }) => { return ( { return (

{value?.name}

{value?.size && ( {(value.size / 1024).toFixed(2)} KB )}
) }} /> ) }, }, { Header: () => (
{t("image-table-thumbnail", "Thumbnail")}
), id: "thumbnail", width: 120, collapse: true, Cell: ({ cell }) => { return (
) }, }, { Header: () => null, id: "delete", width: 40, Cell: ({ row }) => { return ( ) }, }, ] }, []) const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow } = useTable({ columns, data, defaultColumn: { width: "auto", }, }) return ( {headerGroups?.map((headerGroup) => ( {headerGroup.headers.map((col, index) => { return ( {col.render("Header", { index })} ) })} ))} {rows.map((row, index) => { prepareRow(row) return ( {row.cells.map((cell) => { return ( {cell.render("Cell")} ) })} ) })}
) } export default ImageTable