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 { Dispatch, SetStateAction } from 'react';
export type MapToStateSetter = {
[TypeName in keyof MapToType]: Dispatch<SetStateAction<MapToType[TypeName]>>;
};
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<MUIInputProps['onChange'], undefined>
>;
const MAP_TO_VALUE_CONVERTER: MapToValueConverter = {
number: (value) => parseInt(String(value), 10) || 0,
string: (value) => String(value),
type MapToStateSetter = {
[TypeName in keyof MapToType]: Dispatch<SetStateAction<MapToType[TypeName]>>;
};
type CreateInputOnChangeHandlerOptions<TypeName extends keyof MapToType> = {
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<TypeName> = {}): MUIInputProps['onChange'] =>
(event) => {
const {
target: { value },
@ -44,4 +38,10 @@ const createInputOnChangeHandler =
postSet?.call(null, event);
};
export type {
CreateInputOnChangeHandlerOptions,
InputOnChangeParameters,
MapToStateSetter,
};
export default createInputOnChangeHandler;

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

Loading…
Cancel
Save