From 252460a0de9f29c42d275ad51a301f6b883a19e3 Mon Sep 17 00:00:00 2001 From: Christiano Higuto Date: Thu, 29 Aug 2024 10:12:52 -0400 Subject: [PATCH 1/2] feat: rename defaultRoute to initialRoute and fix prop drilling --- packages/react-navigation/src/components/Router.tsx | 12 ++++++------ .../react-navigation/src/components/RoutesRoot.tsx | 11 +++++++---- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/packages/react-navigation/src/components/Router.tsx b/packages/react-navigation/src/components/Router.tsx index a33f7187..01caeaa4 100644 --- a/packages/react-navigation/src/components/Router.tsx +++ b/packages/react-navigation/src/components/Router.tsx @@ -31,7 +31,7 @@ const router = ( AdminProvider: ComponentType>, routes: ReactElement[], items: DrawerItemProps[], - defaultRoute?: string, + initialRoute?: string, authModuleProps?: AuthModule, drawerProps?: DrawerProps, navbarProps?: NavbarProps, @@ -56,11 +56,11 @@ const router = ( + >; - defaultRoute?: string; + initialRoute?: string; useMemoryRouter?: boolean; authModuleProps?: AuthModule; drawerProps?: DrawerProps; @@ -98,7 +98,7 @@ type RouterProps = { const Router = ({ children, AdminProvider, - defaultRoute, + initialRoute, useMemoryRouter = false, authModuleProps, drawerProps, @@ -123,7 +123,7 @@ const Router = ({ AdminProvider, children, items, - defaultRoute, + initialRoute, authModuleProps, drawerProps, navbarProps, diff --git a/packages/react-navigation/src/components/RoutesRoot.tsx b/packages/react-navigation/src/components/RoutesRoot.tsx index 447d2263..df4c7196 100644 --- a/packages/react-navigation/src/components/RoutesRoot.tsx +++ b/packages/react-navigation/src/components/RoutesRoot.tsx @@ -15,7 +15,7 @@ import { AuthModule } from './Router'; type RoutesRootProps = { items: DrawerItemProps[]; routes: ReactElement[]; - defaultRoute?: string; + initialRoute?: string; authModuleProps?: AuthModule; drawerProps?: DrawerProps; navbarProps?: NavbarProps; @@ -32,7 +32,7 @@ type RoutesRootProps = { const RoutesRoot = ({ routes, items, - defaultRoute, + initialRoute, authModuleProps, drawerProps, navbarProps, @@ -42,11 +42,14 @@ const RoutesRoot = ({ renderForgotPassword, renderResetPassword, }: RoutesRootProps) => { - const home = defaultRoute ?? routes[0].props.id; + const home = routes[0].props.id; return ( - } /> + } + /> Date: Fri, 30 Aug 2024 09:14:55 -0400 Subject: [PATCH 2/2] feat: add showAppBar, useNavigateFilter, showDrawerItem and expand custom checkbox widget --- .../CustomWidgets/CustomCheckboxWidget.tsx | 8 +- .../src/components/DefaultRoute.tsx | 80 ++++++++----------- .../src/components/Resource.tsx | 2 + .../src/components/Router.tsx | 18 ++++- .../src/components/RoutesRoot.tsx | 4 + 5 files changed, 61 insertions(+), 51 deletions(-) diff --git a/packages/react-material-ui/src/styles/CustomWidgets/CustomCheckboxWidget.tsx b/packages/react-material-ui/src/styles/CustomWidgets/CustomCheckboxWidget.tsx index 5757ed1d..e3de5385 100644 --- a/packages/react-material-ui/src/styles/CustomWidgets/CustomCheckboxWidget.tsx +++ b/packages/react-material-ui/src/styles/CustomWidgets/CustomCheckboxWidget.tsx @@ -1,8 +1,12 @@ -import React from 'react'; +import React, { ReactNode } from 'react'; import { Checkbox } from '../../components/Checkbox'; import { WidgetProps } from '@rjsf/utils'; -const CustomCheckboxWidget = (props: WidgetProps) => ( +type CustomCheckboxWidgetProps = WidgetProps & { + label: string | ReactNode; +}; + +const CustomCheckboxWidget = (props: CustomCheckboxWidgetProps) => ( { +}: DefaultRouteProps): JSX.Element => { const navigate = useNavigate(); - const resourceName = resource.substring(1); - const menuItems = items?.map((item) => ({ + const menuItems = items.map((item) => ({ ...item, - onClick: () => { - if (!item?.id) return; - navigate(item.id); - }, + onClick: () => item?.id && navigate(item.id), })); - let renderedChildren = null; - - if (page) { - renderedChildren = page; - } - - if (module) { - renderedChildren = ( - - ); - } - - if (renderAppBar) { - if (!isUnprotected) { - return <>{renderAppBar(menuItems, renderedChildren)}; - } - - return ( - - {renderAppBar(menuItems, renderedChildren)} - - ); - } - - if (!isUnprotected) { - return ( - - {renderedChildren} - - ); - } + const content = module ? ( + + ) : ( + page + ); - return ( - + const wrappedContent = showAppBar ? ( + renderAppBar ? ( + renderAppBar(menuItems, content) + ) : ( - {renderedChildren} + {content} - + ) + ) : ( + content ); + + const finalContent = isUnprotected ? ( + wrappedContent + ) : ( + {wrappedContent} + ); + + return <>{finalContent}; }; export default DefaultRoute; diff --git a/packages/react-navigation/src/components/Resource.tsx b/packages/react-navigation/src/components/Resource.tsx index a51d9ab8..45994ca7 100644 --- a/packages/react-navigation/src/components/Resource.tsx +++ b/packages/react-navigation/src/components/Resource.tsx @@ -6,7 +6,9 @@ type ResourceProps = { id: string; name: string; icon: ReactNode; + showDrawerItem?: boolean; isUnprotected?: boolean; + showAppBar?: boolean; module?: Partial; page?: ReactNode; }; diff --git a/packages/react-navigation/src/components/Router.tsx b/packages/react-navigation/src/components/Router.tsx index 01caeaa4..a284afaa 100644 --- a/packages/react-navigation/src/components/Router.tsx +++ b/packages/react-navigation/src/components/Router.tsx @@ -31,6 +31,7 @@ const router = ( AdminProvider: ComponentType>, routes: ReactElement[], items: DrawerItemProps[], + useNavigateFilter?: boolean, initialRoute?: string, authModuleProps?: AuthModule, drawerProps?: DrawerProps, @@ -56,10 +57,11 @@ const router = ( + >; + useNavigateFilter?: boolean; initialRoute?: string; useMemoryRouter?: boolean; authModuleProps?: AuthModule; @@ -98,6 +101,7 @@ type RouterProps = { const Router = ({ children, AdminProvider, + useNavigateFilter, initialRoute, useMemoryRouter = false, authModuleProps, @@ -110,12 +114,21 @@ const Router = ({ renderResetPassword, }: RouterProps) => { const items = Children.map(children, (child) => { + // This validation is needed so `showDrawerItem` + // can be `true` by default + if ( + child.props.showDrawerItem !== undefined && + !child.props.showDrawerItem + ) { + return null; + } + return { id: child.props.id, text: child.props.name, icon: child.props.icon, }; - }); + }).filter((item) => !!item); return (