|
|
@ -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; |
|
|
|