react bell chat
v1.0.53
用于构建聊天 UI 的 React 组件库。
现场演示
请记住,该项目仍处于开发的早期阶段。如果您遇到错误或有功能请求,请创建问题。
npm install react-bell-chat --save
或者
yarn add react-bell-chat
import { ChatFeed } from 'react-bell-chat'
// Your code stuff...
render ( ) {
return (
// Your JSX...
< ChatFeed
messages = { this . state . messages } // Array: list of message objects
authors = { this . state . authors } // Array: list of authors
yourAuthorId = { 2 } // Number: Your author id (corresponds with id from list of authors)
/ >
// Your JSX...
)
}
最低限度是提供消息和作者的列表,请查看示例:
//...
this . state = {
messages : [
{
id : 1 ,
authorId : 1 ,
message : "Sample message" ,
createdOn : new Date ( ) ,
isSend : true
} ,
{
id : 2 ,
authorId : 2 ,
message : "Second sample message" ,
createdOn : new Date ( ) ,
isSend : false
} ,
] ,
authors : [
{
id : 1 ,
name : 'Mark' ,
isTyping : true ,
lastSeenMessageId : 1 ,
bgImageUrl : undefined
} ,
{
id : 2 ,
name : 'Peter' ,
isTyping : false ,
lastSeenMessageId : 2 ,
bgImageUrl : undefined
}
]
} ;
//...
作者的完整道具:
打字稿
export interface Author {
id: number;
name: string;
avatarName?: string;
lastSeenAvatarName?: string;
isTyping?: boolean;
lastSeenMessageId?: number;
bgImageUrl?: number;
}
消息的完整道具:
打字稿
export interface Message {
id?: number;
authorId?: number; // If undefined, message is considered to be "System message"
message: string;
createdOn?: Date;
isSend?: boolean;
}
Api 是作为 ChatFeed 组件的引用获取的。它分为两部分,feedApi 和scrollApi。 Ref 为您提供了这样的对象:
interface ChatFeedApi {
onMessageSend : ( ) => void ; // Should be called when user sends a message (this scrolls the component down)
scrollApi : ChatScrollAreaApi ;
}
滚动 api 在哪里
interface ChatScrollAreaApi {
scrollToBottom : ( behavior ?: ScrollBehavior ) => void ;
scrollTo : ( top : number ) => void ;
scrolledToBottom : ( ) => boolean ; // Check if we are scrolled to bottom
scrolledToLoadThreshold : ( ) => boolean ; // Check if we are scrolled to threshold where we need to load messages
}
export interface ChatFeedProps {
// Structural props
className ?: string ;
// Functional props
messages : Message [ ] ;
authors : Author [ ] ;
yourAuthorId : number ;
hasOldMessages ?: boolean ;
loadOldMessagesThreshold ?: number ;
// Visual props
bubblesCentered ?: boolean ;
bubbleStyles ?: ChatBubbleStyles ;
maxHeight ?: string | number ;
minHeight ?: string | number ;
// Switches
showDateRow ?: boolean ;
showRecipientAvatar ?: boolean ;
showRecipientLastSeenMessage ?: boolean ;
showIsTyping ?: boolean ;
showLoadingMessages ?: boolean ;
// Extra container styles for custom components
showRecipientAvatarChatMessagesStyle ?: React . CSSProperties ;
showRecipientLastSeenMessageChatMessagesStyle ?: React . CSSProperties ;
showIsTypingChatMessagesStyle ?: React . CSSProperties ;
// Custom components
customLoadingMessages ?: ( props : LoadingMessagesProps ) => JSX . Element ;
customChatBubble ?: ( props : ChatBubbleProps ) => JSX . Element ;
customSystemChatBubble ?: ( props : ChatBubbleProps ) => JSX . Element ;
customAvatar ?: ( props : AvatarProps ) => JSX . Element ;
customScrollArea ?: ( props : ChatScrollAreaProps ) => JSX . Element ;
customIsTyping ?: ( props : ChatScrollAreaProps ) => JSX . Element ;
customLastSeenAvatar ?: ( props : LastSeenAvatarProps ) => JSX . Element ;
customDateRow ?: ( props : DateRowProps ) => JSX . Element ;
// Callbacks
onLoadOldMessages ?: ( ) => Promise < void > ; // Make sure to return promise that only resolves after state is updated.
ref ?: ( api : ChatFeedApi ) => void ;
}
大多数 UI 都可以通过注入自定义组件来定制。这些是纯组件,库向它们注入道具,以便您可以自定义几乎任何东西。
问:每次我在父组件上使用 setState 时,我的聊天都会自动向上/向下滚动。
答:确保为自定义组件提供 const 表达式。不是内联函数。
贡献始终受到欢迎和鼓励。
npm run start
该库最初是来自 https://github.com/brandonmowat/react-chat-ui 的一个分支