You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.3 KiB
47 lines
1.3 KiB
import { InputProps as MUIInputProps } from '@mui/material'; |
|
import { Dispatch, SetStateAction } from 'react'; |
|
|
|
import MAP_TO_VALUE_CONVERTER from './consts/MAP_TO_VALUE_CONVERTER'; |
|
|
|
type InputOnChangeParameters = Parameters< |
|
Exclude<MUIInputProps['onChange'], undefined> |
|
>; |
|
|
|
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 = |
|
<TypeName extends keyof MapToType>({ |
|
postSet, |
|
preSet, |
|
set, |
|
setType = 'string', |
|
}: CreateInputOnChangeHandlerOptions<TypeName> = {}): MUIInputProps['onChange'] => |
|
(event) => { |
|
const { |
|
target: { value }, |
|
} = event; |
|
const postConvertValue = MAP_TO_VALUE_CONVERTER[setType]( |
|
value, |
|
) as MapToType[TypeName]; |
|
|
|
preSet?.call(null, event); |
|
set?.call(null, postConvertValue); |
|
postSet?.call(null, event); |
|
}; |
|
|
|
export type { |
|
CreateInputOnChangeHandlerOptions, |
|
InputOnChangeParameters, |
|
MapToStateSetter, |
|
}; |
|
|
|
export default createInputOnChangeHandler;
|
|
|