You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
849 B
27 lines
849 B
2 years ago
|
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;
|