import React, { useMemo, useEffect } from "react";
import { TouchableWithoutFeedback } from "react-native";
import { usePagination, useTable } from "react-table";
import { default as useMazloPagination } from "../../hooks/usePagination";
import { Checkbox } from "../Checkbox";
import { Image } from "../Image";
import { Pagination } from "../Pagination";
import { Text } from "../Text";
import { View } from "../View";
import styles from "./styles";
type Image = {
id: string;
source: {
uri: string;
};
title: string;
};
type ImageItemProps = {
image: Image;
marginHorizontal: number;
marginVertical: number;
selected: boolean;
size: number;
onSelect: () => void;
};
const ImageItem = ({
image: {
source: { uri },
title,
},
marginHorizontal,
marginVertical,
selected,
size,
onSelect,
}: ImageItemProps) => {
return (
{title}
);
};
type Props = {
count?: number;
data: Image[];
total?: number;
selectedPageIndex?: number;
imageSize?: number;
selectedImages: { [id: string]: any };
onAdd?: () => void;
onSelect: (image: Image) => void;
onPageChange?: (pageIndex: number) => void;
};
const ImageGrid = ({
count,
selectedPageIndex = 0,
data,
imageSize = 200,
selectedImages,
total = data ? data.length : count,
onPageChange,
onSelect,
}: Props) => {
const columns = useMemo(() => [{ accessor: "image" }], []);
const marginHorizontal = 0.11 * imageSize;
const marginVertical = 0.06 * imageSize;
const pageSize = count || data.length || 1;
const { onPageChange: onMazloPagination } = useMazloPagination();
const {
prepareRow,
page,
pageCount,
gotoPage,
state: { pageIndex },
} = useTable(
{
columns,
data,
manualPagination: true,
pageCount: Math.ceil(total / count),
initialState: { pageIndex: selectedPageIndex, pageSize },
},
usePagination
);
useEffect(() => {
if (onPageChange) {
onPageChange(pageIndex);
} else {
onMazloPagination(pageIndex);
}
}, [pageIndex]);
return (
{page.map((row) => {
prepareRow(row);
const image = row.original;
return (
onSelect(image)}
/>
);
})}
{
gotoPage(i);
}}
/>
);
};
export default ImageGrid;