import * as React from 'react'; import classNames from 'classnames'; import { Emoji } from '../emoji/Emoji'; import { convertShortName } from '../emoji/lib'; import { Props as EmojiPickerProps } from '../emoji/EmojiPicker'; import { useRestoreFocus } from '../../util/hooks'; import { LocalizerType } from '../../types/Util'; export type RenderEmojiPickerProps = Pick & Pick & { ref: React.Ref; }; export type OwnProps = { i18n: LocalizerType; selected?: string; onClose?: () => unknown; onPick: (emoji: string) => unknown; renderEmojiPicker: (props: RenderEmojiPickerProps) => React.ReactElement; }; export type Props = OwnProps & Pick, 'style'>; const emojis = ['❤️', '👍', '👎', '😂', '😮', '😢']; export const ReactionPicker = React.forwardRef( ({ i18n, selected, onClose, onPick, renderEmojiPicker, style }, ref) => { const [pickingOther, setPickingOther] = React.useState(false); const focusRef = React.useRef(null); // Handle escape key React.useEffect(() => { const handler = (e: KeyboardEvent) => { if (onClose && e.key === 'Escape') { onClose(); } }; document.addEventListener('keydown', handler); return () => { document.removeEventListener('keydown', handler); }; }, [onClose]); // Handle EmojiPicker::onPickEmoji const onPickEmoji: EmojiPickerProps['onPickEmoji'] = React.useCallback( ({ shortName, skinTone }) => { onPick(convertShortName(shortName, skinTone)); }, [onPick] ); // Focus first button and restore focus on unmount useRestoreFocus(focusRef); const otherSelected = selected && !emojis.includes(selected); return pickingOther ? ( renderEmojiPicker({ onPickEmoji, onClose, style, ref }) ) : (
{emojis.map((emoji, index) => { const maybeFocusRef = index === 0 ? focusRef : undefined; return ( ); })}
); } );