-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
botonic-react: upgrade react-textarea-autosize (#2882)
## Description Upgrade version of react-textarea-autosize. This version is compatible with react 16, 17 or 18 Create a component Textarea to extract logic in a new file Create a component WebchatInputPanel that group all inputs: text input, add emoji, add file, send button ## Context More PRs merged in this PR: - #2883 - #2886
- Loading branch information
Showing
15 changed files
with
968 additions
and
664 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
packages/botonic-react/src/webchat/webchat-input-panel/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { INPUT } from '@botonic/core' | ||
import React, { useContext } from 'react' | ||
import { v4 as uuidv4 } from 'uuid' | ||
|
||
import { WEBCHAT } from '../../constants' | ||
import { WebchatContext } from '../../contexts' | ||
import { getFullMimeWhitelist } from '../../message-utils' | ||
import { DeviceAdapter } from '../devices/device-adapter' | ||
import { Attachment } from './attachment' | ||
import { EmojiPicker } from './emoji-picker' | ||
import { OpenedEmojiPicker } from './opened-emoji-picker' | ||
import { PersistentMenu } from './persistent-menu' | ||
import { SendButton } from './send-button' | ||
import { UserInputContainer } from './styles' | ||
import { Textarea } from './textarea' | ||
|
||
interface WebchatInputPanelProps { | ||
persistentMenu: any | ||
enableEmojiPicker: boolean | ||
enableAttachments: boolean | ||
handleAttachment: (event: any) => void | ||
textareaRef: React.MutableRefObject<HTMLTextAreaElement> | ||
deviceAdapter: DeviceAdapter | ||
onUserInput?: (event: any) => Promise<void> | ||
} | ||
|
||
export const WebchatInputPanel = ({ | ||
persistentMenu, | ||
enableEmojiPicker, | ||
enableAttachments, | ||
handleAttachment, | ||
textareaRef, | ||
deviceAdapter, | ||
onUserInput, | ||
}: WebchatInputPanelProps) => { | ||
const { | ||
getThemeProperty, | ||
sendText, | ||
togglePersistentMenu, | ||
toggleEmojiPicker, | ||
webchatState, | ||
} = useContext(WebchatContext) | ||
|
||
const handleSelectedEmoji = event => { | ||
textareaRef.current.value += event.emoji | ||
textareaRef.current.focus() | ||
} | ||
|
||
const handleEmojiClick = () => { | ||
toggleEmojiPicker(!webchatState.isEmojiPickerOpen) | ||
} | ||
|
||
const handleMenu = () => { | ||
togglePersistentMenu(!webchatState.isPersistentMenuOpen) | ||
} | ||
|
||
const sendTextAreaText = async () => { | ||
await sendText(textareaRef.current.value) | ||
textareaRef.current.value = '' | ||
} | ||
|
||
const sendChatEvent = async chatEvent => { | ||
const chatEventInput = { | ||
id: uuidv4(), | ||
type: INPUT.CHAT_EVENT, | ||
data: chatEvent, | ||
} | ||
if (onUserInput) { | ||
onUserInput({ | ||
user: webchatState.session.user, | ||
input: chatEventInput, | ||
session: webchatState.session, | ||
lastRoutePath: webchatState.lastRoutePath, | ||
}) | ||
} | ||
} | ||
|
||
return ( | ||
<UserInputContainer | ||
style={{ | ||
...getThemeProperty(WEBCHAT.CUSTOM_PROPERTIES.userInputStyle), | ||
}} | ||
className='user-input-container' | ||
> | ||
{webchatState.isEmojiPickerOpen && ( | ||
<OpenedEmojiPicker | ||
onEmojiClick={handleSelectedEmoji} | ||
onClick={handleEmojiClick} | ||
/> | ||
)} | ||
|
||
<PersistentMenu onClick={handleMenu} persistentMenu={persistentMenu} /> | ||
|
||
<Textarea | ||
deviceAdapter={deviceAdapter} | ||
persistentMenu={persistentMenu} | ||
textareaRef={textareaRef} | ||
sendChatEvent={sendChatEvent} | ||
sendTextAreaText={sendTextAreaText} | ||
/> | ||
|
||
<EmojiPicker | ||
enableEmojiPicker={enableEmojiPicker} | ||
onClick={handleEmojiClick} | ||
/> | ||
|
||
<Attachment | ||
enableAttachments={enableAttachments} | ||
onChange={handleAttachment} | ||
accept={getFullMimeWhitelist().join(',')} | ||
/> | ||
|
||
<SendButton onClick={sendTextAreaText} /> | ||
</UserInputContainer> | ||
) | ||
} |
Oops, something went wrong.