noogle/components/basicList/basicList.tsx

122 lines
3.1 KiB
TypeScript
Raw Normal View History

import React, { useState, useMemo, useEffect } from "react";
2022-11-26 12:36:08 +03:00
import {
Box,
IconButton,
List,
ListItem,
Pagination,
Stack,
Typography,
Grid,
2022-12-03 20:20:07 +03:00
Slide,
Collapse,
Grow,
2022-11-26 12:36:08 +03:00
} from "@mui/material";
2022-12-03 20:20:07 +03:00
import { BasicDataItem, BasicDataViewProps } from "../../types/basicDataView";
2022-11-26 12:36:08 +03:00
import { SearchInput } from "../searchInput";
import Radio from "@mui/material/Radio";
import RadioGroup from "@mui/material/RadioGroup";
import FormControlLabel from "@mui/material/FormControlLabel";
import FormControl from "@mui/material/FormControl";
import FormLabel from "@mui/material/FormLabel";
import ClearIcon from "@mui/icons-material/Clear";
2022-12-11 23:22:45 +03:00
2022-11-27 16:40:17 +03:00
import { NixType, nixTypes } from "../../types/nix";
2022-12-18 19:58:53 +03:00
import { Filter } from "../searchInput/searchInput";
import { useRouter } from "next/router";
2022-11-26 12:36:08 +03:00
export type BasicListItem = {
item: React.ReactNode;
key: string;
};
export type BasicListProps = BasicDataViewProps & {
2022-12-18 19:58:53 +03:00
handleFilter: (filter: Filter | ((curr: Filter) => Filter)) => void;
2022-12-03 19:48:36 +03:00
selected?: string | null;
2022-12-09 18:51:33 +03:00
itemsPerPage: number;
2022-11-26 12:36:08 +03:00
};
export function BasicList(props: BasicListProps) {
2022-12-03 19:38:50 +03:00
const {
items,
pageCount = 1,
2022-12-09 18:41:59 +03:00
itemsPerPage,
2022-12-03 19:38:50 +03:00
handleSearch,
handleFilter,
selected = "",
} = props;
2022-11-26 12:36:08 +03:00
// const [from, setFrom] = useState<NixType>("any");
// const [to, setTo] = useState<NixType>("any");
const [page, setPage] = useState<number>(1);
const router = useRouter();
useEffect(() => {
const { query } = router;
if (query?.page) {
if (typeof query.page === "string") {
const page = Number(query.page);
setPage(page);
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [router]);
2022-11-26 12:36:08 +03:00
2022-12-09 18:41:59 +03:00
const pageItems = useMemo(() => {
const startIdx = (page - 1) * itemsPerPage;
const endIdx = page * itemsPerPage;
return items.slice(startIdx, endIdx);
}, [page, items, itemsPerPage]);
2022-11-26 12:36:08 +03:00
const [searchTerm, setSearchTerm] = useState("");
const handlePageChange = (
_event: React.ChangeEvent<unknown>,
value: number
) => {
setPage(value);
};
2022-12-18 19:58:53 +03:00
const _handleFilter = (filter: Filter | ((curr: Filter) => Filter)) => {
2022-12-11 23:22:45 +03:00
handleFilter(filter);
2022-11-26 12:36:08 +03:00
setPage(1);
};
const _handleSearch = (term: string) => {
handleSearch && handleSearch(term);
setSearchTerm(term);
setPage(1);
};
2022-11-27 16:16:52 +03:00
2022-11-26 12:36:08 +03:00
return (
<Stack>
<SearchInput
2022-12-11 23:22:45 +03:00
handleFilter={_handleFilter}
2022-11-26 12:36:08 +03:00
placeholder="search nix functions"
handleSearch={_handleSearch}
page={page}
2022-11-26 12:36:08 +03:00
clearSearch={() => _handleSearch("")}
/>
2022-12-11 23:22:45 +03:00
2022-11-26 12:36:08 +03:00
<List aria-label="basic-list" sx={{ pt: 0 }}>
2022-12-09 18:41:59 +03:00
{pageItems.map(({ item, key }, idx) => (
2022-12-08 01:10:58 +03:00
<Box key={`${key}-${idx}`}>
2022-12-09 18:41:59 +03:00
<ListItem sx={{ px: 0 }} key={key} aria-label={`item-${key}`}>
2022-12-03 19:38:50 +03:00
{item}
</ListItem>
2022-12-03 20:20:07 +03:00
</Box>
2022-11-26 12:36:08 +03:00
))}
</List>
<Pagination
hideNextButton
hidePrevButton
2022-11-26 12:36:08 +03:00
sx={{ display: "flex", justifyContent: "center", mt: 1, mb: 10 }}
2022-12-09 18:41:59 +03:00
count={Math.ceil(items.length / itemsPerPage) || 1}
2022-11-26 12:36:08 +03:00
color="primary"
page={page}
onChange={handlePageChange}
/>
</Stack>
);
}