fix(striker-ui): use subset of type map

main
Tsu-ba-me 2 years ago
parent c017c5135e
commit b470070960
  1. 22
      striker-ui/components/InputWithRef.tsx
  2. 14
      striker-ui/lib/createInputOnChangeHandler.ts
  3. 10
      striker-ui/lib/isEmpty.ts

@ -12,7 +12,9 @@ import createInputOnChangeHandler, {
MapToStateSetter, MapToStateSetter,
} from '../lib/createInputOnChangeHandler'; } from '../lib/createInputOnChangeHandler';
type InputWithRefOptionalProps<TypeName extends keyof MapToType> = { type InputWithRefTypeMap = Pick<MapToType, 'number' | 'string'>;
type InputWithRefOptionalProps<TypeName extends keyof InputWithRefTypeMap> = {
createInputOnChangeHandlerOptions?: Omit< createInputOnChangeHandlerOptions?: Omit<
CreateInputOnChangeHandlerOptions<TypeName>, CreateInputOnChangeHandlerOptions<TypeName>,
'set' 'set'
@ -21,22 +23,21 @@ type InputWithRefOptionalProps<TypeName extends keyof MapToType> = {
}; };
type InputWithRefProps< type InputWithRefProps<
TypeName extends keyof MapToType, TypeName extends keyof InputWithRefTypeMap,
InputComponent extends ReactElement, InputComponent extends ReactElement,
> = InputWithRefOptionalProps<TypeName> & { > = InputWithRefOptionalProps<TypeName> & {
input: InputComponent; input: InputComponent;
}; };
type InputForwardedRefContent<TypeName extends keyof MapToType> = { type InputForwardedRefContent<TypeName extends keyof InputWithRefTypeMap> = {
getIsChangedByUser?: () => boolean; getIsChangedByUser?: () => boolean;
getValue?: () => MapToType[TypeName]; getValue?: () => InputWithRefTypeMap[TypeName];
setValue?: MapToStateSetter[TypeName]; setValue?: MapToStateSetter[TypeName];
}; };
const MAP_TO_INITIAL_VALUE: MapToType = { const MAP_TO_INITIAL_VALUE: InputWithRefTypeMap = {
number: 0, number: 0,
string: '', string: '',
undefined,
}; };
const INPUT_WITH_REF_DEFAULT_PROPS: Required< const INPUT_WITH_REF_DEFAULT_PROPS: Required<
@ -47,7 +48,10 @@ const INPUT_WITH_REF_DEFAULT_PROPS: Required<
}; };
const InputWithRef = forwardRef( const InputWithRef = forwardRef(
<TypeName extends keyof MapToType, InputComponent extends ReactElement>( <
TypeName extends keyof InputWithRefTypeMap,
InputComponent extends ReactElement,
>(
{ {
createInputOnChangeHandlerOptions: { createInputOnChangeHandlerOptions: {
postSet: postSetAppend, postSet: postSetAppend,
@ -62,9 +66,9 @@ const InputWithRef = forwardRef(
props: { onChange: initOnChange, value: initValue, ...restInitProps }, props: { onChange: initOnChange, value: initValue, ...restInitProps },
} = input; } = input;
const [value, setValue] = useState<MapToType[TypeName]>( const [value, setValue] = useState<InputWithRefTypeMap[TypeName]>(
initValue ?? MAP_TO_INITIAL_VALUE[valueType], initValue ?? MAP_TO_INITIAL_VALUE[valueType],
) as [MapToType[TypeName], MapToStateSetter[TypeName]]; ) as [InputWithRefTypeMap[TypeName], MapToStateSetter[TypeName]];
const [isChangedByUser, setIsChangedByUser] = useState<boolean>(false); const [isChangedByUser, setIsChangedByUser] = useState<boolean>(false);
const onChange = createInputOnChangeHandler<TypeName>({ const onChange = createInputOnChangeHandler<TypeName>({

@ -3,15 +3,21 @@ import { Dispatch, SetStateAction } from 'react';
import MAP_TO_VALUE_CONVERTER from './consts/MAP_TO_VALUE_CONVERTER'; import MAP_TO_VALUE_CONVERTER from './consts/MAP_TO_VALUE_CONVERTER';
type CreateInputOnChangeHandlerTypeMap = Pick<MapToType, 'number' | 'string'>;
type InputOnChangeParameters = Parameters< type InputOnChangeParameters = Parameters<
Exclude<MUIInputProps['onChange'], undefined> Exclude<MUIInputProps['onChange'], undefined>
>; >;
type MapToStateSetter = { type MapToStateSetter = {
[TypeName in keyof MapToType]: Dispatch<SetStateAction<MapToType[TypeName]>>; [TypeName in keyof CreateInputOnChangeHandlerTypeMap]: Dispatch<
SetStateAction<CreateInputOnChangeHandlerTypeMap[TypeName]>
>;
}; };
type CreateInputOnChangeHandlerOptions<TypeName extends keyof MapToType> = { type CreateInputOnChangeHandlerOptions<
TypeName extends keyof CreateInputOnChangeHandlerTypeMap,
> = {
postSet?: (...args: InputOnChangeParameters) => void; postSet?: (...args: InputOnChangeParameters) => void;
preSet?: (...args: InputOnChangeParameters) => void; preSet?: (...args: InputOnChangeParameters) => void;
set?: MapToStateSetter[TypeName]; set?: MapToStateSetter[TypeName];
@ -19,7 +25,7 @@ type CreateInputOnChangeHandlerOptions<TypeName extends keyof MapToType> = {
}; };
const createInputOnChangeHandler = const createInputOnChangeHandler =
<TypeName extends keyof MapToType>({ <TypeName extends keyof CreateInputOnChangeHandlerTypeMap>({
postSet, postSet,
preSet, preSet,
set, set,
@ -31,7 +37,7 @@ const createInputOnChangeHandler =
} = event; } = event;
const postConvertValue = MAP_TO_VALUE_CONVERTER[setType]( const postConvertValue = MAP_TO_VALUE_CONVERTER[setType](
value, value,
) as MapToType[TypeName]; ) as CreateInputOnChangeHandlerTypeMap[TypeName];
preSet?.call(null, event); preSet?.call(null, event);
set?.call(null, postConvertValue); set?.call(null, postConvertValue);

@ -1,6 +1,8 @@
type IsEmptyTypeMap = Pick<MapToType, 'number' | 'string' | 'undefined'>;
type MapToValueIsEmptyFunction = { type MapToValueIsEmptyFunction = {
[TypeName in keyof MapToType]: ( [TypeName in keyof IsEmptyTypeMap]: (
value: MapToType[TypeName] | undefined, value: IsEmptyTypeMap[TypeName],
) => boolean; ) => boolean;
}; };
@ -10,8 +12,8 @@ const MAP_TO_VALUE_IS_EMPTY_FUNCTION: MapToValueIsEmptyFunction = {
undefined: () => true, undefined: () => true,
}; };
const isEmpty = <TypeName extends keyof MapToType>( const isEmpty = <TypeName extends keyof IsEmptyTypeMap>(
values: Array<MapToType[TypeName] | undefined>, values: Array<IsEmptyTypeMap[TypeName]>,
{ not, fn = 'every' }: { not?: boolean; fn?: 'every' | 'some' } = {}, { not, fn = 'every' }: { not?: boolean; fn?: 'every' | 'some' } = {},
): boolean => ): boolean =>
values[fn]((value) => { values[fn]((value) => {

Loading…
Cancel
Save