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,
} from '../lib/createInputOnChangeHandler';
type InputWithRefOptionalProps<TypeName extends keyof MapToType> = {
type InputWithRefTypeMap = Pick<MapToType, 'number' | 'string'>;
type InputWithRefOptionalProps<TypeName extends keyof InputWithRefTypeMap> = {
createInputOnChangeHandlerOptions?: Omit<
CreateInputOnChangeHandlerOptions<TypeName>,
'set'
@ -21,22 +23,21 @@ type InputWithRefOptionalProps<TypeName extends keyof MapToType> = {
};
type InputWithRefProps<
TypeName extends keyof MapToType,
TypeName extends keyof InputWithRefTypeMap,
InputComponent extends ReactElement,
> = InputWithRefOptionalProps<TypeName> & {
input: InputComponent;
};
type InputForwardedRefContent<TypeName extends keyof MapToType> = {
type InputForwardedRefContent<TypeName extends keyof InputWithRefTypeMap> = {
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 MapToType, InputComponent extends ReactElement>(
<
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<MapToType[TypeName]>(
const [value, setValue] = useState<InputWithRefTypeMap[TypeName]>(
initValue ?? MAP_TO_INITIAL_VALUE[valueType],
) as [MapToType[TypeName], MapToStateSetter[TypeName]];
) as [InputWithRefTypeMap[TypeName], MapToStateSetter[TypeName]];
const [isChangedByUser, setIsChangedByUser] = useState<boolean>(false);
const onChange = createInputOnChangeHandler<TypeName>({

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

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

Loading…
Cancel
Save