feat(striker-ui): add buildMapToMessageSetter()

main
Tsu-ba-me 2 years ago
parent f87379fa98
commit fb16016ad0
  1. 26
      striker-ui/lib/buildMapToMessageSetter.ts
  2. 1
      striker-ui/types/MapToInputTestID.d.ts
  3. 3
      striker-ui/types/MessageSetterFunction.d.ts

@ -0,0 +1,26 @@
import { MutableRefObject } from 'react';
import { MessageGroupForwardedRefContent } from '../components/MessageGroup';
type BuildMapToMessageSetterReturnType<T extends MapToInputTestID> = {
[MessageSetterID in keyof T]: MessageSetterFunction;
};
const buildMapToMessageSetter = <T extends MapToInputTestID>(
mapToID: T,
messageGroupRef: MutableRefObject<MessageGroupForwardedRefContent>,
): BuildMapToMessageSetterReturnType<T> =>
Object.entries(mapToID).reduce<BuildMapToMessageSetterReturnType<T>>(
(previous, [key, id]) => {
const setter: MessageSetterFunction = (message?) => {
messageGroupRef.current.setMessage?.call(null, id, message);
};
previous[key as keyof T] = setter;
return previous;
},
{} as BuildMapToMessageSetterReturnType<T>,
);
export default buildMapToMessageSetter;

@ -0,0 +1 @@
type MapToInputTestID = { [key: string]: string };

@ -0,0 +1,3 @@
type MessageSetterFunction = (
message?: import('../components/MessageBox').Message,
) => void;
Loading…
Cancel
Save