1
- import React , { PropsWithChildren , useReducer , useEffect } from 'react' ;
1
+ import { PropsWithChildren , useReducer , useEffect , useMemo , Dispatch } from 'react' ;
2
2
import { ColumnType , GenericColumnType } from './models/column' ;
3
3
import { getDSVImportContext , useDSVImport , createReducer } from './features/context' ;
4
4
import { createParserMiddleware } from './middlewares/parserMiddleware' ;
@@ -10,29 +10,30 @@ import { createTransformerMiddleware } from './middlewares/transformerMiddleware
10
10
import { Transformer } from './models/transformer' ;
11
11
import { TextareaInput } from './components/inputs/TextareaInput' ;
12
12
import { TablePreview } from './components/previews/TablePreview' ;
13
+ import { Actions } from './models/actions' ;
13
14
14
15
interface EventListenerProps < T > {
15
16
onChange ?: ( value : T [ ] ) => void ;
16
17
onValidation ?: ( errors : ValidationError < T > [ ] ) => void ;
17
18
}
18
19
19
- const EventListener = < T extends GenericColumnType > ( props : EventListenerProps < T > ) => {
20
+ function EventListener < T extends GenericColumnType > ( { onChange , onValidation } : EventListenerProps < T > ) {
20
21
const [ context ] = useDSVImport < T > ( ) ;
21
22
22
23
useEffect ( ( ) => {
23
- if ( context . parsed && props . onChange ) {
24
- props . onChange ( context . parsed ) ;
24
+ if ( context . parsed && onChange ) {
25
+ onChange ( context . parsed ) ;
25
26
}
26
27
} , [ context . parsed ] ) ;
27
28
28
29
useEffect ( ( ) => {
29
- if ( context . validation && props . onValidation ) {
30
- props . onValidation ( context . validation ) ;
30
+ if ( context . validation && onValidation ) {
31
+ onValidation ( context . validation ) ;
31
32
}
32
33
} , [ context . validation ] ) ;
33
34
34
35
return null ;
35
- } ;
36
+ }
36
37
37
38
export type Props < T > = {
38
39
/**
@@ -56,22 +57,30 @@ export type Props<T> = {
56
57
/**
57
58
* This is the main component, which creates a context for it's children. All children can access the information of the `DSVImport`.
58
59
*/
59
- export function DSVImport < T extends GenericColumnType > ( props : PropsWithChildren < Props < T > > ) {
60
+ export function DSVImport < T extends GenericColumnType > ( {
61
+ columns,
62
+ onChange,
63
+ onValidation,
64
+ transformers,
65
+ children,
66
+ } : PropsWithChildren < Props < T > > ) {
60
67
const DSVImportContext = getDSVImportContext < T > ( ) ;
61
- const initialValues : State < T > = { columns : props . columns , transformers : props . transformers } ;
68
+ const initialValues : State < T > = { columns, transformers } ;
62
69
const [ state , dispatch ] = useReducer ( createReducer < T > ( ) , initialValues ) ;
63
70
const enhancedDispatch = applyMiddlewares (
64
71
state ,
65
72
dispatch ,
66
73
createParserMiddleware ( ) ,
67
74
createTransformerMiddleware ( ) ,
68
- createValidatorMiddleware ( )
75
+ createValidatorMiddleware ( ) ,
69
76
) ;
70
77
78
+ const value = useMemo < [ State < T > , Dispatch < Actions < T > > ] > ( ( ) => [ state , enhancedDispatch ] , [ state ] ) ;
79
+
71
80
return (
72
- < DSVImportContext . Provider value = { [ state , enhancedDispatch ] } >
73
- < EventListener < T > onChange = { props . onChange } onValidation = { props . onValidation } />
74
- { props . children }
81
+ < DSVImportContext . Provider value = { value } >
82
+ < EventListener < T > onChange = { onChange } onValidation = { onValidation } />
83
+ { children }
75
84
</ DSVImportContext . Provider >
76
85
) ;
77
86
}
0 commit comments