1- import { FieldSet , ListTemplate } from "@maykin-ui/admin-ui" ;
2- import { useCallback } from "react" ;
1+ import { ListTemplate } from "@maykin-ui/admin-ui" ;
2+ import React , { useCallback } from "react" ;
33import {
4- useLocation ,
4+ useNavigate ,
55 useNavigation ,
66 useOutlet ,
77 useSearchParams ,
@@ -10,7 +10,7 @@ import { ListResponse } from "~/api/types";
1010import { useBreadcrumbItems } from "~/hooks" ;
1111
1212export type ListViewProps < T extends object > = ListResponse < T > & {
13- fieldsets : FieldSet < T > [ ] ;
13+ getHref ?: ( obj : T ) => string ;
1414} ;
1515
1616/**
@@ -19,20 +19,22 @@ export type ListViewProps<T extends object> = ListResponse<T> & {
1919 * The primary action (click) shows item details in a side pane.
2020 * Ctrl+click or Cmd+click navigates to the item's detail route in fullscreen.
2121 *
22- * @typeParam T - The type of items in the list. Must include at least `uuid` and `identificatie` fields.
22+ * @typeParam T - The type of items in the list. Must include at least `uuid` and
23+ * `identificatie` fields.
2324 *
2425 * @param fields - The field's configuration.
2526 * @param pagination - The paginator configuration.
2627 * @param results - The list of items to render in the data grid.
27- * @param fieldsets - Optional custom fieldsets used for rendering item details.
28- * @param fieldsets - Optional custom fieldsets used for rendering item details .
28+ * @param getHref - A function that if set, receives the row and should return a
29+ * URL to navigate to .
2930 */
3031export function ListView < T extends object > ( {
3132 fields,
3233 pagination,
3334 results,
35+ getHref,
3436} : ListViewProps < T > ) {
35- const { pathname } = useLocation ( ) ;
37+ const navigate = useNavigate ( ) ;
3638 const { state } = useNavigation ( ) ;
3739 const outlet = useOutlet ( ) ;
3840 const [ urlSearchParams , setURLSearchParams ] = useSearchParams ( ) ;
@@ -69,22 +71,37 @@ export function ListView<T extends object>({
6971 [ urlSearchParams ] ,
7072 ) ;
7173
74+ /**
75+ * Gets called when a row's identifier is clicked.
76+ * @param event - The mouse event.
77+ * @param data - The row data.
78+ */
79+ const handleClick = useCallback (
80+ ( event : React . MouseEvent < HTMLAnchorElement > , data : T ) => {
81+ event . preventDefault ( ) ;
82+ if ( getHref ) {
83+ navigate ( getHref ( data ) ) ;
84+ }
85+ } ,
86+ [ getHref ] ,
87+ ) ;
88+
7289 return (
7390 outlet || (
7491 < ListTemplate
7592 breadcrumbItems = { breadcrumbItems }
7693 dataGridProps = { {
7794 objectList : results . map ( ( row ) => ( {
7895 ...row ,
79- // @ts -expect-error - Assume uuid.
80- href : row . uuid ? `${ pathname } /${ row . uuid } ` : undefined ,
96+ href : getHref && getHref ( row ) ,
8197 } ) ) ,
8298 decorate : true ,
8399 fields : fields ,
84100 height : "fill-available-space" ,
85101 showPaginator : Boolean ( pagination ) ,
86102 loading : state !== "idle" ,
87103 paginatorProps : pagination ,
104+ onClick : handleClick ,
88105 onPageChange : handlePageChange ,
89106 } }
90107 />
0 commit comments