UNPKG

10 kBSource Map (JSON)View Raw
1{"version":3,"names":["React","NavigationBuilderContext","NavigationContext","NavigationRouteContext","SceneView","useNavigationCache","useRouteCache","useDescriptors","state","screens","navigation","screenOptions","defaultScreenOptions","onAction","getState","setState","addListener","addKeyedListener","onRouteFocus","router","emitter","options","setOptions","useState","onDispatchAction","onOptionsChange","stackRef","useContext","context","useMemo","navigations","routes","reduce","acc","route","i","config","name","screen","props","key","optionsList","filter","Boolean","customOptions","curr","Object","assign","mergedOptions","clearOptions","o","_","rest","render"],"sources":["useDescriptors.tsx"],"sourcesContent":["import type {\n NavigationAction,\n NavigationState,\n ParamListBase,\n Router,\n} from '@react-navigation/routers';\nimport * as React from 'react';\n\nimport NavigationBuilderContext, {\n AddKeyedListener,\n AddListener,\n} from './NavigationBuilderContext';\nimport NavigationContext from './NavigationContext';\nimport NavigationRouteContext from './NavigationRouteContext';\nimport SceneView from './SceneView';\nimport type {\n Descriptor,\n EventMapBase,\n NavigationHelpers,\n NavigationProp,\n RouteConfig,\n RouteProp,\n} from './types';\nimport type { NavigationEventEmitter } from './useEventEmitter';\nimport useNavigationCache from './useNavigationCache';\nimport useRouteCache from './useRouteCache';\n\nexport type ScreenConfigWithParent<\n State extends NavigationState,\n ScreenOptions extends {},\n EventMap extends EventMapBase\n> = {\n keys: (string | undefined)[];\n options: (ScreenOptionsOrCallback<ScreenOptions> | undefined)[] | undefined;\n props: RouteConfig<ParamListBase, string, State, ScreenOptions, EventMap>;\n};\n\ntype ScreenOptionsOrCallback<ScreenOptions extends {}> =\n | ScreenOptions\n | ((props: {\n route: RouteProp<ParamListBase, string>;\n navigation: any;\n }) => ScreenOptions);\n\ntype Options<\n State extends NavigationState,\n ScreenOptions extends {},\n EventMap extends EventMapBase\n> = {\n state: State;\n screens: Record<\n string,\n ScreenConfigWithParent<State, ScreenOptions, EventMap>\n >;\n navigation: NavigationHelpers<ParamListBase>;\n screenOptions?: ScreenOptionsOrCallback<ScreenOptions>;\n defaultScreenOptions?:\n | ScreenOptions\n | ((props: {\n route: RouteProp<ParamListBase>;\n navigation: any;\n options: ScreenOptions;\n }) => ScreenOptions);\n onAction: (action: NavigationAction) => boolean;\n getState: () => State;\n setState: (state: State) => void;\n addListener: AddListener;\n addKeyedListener: AddKeyedListener;\n onRouteFocus: (key: string) => void;\n router: Router<State, NavigationAction>;\n emitter: NavigationEventEmitter<EventMap>;\n};\n\n/**\n * Hook to create descriptor objects for the child routes.\n *\n * A descriptor object provides 3 things:\n * - Helper method to render a screen\n * - Options specified by the screen for the navigator\n * - Navigation object intended for the route\n */\nexport default function useDescriptors<\n State extends NavigationState,\n ActionHelpers extends Record<string, () => void>,\n ScreenOptions extends {},\n EventMap extends EventMapBase\n>({\n state,\n screens,\n navigation,\n screenOptions,\n defaultScreenOptions,\n onAction,\n getState,\n setState,\n addListener,\n addKeyedListener,\n onRouteFocus,\n router,\n emitter,\n}: Options<State, ScreenOptions, EventMap>) {\n const [options, setOptions] = React.useState<Record<string, object>>({});\n const { onDispatchAction, onOptionsChange, stackRef } = React.useContext(\n NavigationBuilderContext\n );\n\n const context = React.useMemo(\n () => ({\n navigation,\n onAction,\n addListener,\n addKeyedListener,\n onRouteFocus,\n onDispatchAction,\n onOptionsChange,\n stackRef,\n }),\n [\n navigation,\n onAction,\n addListener,\n addKeyedListener,\n onRouteFocus,\n onDispatchAction,\n onOptionsChange,\n stackRef,\n ]\n );\n\n const navigations = useNavigationCache<State, ScreenOptions, EventMap>({\n state,\n getState,\n navigation,\n setOptions,\n router,\n emitter,\n });\n\n const routes = useRouteCache(state.routes);\n\n return routes.reduce<\n Record<\n string,\n Descriptor<\n ScreenOptions,\n NavigationProp<\n ParamListBase,\n string,\n string | undefined,\n State,\n ScreenOptions,\n EventMap\n > &\n ActionHelpers,\n RouteProp<ParamListBase>\n >\n >\n >((acc, route, i) => {\n const config = screens[route.name];\n const screen = config.props;\n const navigation = navigations[route.key];\n\n const optionsList = [\n // The default `screenOptions` passed to the navigator\n screenOptions,\n // The `screenOptions` props passed to `Group` elements\n ...((config.options\n ? config.options.filter(Boolean)\n : []) as ScreenOptionsOrCallback<ScreenOptions>[]),\n // The `options` prop passed to `Screen` elements,\n screen.options,\n // The options set via `navigation.setOptions`\n options[route.key],\n ];\n\n const customOptions = optionsList.reduce<ScreenOptions>(\n (acc, curr) =>\n Object.assign(\n acc,\n typeof curr !== 'function' ? curr : curr({ route, navigation })\n ),\n {} as ScreenOptions\n );\n\n const mergedOptions = {\n ...(typeof defaultScreenOptions === 'function'\n ? // @ts-expect-error: ts gives incorrect error here\n defaultScreenOptions({\n route,\n navigation,\n options: customOptions,\n })\n : defaultScreenOptions),\n ...customOptions,\n };\n\n const clearOptions = () =>\n setOptions((o) => {\n if (route.key in o) {\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n const { [route.key]: _, ...rest } = o;\n return rest;\n }\n\n return o;\n });\n\n acc[route.key] = {\n route,\n // @ts-expect-error: it's missing action helpers, fix later\n navigation,\n render() {\n return (\n <NavigationBuilderContext.Provider key={route.key} value={context}>\n <NavigationContext.Provider value={navigation}>\n <NavigationRouteContext.Provider value={route}>\n <SceneView\n navigation={navigation}\n route={route}\n screen={screen}\n routeState={state.routes[i].state}\n getState={getState}\n setState={setState}\n options={mergedOptions}\n clearOptions={clearOptions}\n />\n </NavigationRouteContext.Provider>\n </NavigationContext.Provider>\n </NavigationBuilderContext.Provider>\n );\n },\n options: mergedOptions as ScreenOptions,\n };\n\n return acc;\n }, {});\n}\n"],"mappings":"AAMA,OAAO,KAAKA,KAAZ,MAAuB,OAAvB;AAEA,OAAOC,wBAAP,MAGO,4BAHP;AAIA,OAAOC,iBAAP,MAA8B,qBAA9B;AACA,OAAOC,sBAAP,MAAmC,0BAAnC;AACA,OAAOC,SAAP,MAAsB,aAAtB;AAUA,OAAOC,kBAAP,MAA+B,sBAA/B;AACA,OAAOC,aAAP,MAA0B,iBAA1B;;AAgDA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAe,SAASC,cA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