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<
TypeName extends keyof InputWithRefTypeMap,
> = {
createInputOnChangeHandlerOptions?: Omit<
CreateInputOnChangeHandlerOptions<TypeName>,
'set'
>;
createInputOnChangeHandlerOptions?: CreateInputOnChangeHandlerOptions<TypeName>;
required?: boolean;
valueType?: TypeName | 'string';
valueType?: TypeName;
};
type InputWithRefOptionalPropsWithoutDefault = {
inputTestBatch?: InputTestBatch;
@ -76,12 +73,12 @@ const InputWithRef = forwardRef(
createInputOnChangeHandlerOptions: {
postSet: postSetAppend,
...restCreateInputOnChangeHandlerOptions
} = INPUT_WITH_REF_DEFAULT_PROPS.createInputOnChangeHandlerOptions,
} = INPUT_WITH_REF_DEFAULT_PROPS.createInputOnChangeHandlerOptions as CreateInputOnChangeHandlerOptions<TypeName>,
input,
inputTestBatch,
onFirstRender,
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>,
ref: ForwardedRef<InputForwardedRefContent<TypeName>>,
) => {

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

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

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

Loading…
Cancel
Save