1
- import { FC , PropsWithChildren , ComponentPropsWithoutRef , createContext , useContext , useReducer } from 'react' ;
1
+ import { PropsWithChildren , ComponentPropsWithoutRef , createContext , useContext , useReducer } from 'react' ;
2
2
3
3
export type TagType = React . ElementType | keyof JSX . IntrinsicElements ;
4
4
5
- type TypesElementProps < T extends TagType > = {
5
+ type TypesElementProps < T extends TagType = 'span' > = {
6
6
as ?: T ;
7
7
render ?: ( props : TypesElement < T > , result : { type : 'type' | 'value' ; value ?: unknown } ) => React . ReactNode ;
8
8
'data-type' ?: string ;
9
9
} ;
10
10
11
11
export type TypesElement < T extends TagType > = TypesElementProps < T > & ComponentPropsWithoutRef < T > ;
12
- export type InitialTypesState = {
12
+ export type InitialTypesState < T extends TagType = 'span' > = {
13
13
displayDataTypes ?: boolean ;
14
- Url ?: TypesElement < TagType > ;
15
- Str ?: TypesElement < TagType > ;
16
- Undefined ?: TypesElement < TagType > ;
17
- Null ?: TypesElement < TagType > ;
18
- True ?: TypesElement < TagType > ;
19
- False ?: TypesElement < TagType > ;
20
- Date ?: TypesElement < TagType > ;
21
- Float ?: TypesElement < TagType > ;
22
- Set ?: TypesElement < TagType > ;
23
- Int ?: TypesElement < TagType > ;
24
- Map ?: TypesElement < TagType > ;
25
- Nan ?: TypesElement < TagType > ;
26
- Bigint ?: TypesElement < TagType > ;
14
+ Url ?: TypesElement < T > ;
15
+ Str ?: TypesElement < T > ;
16
+ Undefined ?: TypesElement < T > ;
17
+ Null ?: TypesElement < T > ;
18
+ True ?: TypesElement < T > ;
19
+ False ?: TypesElement < T > ;
20
+ Date ?: TypesElement < T > ;
21
+ Float ?: TypesElement < T > ;
22
+ Set ?: TypesElement < T > ;
23
+ Int ?: TypesElement < T > ;
24
+ Map ?: TypesElement < T > ;
25
+ Nan ?: TypesElement < T > ;
26
+ Bigint ?: TypesElement < T > ;
27
27
} ;
28
- type Dispatch = React . Dispatch < InitialTypesState > ;
28
+ type Dispatch < T extends TagType > = React . Dispatch < InitialTypesState < T > > ;
29
29
30
- const initialState : InitialTypesState = {
30
+ const initialState : InitialTypesState < TagType | 'span' > = {
31
31
Str : {
32
32
as : 'span' ,
33
33
'data-type' : 'string' ,
@@ -67,6 +67,7 @@ const initialState: InitialTypesState = {
67
67
Map : {
68
68
style : {
69
69
color : 'var(--w-rjv-type-map-color, #268bd2)' ,
70
+ marginRight : 3 ,
70
71
} ,
71
72
as : 'span' ,
72
73
'data-type' : 'map' ,
@@ -103,6 +104,7 @@ const initialState: InitialTypesState = {
103
104
Set : {
104
105
style : {
105
106
color : 'var(--w-rjv-type-set-color, #268bd2)' ,
107
+ marginRight : 3 ,
106
108
} ,
107
109
as : 'span' ,
108
110
'data-type' : 'set' ,
@@ -146,9 +148,10 @@ const initialState: InitialTypesState = {
146
148
children : 'date' ,
147
149
} ,
148
150
} ;
149
- const Context = createContext < InitialTypesState > ( initialState ) ;
150
151
151
- const reducer = ( state : InitialTypesState , action : InitialTypesState ) => ( {
152
+ const Context = createContext < InitialTypesState < TagType > > ( initialState ) ;
153
+
154
+ const reducer = < T extends TagType > ( state : InitialTypesState < T > , action : InitialTypesState < T > ) => ( {
152
155
...state ,
153
156
...action ,
154
157
} ) ;
@@ -157,7 +160,7 @@ export const useTypesStore = () => {
157
160
return useContext ( Context ) ;
158
161
} ;
159
162
160
- const DispatchTypes = createContext < Dispatch > ( ( ) => { } ) ;
163
+ const DispatchTypes = createContext < Dispatch < TagType > > ( ( ) => { } ) ;
161
164
DispatchTypes . displayName = 'JVR.DispatchTypes' ;
162
165
163
166
export function useTypes ( ) {
@@ -168,17 +171,17 @@ export function useTypesDispatch() {
168
171
return useContext ( DispatchTypes ) ;
169
172
}
170
173
171
- interface TypesProps {
172
- initial : InitialTypesState ;
173
- dispatch : Dispatch ;
174
+ interface TypesProps < T extends TagType > {
175
+ initial : InitialTypesState < T > ;
176
+ dispatch : Dispatch < TagType > ;
174
177
}
175
178
176
- export const Types : FC < PropsWithChildren < TypesProps > > = ( { initial, dispatch, children } ) => {
179
+ export function Types < T extends TagType > ( { initial, dispatch, children } : PropsWithChildren < TypesProps < T > > ) {
177
180
return (
178
- < Context . Provider value = { initial } >
181
+ < Context . Provider value = { initial as unknown as InitialTypesState < TagType > } >
179
182
< DispatchTypes . Provider value = { dispatch } > { children } </ DispatchTypes . Provider >
180
183
</ Context . Provider >
181
184
) ;
182
- } ;
185
+ }
183
186
184
187
Types . displayName = 'JVR.Types' ;
0 commit comments