diff --git a/striker-ui/lib/consts/MAP_TO_VALUE_CONVERTER.ts b/striker-ui/lib/consts/MAP_TO_VALUE_CONVERTER.ts new file mode 100644 index 00000000..20e0334d --- /dev/null +++ b/striker-ui/lib/consts/MAP_TO_VALUE_CONVERTER.ts @@ -0,0 +1,6 @@ +const MAP_TO_VALUE_CONVERTER: MapToValueConverter = { + number: (value) => parseInt(String(value), 10) || 0, + string: (value) => String(value), +}; + +export default MAP_TO_VALUE_CONVERTER; diff --git a/striker-ui/lib/createInputOnChangeHandler.ts b/striker-ui/lib/createInputOnChangeHandler.ts index 5b3d2c5e..5d3ac920 100644 --- a/striker-ui/lib/createInputOnChangeHandler.ts +++ b/striker-ui/lib/createInputOnChangeHandler.ts @@ -1,22 +1,21 @@ -import { Dispatch, SetStateAction } from 'react'; - import { InputProps as MUIInputProps } from '@mui/material'; +import { Dispatch, SetStateAction } from 'react'; -export type MapToStateSetter = { - [TypeName in keyof MapToType]: Dispatch>; -}; - -export type MapToValueConverter = { - [TypeName in keyof MapToType]: (value: unknown) => MapToType[TypeName]; -}; +import MAP_TO_VALUE_CONVERTER from './consts/MAP_TO_VALUE_CONVERTER'; -export type InputOnChangeParameters = Parameters< +type InputOnChangeParameters = Parameters< Exclude >; -const MAP_TO_VALUE_CONVERTER: MapToValueConverter = { - number: (value) => parseInt(String(value), 10) || 0, - string: (value) => String(value), +type MapToStateSetter = { + [TypeName in keyof MapToType]: Dispatch>; +}; + +type CreateInputOnChangeHandlerOptions = { + postSet?: (...args: InputOnChangeParameters) => void; + preSet?: (...args: InputOnChangeParameters) => void; + set?: MapToStateSetter[TypeName]; + setType?: TypeName | 'string'; }; const createInputOnChangeHandler = @@ -25,12 +24,7 @@ const createInputOnChangeHandler = preSet, set, setType = 'string', - }: { - postSet?: (...args: InputOnChangeParameters) => void; - preSet?: (...args: InputOnChangeParameters) => void; - set?: MapToStateSetter[TypeName]; - setType?: TypeName | 'string'; - } = {}): MUIInputProps['onChange'] => + }: CreateInputOnChangeHandlerOptions = {}): MUIInputProps['onChange'] => (event) => { const { target: { value }, @@ -44,4 +38,10 @@ const createInputOnChangeHandler = postSet?.call(null, event); }; +export type { + CreateInputOnChangeHandlerOptions, + InputOnChangeParameters, + MapToStateSetter, +}; + export default createInputOnChangeHandler; diff --git a/striker-ui/types/MapToType.d.ts b/striker-ui/types/MapToType.d.ts index 69bfeea2..0e34996f 100644 --- a/striker-ui/types/MapToType.d.ts +++ b/striker-ui/types/MapToType.d.ts @@ -2,3 +2,7 @@ declare type MapToType = { number: number; string: string; }; + +declare type MapToValueConverter = { + [TypeName in keyof MapToType]: (value: unknown) => MapToType[TypeName]; +};