fix(striker-ui): relocate map to value converter constant

main
Tsu-ba-me 2 years ago
parent 89ad9d34e9
commit 7ad4ad725e
  1. 6
      striker-ui/lib/consts/MAP_TO_VALUE_CONVERTER.ts
  2. 38
      striker-ui/lib/createInputOnChangeHandler.ts
  3. 4
      striker-ui/types/MapToType.d.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;

@ -1,22 +1,21 @@
import { Dispatch, SetStateAction } from 'react';
import { InputProps as MUIInputProps } from '@mui/material'; import { InputProps as MUIInputProps } from '@mui/material';
import { Dispatch, SetStateAction } from 'react';
export type MapToStateSetter = { import MAP_TO_VALUE_CONVERTER from './consts/MAP_TO_VALUE_CONVERTER';
[TypeName in keyof MapToType]: Dispatch<SetStateAction<MapToType[TypeName]>>;
};
export type MapToValueConverter = {
[TypeName in keyof MapToType]: (value: unknown) => MapToType[TypeName];
};
export type InputOnChangeParameters = Parameters< type InputOnChangeParameters = Parameters<
Exclude<MUIInputProps['onChange'], undefined> Exclude<MUIInputProps['onChange'], undefined>
>; >;
const MAP_TO_VALUE_CONVERTER: MapToValueConverter = { type MapToStateSetter = {
number: (value) => parseInt(String(value), 10) || 0, [TypeName in keyof MapToType]: Dispatch<SetStateAction<MapToType[TypeName]>>;
string: (value) => String(value), };
type CreateInputOnChangeHandlerOptions<TypeName extends keyof MapToType> = {
postSet?: (...args: InputOnChangeParameters) => void;
preSet?: (...args: InputOnChangeParameters) => void;
set?: MapToStateSetter[TypeName];
setType?: TypeName | 'string';
}; };
const createInputOnChangeHandler = const createInputOnChangeHandler =
@ -25,12 +24,7 @@ const createInputOnChangeHandler =
preSet, preSet,
set, set,
setType = 'string', setType = 'string',
}: { }: CreateInputOnChangeHandlerOptions<TypeName> = {}): MUIInputProps['onChange'] =>
postSet?: (...args: InputOnChangeParameters) => void;
preSet?: (...args: InputOnChangeParameters) => void;
set?: MapToStateSetter[TypeName];
setType?: TypeName | 'string';
} = {}): MUIInputProps['onChange'] =>
(event) => { (event) => {
const { const {
target: { value }, target: { value },
@ -44,4 +38,10 @@ const createInputOnChangeHandler =
postSet?.call(null, event); postSet?.call(null, event);
}; };
export type {
CreateInputOnChangeHandlerOptions,
InputOnChangeParameters,
MapToStateSetter,
};
export default createInputOnChangeHandler; export default createInputOnChangeHandler;

@ -2,3 +2,7 @@ declare type MapToType = {
number: number; number: number;
string: string; string: string;
}; };
declare type MapToValueConverter = {
[TypeName in keyof MapToType]: (value: unknown) => MapToType[TypeName];
};

Loading…
Cancel
Save