refactor(striker-ui): repairs types in map to value converter

main
Tsu-ba-me 2 years ago
parent 2e9a22373b
commit abc7fdf54c
  1. 11
      striker-ui/components/InputWithRef.tsx
  2. 2
      striker-ui/lib/consts/MAP_TO_VALUE_CONVERTER.ts
  3. 4
      striker-ui/lib/createInputOnChangeHandler.ts
  4. 6
      striker-ui/types/MapToType.d.ts

@ -22,12 +22,9 @@ type InputWithRefTypeMap = Pick<MapToType, 'number' | 'string'>;
type InputWithRefOptionalPropsWithDefault< type InputWithRefOptionalPropsWithDefault<
TypeName extends keyof InputWithRefTypeMap, TypeName extends keyof InputWithRefTypeMap,
> = { > = {
createInputOnChangeHandlerOptions?: Omit< createInputOnChangeHandlerOptions?: CreateInputOnChangeHandlerOptions<TypeName>;
CreateInputOnChangeHandlerOptions<TypeName>,
'set'
>;
required?: boolean; required?: boolean;
valueType?: TypeName | 'string'; valueType?: TypeName;
}; };
type InputWithRefOptionalPropsWithoutDefault = { type InputWithRefOptionalPropsWithoutDefault = {
inputTestBatch?: InputTestBatch; inputTestBatch?: InputTestBatch;
@ -76,12 +73,12 @@ const InputWithRef = forwardRef(
createInputOnChangeHandlerOptions: { createInputOnChangeHandlerOptions: {
postSet: postSetAppend, postSet: postSetAppend,
...restCreateInputOnChangeHandlerOptions ...restCreateInputOnChangeHandlerOptions
} = INPUT_WITH_REF_DEFAULT_PROPS.createInputOnChangeHandlerOptions, } = INPUT_WITH_REF_DEFAULT_PROPS.createInputOnChangeHandlerOptions as CreateInputOnChangeHandlerOptions<TypeName>,
input, input,
inputTestBatch, inputTestBatch,
onFirstRender, onFirstRender,
required: isRequired = INPUT_WITH_REF_DEFAULT_PROPS.required, required: isRequired = INPUT_WITH_REF_DEFAULT_PROPS.required,
valueType = INPUT_WITH_REF_DEFAULT_PROPS.valueType, valueType = INPUT_WITH_REF_DEFAULT_PROPS.valueType as TypeName,
}: InputWithRefProps<TypeName, InputComponent>, }: InputWithRefProps<TypeName, InputComponent>,
ref: ForwardedRef<InputForwardedRefContent<TypeName>>, ref: ForwardedRef<InputForwardedRefContent<TypeName>>,
) => { ) => {

@ -1,7 +1,7 @@
const MAP_TO_VALUE_CONVERTER: MapToValueConverter = { const MAP_TO_VALUE_CONVERTER: MapToValueConverter = {
boolean: (value) => Boolean(value),
number: (value) => parseInt(String(value), 10) || 0, number: (value) => parseInt(String(value), 10) || 0,
string: (value) => String(value), string: (value) => String(value),
undefined: () => undefined,
}; };
export default MAP_TO_VALUE_CONVERTER; export default MAP_TO_VALUE_CONVERTER;

@ -21,7 +21,7 @@ type CreateInputOnChangeHandlerOptions<
postSet?: (...args: InputOnChangeParameters) => void; postSet?: (...args: InputOnChangeParameters) => void;
preSet?: (...args: InputOnChangeParameters) => void; preSet?: (...args: InputOnChangeParameters) => void;
set?: MapToStateSetter[TypeName]; set?: MapToStateSetter[TypeName];
setType?: TypeName | 'string'; setType?: TypeName;
}; };
const createInputOnChangeHandler = const createInputOnChangeHandler =
@ -29,7 +29,7 @@ const createInputOnChangeHandler =
postSet, postSet,
preSet, preSet,
set, set,
setType = 'string', setType = 'string' as TypeName,
}: CreateInputOnChangeHandlerOptions<TypeName> = {}): MUIInputProps['onChange'] => }: CreateInputOnChangeHandlerOptions<TypeName> = {}): MUIInputProps['onChange'] =>
(event) => { (event) => {
const { const {

@ -6,6 +6,10 @@ declare type MapToType = {
undefined: undefined; undefined: undefined;
}; };
type ReducedMapToType = Pick<MapToType, 'boolean' | 'number' | 'string'>;
declare type MapToValueConverter = { declare type MapToValueConverter = {
[TypeName in keyof MapToType]: (value: unknown) => MapToType[TypeName]; [TypeName in keyof ReducedMapToType]: (
value: unknown,
) => ReducedMapToType[TypeName];
}; };

Loading…
Cancel
Save