@assisthq/tagging-utils
Version:
43 lines (35 loc) • 1.09 kB
text/typescript
import { MutableRefObject } from "react";
import { DispatchMentions, DispatchTextState, MentionEntity } from "../types";
import createMentionEntity from "./createMentionEntity";
import updateTextWithMentionTag from "./updateTextWithMentionTag";
interface User {
email: string;
firstname: string;
lastname: string;
}
interface Focusable {
focus: () => void;
}
function createOnUserTaggedHandler(
setText: DispatchTextState,
mentions: MentionEntity[],
setMentions: DispatchMentions
) {
return <T extends Focusable>() => {
return function onUserTag(
user: User,
text: string,
textInput: MutableRefObject<T>
) {
const email = user.email;
let name = `${user.firstname} ${user.lastname}`;
const postText = updateTextWithMentionTag(text, name);
const textlen = postText.length;
const userMentionEntity = createMentionEntity(textlen, name, email);
setText(postText);
setMentions([...mentions, userMentionEntity]);
textInput.current.focus();
};
};
}
export default createOnUserTaggedHandler;