UNPKG

5.71 kBSource Map (JSON)View Raw
1{"version":3,"names":["React","NavigationBuilderContext","NavigationStateContext","useOptionsGetters","key","options","navigation","optionsRef","useRef","optionsGettersFromChildRef","onOptionsChange","useContext","addOptionsGetter","parentAddOptionsGetter","optionsChangeListener","useCallback","isFocused","hasChildren","Object","keys","current","length","useEffect","addListener","getOptionsFromListener","hasOwnProperty","result","getCurrentOptions","optionsFromListener","getter"],"sources":["useOptionsGetters.tsx"],"sourcesContent":["import type { ParamListBase } from '@react-navigation/routers';\nimport * as React from 'react';\n\nimport NavigationBuilderContext from './NavigationBuilderContext';\nimport NavigationStateContext from './NavigationStateContext';\nimport type { NavigationProp } from './types';\n\ntype Options = {\n key?: string;\n navigation?: NavigationProp<ParamListBase>;\n options?: object | undefined;\n};\n\nexport default function useOptionsGetters({\n key,\n options,\n navigation,\n}: Options) {\n const optionsRef = React.useRef<object | undefined>(options);\n const optionsGettersFromChildRef = React.useRef<\n Record<string, () => object | undefined | null>\n >({});\n\n const { onOptionsChange } = React.useContext(NavigationBuilderContext);\n const { addOptionsGetter: parentAddOptionsGetter } = React.useContext(\n NavigationStateContext\n );\n\n const optionsChangeListener = React.useCallback(() => {\n const isFocused = navigation?.isFocused() ?? true;\n const hasChildren = Object.keys(optionsGettersFromChildRef.current).length;\n\n if (isFocused && !hasChildren) {\n onOptionsChange(optionsRef.current ?? {});\n }\n }, [navigation, onOptionsChange]);\n\n React.useEffect(() => {\n optionsRef.current = options;\n optionsChangeListener();\n\n return navigation?.addListener('focus', optionsChangeListener);\n }, [navigation, options, optionsChangeListener]);\n\n const getOptionsFromListener = React.useCallback(() => {\n for (let key in optionsGettersFromChildRef.current) {\n if (optionsGettersFromChildRef.current.hasOwnProperty(key)) {\n const result = optionsGettersFromChildRef.current[key]?.();\n\n // null means unfocused route\n if (result !== null) {\n return result;\n }\n }\n }\n\n return null;\n }, []);\n\n const getCurrentOptions = React.useCallback(() => {\n const isFocused = navigation?.isFocused() ?? true;\n\n if (!isFocused) {\n return null;\n }\n\n const optionsFromListener = getOptionsFromListener();\n\n if (optionsFromListener !== null) {\n return optionsFromListener;\n }\n\n return optionsRef.current;\n }, [navigation, getOptionsFromListener]);\n\n React.useEffect(() => {\n return parentAddOptionsGetter?.(key!, getCurrentOptions);\n }, [getCurrentOptions, parentAddOptionsGetter, key]);\n\n const addOptionsGetter = React.useCallback(\n (key: string, getter: () => object | undefined | null) => {\n optionsGettersFromChildRef.current[key] = getter;\n optionsChangeListener();\n\n return () => {\n // eslint-disable-next-line @typescript-eslint/no-dynamic-delete\n delete optionsGettersFromChildRef.current[key];\n optionsChangeListener();\n };\n },\n [optionsChangeListener]\n );\n\n return {\n addOptionsGetter,\n getCurrentOptions,\n };\n}\n"],"mappings":"AACA,OAAO,KAAKA,KAAZ,MAAuB,OAAvB;AAEA,OAAOC,wBAAP,MAAqC,4BAArC;AACA,OAAOC,sBAAP,MAAmC,0BAAnC;AASA,eAAe,SAASC,iBAAT,OAIH;EAAA,IAJ8B;IACxCC,GADwC;IAExCC,OAFwC;IAGxCC;EAHwC,CAI9B;EACV,MAAMC,UAAU,GAAGP,KAAK,CAACQ,MAAN,CAAiCH,OAAjC,CAAnB;EACA,MAAMI,0BAA0B,GAAGT,KAAK,CAACQ,MAAN,CAEjC,EAFiC,CAAnC;EAIA,MAAM;IAAEE;EAAF,IAAsBV,KAAK,CAACW,UAAN,CAAiBV,wBAAjB,CAA5B;EACA,MAAM;IAAEW,gBAAgB,EAAEC;EAApB,IAA+Cb,KAAK,CAACW,UAAN,CACnDT,sBADmD,CAArD;EAIA,MAAMY,qBAAqB,GAAGd,KAAK,CAACe,WAAN,CAAkB,MAAM;IAAA;;IACpD,MAAMC,SAAS,4BAAGV,UAAH,aAAGA,UAAH,uBAAGA,UAAU,CAAEU,SAAZ,EAAH,yEAA8B,IAA7C;IACA,MAAMC,WAAW,GAAGC,MAAM,CAACC,IAAP,CAAYV,0BAA0B,CAACW,OAAvC,EAAgDC,MAApE;;IAEA,IAAIL,SA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