Skip to content

Commit d06942a

Browse files
committed
Add generic column type
1 parent ca55bda commit d06942a

File tree

5 files changed

+13
-10
lines changed

5 files changed

+13
-10
lines changed

src/DSVImport.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { PropsWithChildren, useReducer, useEffect } from 'react';
2-
import { ColumnType } from './models/column';
2+
import { ColumnType, GenericColumnType } from './models/column';
33
import { getDSVImportContext, useDSVImport, createReducer } from './features/context';
44
import { createParserMiddleware } from './middlewares/parserMiddleware';
55
import { State } from './models/state';
@@ -14,7 +14,7 @@ interface EventListenerProps<T> {
1414
onValidation?: (errors: ValidationError<T>[]) => void;
1515
}
1616

17-
const EventListener = <T extends { [key: string]: string }>(props: EventListenerProps<T>) => {
17+
const EventListener = <T extends GenericColumnType>(props: EventListenerProps<T>) => {
1818
const [context] = useDSVImport<T>();
1919

2020
useEffect(() => {
@@ -39,7 +39,7 @@ export interface Props<T> {
3939
transformers?: Transformer[];
4040
}
4141

42-
export const DSVImport = <T extends { [key: string]: string }>(props: PropsWithChildren<Props<T>>) => {
42+
export const DSVImport = <T extends GenericColumnType>(props: PropsWithChildren<Props<T>>) => {
4343
const DSVImportContext = getDSVImportContext<T>();
4444
const initialValues: State<T> = { columns: props.columns, transformers: props.transformers };
4545
const [state, dispatch] = useReducer(createReducer<T>(), initialValues);

src/features/context.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { State, emptyState } from '../models/state';
22
import { createContext, Dispatch, useContext } from 'react';
33
import { Actions } from '../models/actions';
4+
import { GenericColumnType } from '../models/column';
45

56
export const reducer = <T>(state: State<T>, action: Actions<T>) => {
67
switch (action.type) {
@@ -34,4 +35,4 @@ export const getDSVImportContext = <T>() => {
3435
}
3536
return contextSingleton as React.Context<[State<T>, Dispatch<Actions<T>>]>;
3637
};
37-
export const useDSVImport = <T = { [key: string]: string }>() => useContext(getDSVImportContext<T>());
38+
export const useDSVImport = <T = GenericColumnType>() => useContext(getDSVImportContext<T>());

src/index.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1+
import { PropsWithChildren } from 'react';
2+
import { DSVImport as Import, Props } from './DSVImport';
3+
import { GenericColumnType } from './models/column';
14
import { TextareaInput } from './components/inputs/TextareaInput';
25
import { TablePreview } from './components/previews/TablePreview';
3-
import { DSVImport as Import, Props } from './DSVImport';
4-
import { PropsWithChildren } from 'react';
56

6-
export function DSVImport<T extends { [key: string]: string }>(props: PropsWithChildren<Props<T>>) {
7+
export function DSVImport<T extends GenericColumnType>(props: PropsWithChildren<Props<T>>) {
78
return Import<T>(props);
89
}
910

1011
DSVImport.TextareaInput = TextareaInput;
1112
DSVImport.TablePreview = TablePreview;
1213

13-
export { ColumnType } from './models/column';
14+
export { ColumnType, GenericColumnType } from './models/column';
1415
export { useDSVImport } from './features/context';
1516
export { Rule } from './models/rule';
1617
export { Transformer } from './models/transformer';

src/models/column.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Rule } from './rule';
22
import { Transformer } from './transformer';
33

4+
export type GenericColumnType = { [key: string]: string };
45
export type ColumnType<T> = { key: keyof T; label: string; rules?: Rule[]; transformers?: Transformer[] };

src/models/state.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ColumnType } from './column';
1+
import { ColumnType, GenericColumnType } from './column';
22
import { ValidationError } from './validation';
33
import { Transformer } from './transformer';
44

@@ -10,6 +10,6 @@ export interface State<T> {
1010
columns: ColumnType<T>[];
1111
}
1212

13-
export const emptyState: State<{ [key: string]: string }> = {
13+
export const emptyState: State<GenericColumnType> = {
1414
columns: []
1515
};

0 commit comments

Comments
 (0)