2023-02-17 05:50:50 +00:00
|
|
|
import { ChangeEventHandler } from 'react';
|
2022-07-23 00:41:16 +00:00
|
|
|
|
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,
|
2023-02-17 05:50:50 +00:00
|
|
|
valueKey = 'value',
|
|
|
|
}: CreateInputOnChangeHandlerOptions<TypeName> = {}): ChangeEventHandler<HTMLInputElement> =>
|
2022-07-23 00:41:16 +00:00
|
|
|
(event) => {
|
|
|
|
const {
|
2023-02-17 05:50:50 +00:00
|
|
|
target: { [valueKey]: value },
|
2022-07-23 00:41:16 +00:00
|
|
|
} = 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;
|