From b470070960aa370ce2d1e158041e2637cfd698ec Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Wed, 7 Sep 2022 16:32:55 -0400 Subject: [PATCH] fix(striker-ui): use subset of type map --- striker-ui/components/InputWithRef.tsx | 22 ++++++++++++-------- striker-ui/lib/createInputOnChangeHandler.ts | 14 +++++++++---- striker-ui/lib/isEmpty.ts | 10 +++++---- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/striker-ui/components/InputWithRef.tsx b/striker-ui/components/InputWithRef.tsx index eb3bf9e2..06b532b9 100644 --- a/striker-ui/components/InputWithRef.tsx +++ b/striker-ui/components/InputWithRef.tsx @@ -12,7 +12,9 @@ import createInputOnChangeHandler, { MapToStateSetter, } from '../lib/createInputOnChangeHandler'; -type InputWithRefOptionalProps = { +type InputWithRefTypeMap = Pick; + +type InputWithRefOptionalProps = { createInputOnChangeHandlerOptions?: Omit< CreateInputOnChangeHandlerOptions, 'set' @@ -21,22 +23,21 @@ type InputWithRefOptionalProps = { }; type InputWithRefProps< - TypeName extends keyof MapToType, + TypeName extends keyof InputWithRefTypeMap, InputComponent extends ReactElement, > = InputWithRefOptionalProps & { input: InputComponent; }; -type InputForwardedRefContent = { +type InputForwardedRefContent = { getIsChangedByUser?: () => boolean; - getValue?: () => MapToType[TypeName]; + getValue?: () => InputWithRefTypeMap[TypeName]; setValue?: MapToStateSetter[TypeName]; }; -const MAP_TO_INITIAL_VALUE: MapToType = { +const MAP_TO_INITIAL_VALUE: InputWithRefTypeMap = { number: 0, string: '', - undefined, }; const INPUT_WITH_REF_DEFAULT_PROPS: Required< @@ -47,7 +48,10 @@ const INPUT_WITH_REF_DEFAULT_PROPS: Required< }; const InputWithRef = forwardRef( - ( + < + TypeName extends keyof InputWithRefTypeMap, + InputComponent extends ReactElement, + >( { createInputOnChangeHandlerOptions: { postSet: postSetAppend, @@ -62,9 +66,9 @@ const InputWithRef = forwardRef( props: { onChange: initOnChange, value: initValue, ...restInitProps }, } = input; - const [value, setValue] = useState( + const [value, setValue] = useState( initValue ?? MAP_TO_INITIAL_VALUE[valueType], - ) as [MapToType[TypeName], MapToStateSetter[TypeName]]; + ) as [InputWithRefTypeMap[TypeName], MapToStateSetter[TypeName]]; const [isChangedByUser, setIsChangedByUser] = useState(false); const onChange = createInputOnChangeHandler({ diff --git a/striker-ui/lib/createInputOnChangeHandler.ts b/striker-ui/lib/createInputOnChangeHandler.ts index 5d3ac920..1103dbc6 100644 --- a/striker-ui/lib/createInputOnChangeHandler.ts +++ b/striker-ui/lib/createInputOnChangeHandler.ts @@ -3,15 +3,21 @@ import { Dispatch, SetStateAction } from 'react'; import MAP_TO_VALUE_CONVERTER from './consts/MAP_TO_VALUE_CONVERTER'; +type CreateInputOnChangeHandlerTypeMap = Pick; + type InputOnChangeParameters = Parameters< Exclude >; type MapToStateSetter = { - [TypeName in keyof MapToType]: Dispatch>; + [TypeName in keyof CreateInputOnChangeHandlerTypeMap]: Dispatch< + SetStateAction + >; }; -type CreateInputOnChangeHandlerOptions = { +type CreateInputOnChangeHandlerOptions< + TypeName extends keyof CreateInputOnChangeHandlerTypeMap, +> = { postSet?: (...args: InputOnChangeParameters) => void; preSet?: (...args: InputOnChangeParameters) => void; set?: MapToStateSetter[TypeName]; @@ -19,7 +25,7 @@ type CreateInputOnChangeHandlerOptions = { }; const createInputOnChangeHandler = - ({ + ({ postSet, preSet, set, @@ -31,7 +37,7 @@ const createInputOnChangeHandler = } = event; const postConvertValue = MAP_TO_VALUE_CONVERTER[setType]( value, - ) as MapToType[TypeName]; + ) as CreateInputOnChangeHandlerTypeMap[TypeName]; preSet?.call(null, event); set?.call(null, postConvertValue); diff --git a/striker-ui/lib/isEmpty.ts b/striker-ui/lib/isEmpty.ts index 5e3f3608..d2fd1970 100644 --- a/striker-ui/lib/isEmpty.ts +++ b/striker-ui/lib/isEmpty.ts @@ -1,6 +1,8 @@ +type IsEmptyTypeMap = Pick; + type MapToValueIsEmptyFunction = { - [TypeName in keyof MapToType]: ( - value: MapToType[TypeName] | undefined, + [TypeName in keyof IsEmptyTypeMap]: ( + value: IsEmptyTypeMap[TypeName], ) => boolean; }; @@ -10,8 +12,8 @@ const MAP_TO_VALUE_IS_EMPTY_FUNCTION: MapToValueIsEmptyFunction = { undefined: () => true, }; -const isEmpty = ( - values: Array, +const isEmpty = ( + values: Array, { not, fn = 'every' }: { not?: boolean; fn?: 'every' | 'some' } = {}, ): boolean => values[fn]((value) => {