refactor: extract common logic for ChatInputEmotion with BaseChatInputButton

pull/90/head
moonrailgun 2 years ago
parent 3be4cdc439
commit 4aae4779a1

@ -0,0 +1,33 @@
import { Popover } from 'antd';
import React, { useState } from 'react';
import { Icon } from 'tailchat-design';
interface BaseChatInputButtonProps {
icon: string;
popoverContent: (ctx: { hidePopover: () => void }) => React.ReactElement;
}
export const BaseChatInputButton: React.FC<BaseChatInputButtonProps> =
React.memo((props) => {
const [visible, setVisible] = useState(false);
return (
<Popover
open={visible}
onOpenChange={setVisible}
content={() =>
props.popoverContent({
hidePopover: () => {
setVisible(false);
},
})
}
overlayClassName="chat-message-input_action-popover"
showArrow={false}
placement="topRight"
trigger={['click']}
>
<Icon className="text-2xl cursor-pointer" icon={props.icon} />
</Popover>
);
});
BaseChatInputButton.displayName = 'BaseChatInputButton';

@ -1,41 +1,25 @@
import { Icon } from 'tailchat-design';
import { Popover } from 'antd';
import React, { useCallback, useState } from 'react';
import React from 'react';
import { useChatInputActionContext } from './context';
import { EmojiPanel } from '@/components/Emoji';
import { BaseChatInputButton } from './BaseChatInputButton';
import './Emotion.less';
export const ChatInputEmotion: React.FC = React.memo(() => {
const actionContext = useChatInputActionContext();
const { appendMsg } = actionContext;
const [visible, setVisible] = useState(false);
const handleSelect = useCallback(
async (code: string) => {
appendMsg(code);
setVisible(false);
},
[appendMsg]
);
const content = <EmojiPanel onSelect={handleSelect} />;
return (
<Popover
open={visible}
onOpenChange={setVisible}
content={content}
overlayClassName="chat-message-input_action-popover"
showArrow={false}
placement="topRight"
trigger={['click']}
>
<Icon
className="text-2xl cursor-pointer"
<BaseChatInputButton
icon="mdi:emoticon-happy-outline"
popoverContent={({ hidePopover }) => (
<EmojiPanel
onSelect={(code) => {
appendMsg(code);
hidePopover();
}}
/>
)}
/>
</Popover>
);
});
ChatInputEmotion.displayName = 'ChatInputEmotion';

Loading…
Cancel
Save