2022-07-23 00:41:16 +00:00
|
|
|
import { InputProps as MUIInputProps } from '@mui/material';
|
|
|
|
|
2022-07-26 23:05:18 +00:00
|
|
|
import MAP_TO_VALUE_CONVERTER from './consts/MAP_TO_VALUE_CONVERTER';
|
2022-07-23 00:41:16 +00:00
|
|
|
|
|
|
|
const createInputOnChangeHandler =
|
2022-11-26 02:56:50 +00:00
|
|
|
<TypeName extends keyof MapToInputType>({
|
2022-07-23 00:41:16 +00:00
|
|
|
postSet,
|
|
|
|
preSet,
|
|
|
|
set,
|
2022-11-26 01:38:01 +00:00
|
|
|
setType = 'string' as TypeName,
|
2022-07-26 23:05:18 +00:00
|
|
|
}: CreateInputOnChangeHandlerOptions<TypeName> = {}): MUIInputProps['onChange'] =>
|
2022-07-23 00:41:16 +00:00
|
|
|
(event) => {
|
|
|
|
const {
|
|
|
|
target: { value },
|
|
|
|
} = event;
|
|
|
|
const postConvertValue = MAP_TO_VALUE_CONVERTER[setType](
|
|
|
|
value,
|
2022-11-26 02:56:50 +00:00
|
|
|
) as MapToInputType[TypeName];
|
2022-07-23 00:41:16 +00:00
|
|
|
|
|
|
|
preSet?.call(null, event);
|
|
|
|
set?.call(null, postConvertValue);
|
|
|
|
postSet?.call(null, event);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default createInputOnChangeHandler;
|