-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
124aab3
commit b7d6532
Showing
5 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
// Auto-generated file. Do not modify manually. | ||
export * from "./login"; | ||
export * from "./list"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./list"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import * as React from "react"; | ||
import { useEffect, useState } from "react"; | ||
|
||
import { P } from "../../components"; | ||
import { AttributeData } from "../../lib/data/attributedata"; | ||
import { List } from "./list"; | ||
|
||
const meta = { | ||
title: "Templates/List", | ||
component: List, | ||
argTypes: { onSubmit: { action: "onSubmit" } }, | ||
} satisfies Meta<typeof List>; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof meta>; | ||
|
||
export const ListTemplate: Story = { | ||
args: { | ||
children: <P>The quick brown fox jumps over the lazy dog.</P>, | ||
pageSize: 50, | ||
results: [], | ||
showPaginator: true, | ||
sort: true, | ||
title: "Lijstweergave", | ||
}, | ||
render: (args) => { | ||
const [loading, setLoading] = useState(false); | ||
const [page, setPage] = useState<number>(args.paginatorProps?.page || 1); | ||
const [pageSize, setPageSize] = useState<number>(args.pageSize || 10); | ||
const [results, setResults] = useState<AttributeData[]>([]); | ||
const [sort, setSort] = useState<string>(""); | ||
|
||
/** | ||
* Fetches data from jsonplaceholder.typicode.com. | ||
*/ | ||
useEffect(() => { | ||
setLoading(true); | ||
const abortController = new AbortController(); | ||
const sortKey = sort.replace(/^-/, ""); | ||
const sortDirection = sort.startsWith("-") ? "desc" : "asc"; | ||
|
||
// Process sorting and pagination locally in place for demonstration purposes. | ||
fetch( | ||
`https://jsonplaceholder.typicode.com/posts?_page=${page}&_limit=${pageSize}&_sort=${sortKey}&_order=${sortDirection}`, | ||
{ | ||
signal: abortController.signal, | ||
}, | ||
) | ||
.then((response) => response.json()) | ||
.then((data: AttributeData[]) => { | ||
setResults(data); | ||
setLoading(false); | ||
}); | ||
|
||
return () => { | ||
abortController.abort(); | ||
setLoading(false); | ||
}; | ||
}, [page, pageSize, sort]); | ||
|
||
return ( | ||
<List | ||
{...args} | ||
count={100} | ||
results={results} | ||
onSort={(field) => setSort(field)} | ||
loading={loading} | ||
page={page} | ||
pageSize={pageSize} | ||
pageSizeOptions={[ | ||
{ label: 10 }, | ||
{ label: 20 }, | ||
{ label: 30 }, | ||
{ label: 40 }, | ||
{ label: 50 }, | ||
]} | ||
onPageChange={setPage} | ||
onPageSizeChange={setPageSize} | ||
/> | ||
); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import React, { useContext } from "react"; | ||
|
||
import { | ||
Body, | ||
Card, | ||
Column, | ||
DataGrid, | ||
DataGridProps, | ||
Grid, | ||
Logo, | ||
Page, | ||
} from "../../components"; | ||
import { ConfigContext, NavigationContext } from "../../contexts"; | ||
|
||
export type ListProps = DataGridProps & | ||
React.PropsWithChildren<{ | ||
/** Logo (JSX) slot. */ | ||
slotLogo?: React.ReactNode; | ||
|
||
/** Primary navigation (JSX) slot. */ | ||
slotPrimaryNavigation?: React.ReactNode; | ||
}>; | ||
|
||
/** | ||
* List template | ||
* @constructor | ||
*/ | ||
export const List: React.FC<ListProps> = ({ | ||
children, | ||
slotLogo, | ||
slotPrimaryNavigation, | ||
...props | ||
}) => { | ||
const { logo: CustomLogo } = useContext(ConfigContext); | ||
const { primaryNavigation } = useContext(NavigationContext); | ||
|
||
return ( | ||
<Page> | ||
<Grid> | ||
<Column span={12}> | ||
{slotLogo || CustomLogo || <Logo />} | ||
{slotPrimaryNavigation || primaryNavigation} | ||
</Column> | ||
|
||
<Column span={12}> | ||
{children} | ||
|
||
<Card> | ||
<Body> | ||
<DataGrid {...props} /> | ||
</Body> | ||
</Card> | ||
</Column> | ||
</Grid> | ||
</Page> | ||
); | ||
}; |