File tree 3 files changed +36
-42
lines changed
3 files changed +36
-42
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ import React from 'react' ;
2
+
3
+ export default function buildContext < P , V > ( {
4
+ defaultValue = { } as V ,
5
+ useContextValue
6
+ } : {
7
+ defaultValue ?: V ;
8
+ useContextValue : ( p : P ) => V ;
9
+ } ) {
10
+ const Context = React . createContext ( defaultValue ) ;
11
+
12
+ function useContext ( ) {
13
+ return React . useContext ( Context ) ;
14
+ }
15
+
16
+ function ContextProvider ( {
17
+ children,
18
+ contextValueParameters = undefined as P
19
+ } : React . PropsWithChildren < {
20
+ contextValueParameters ?: P ;
21
+ } > ) {
22
+ const value = useContextValue ( contextValueParameters ) ;
23
+
24
+ if ( value === undefined ) {
25
+ return null ;
26
+ }
27
+
28
+ return < Context . Provider value = { value } > { children } </ Context . Provider > ;
29
+ }
30
+
31
+ return { useContext, ContextProvider} ;
32
+ }
Original file line number Diff line number Diff line change 1
- import React from 'react' ;
2
1
import buildContext from '~/components/jsx-helpers/build-context' ;
2
+ import { useState } from 'react' ;
3
3
4
4
function useContextValue ( { prefix} = { prefix : 'menulabel' } ) {
5
- const [ activeDropdown , setActiveDropdown ] = React . useState ( { } ) ;
6
- const [ submenuLabel , setSubmenuLabel ] = React . useState ( ) ;
5
+ const [ activeDropdown , setActiveDropdown ] = useState ( { } ) ;
6
+ const [ submenuLabel , setSubmenuLabel ] = useState ( ) ;
7
7
8
8
return {
9
9
activeDropdown,
@@ -16,14 +16,4 @@ function useContextValue({prefix} = {prefix: 'menulabel'}) {
16
16
17
17
const { useContext, ContextProvider} = buildContext ( { useContextValue} ) ;
18
18
19
- type Arg = {
20
- prefix : string ;
21
- } ;
22
- type DDCPArgs = React . PropsWithChildren < {
23
- contextValueParameters ?: Arg ;
24
- } > ;
25
- const DropdownContextProvider = ContextProvider as (
26
- args : DDCPArgs
27
- ) => JSX . Element ;
28
-
29
- export { useContext as default , DropdownContextProvider } ;
19
+ export { useContext as default , ContextProvider as DropdownContextProvider } ;
You can’t perform that action at this time.
0 commit comments