1.0.0 • Published 3 years ago
abc-chat v1.0.0
React Responsive Chat
React Responsive Chat provides a great Chat UI for your React Web App
Installation
Install react-responsive-chat with npm
npm install react-responsive-chatimport React,{useState, useEffect} from 'react';
import ReactResponsiveChat from 'react-responsive-chat';
function App(){
const user={
fullName:"John Smith",
photo:"...",
_id:"..."
}
const [chats, setChats]=useState([]);
const [isLoadingChats, setIsLoadingChats]=useState(false);
const [chatRooms, setChatRooms]=useState([]);
const [rooms, setRooms]=useState([]);
const [isLoadingChatRooms, setIsLoadingChatRooms]=useState(false);
const [selectedChatRoom, setSelectedChatRoom]=useState(null);
<!--Required For Load More Case-->
const [totalRooms, setTotalRooms] = useState(0);
const [roomsPageNo, setRoomsPageNo] = useState(1);
const [totalChats, setTotalChats] = useState(0);
const [chatsPageNo, setChatsPageNo] = useState(1);
const pageLimit = 10;
useEffect(() => {
if(selectedChatRoom !== null){
setChats([
{
_id: 1,
text: 'Hello developer',
createdAt: new Date(),
user: {
_id: 2,
name: 'React Responsive Chat',
avatar: 'https://...',
},
},
])
}
}, [selectedChatRoom])
return(
<ReactResponsiveChat
chats={chats}
chatRooms={chatRooms}
selectedChatRoom={selectedChatRoom}
onChatRoomSelection={(e)=>setSelectedChatRoom(e)}
user={user}
isLoadingChats={isLoadingChats}
isLoadingChatRooms={isLoadingChatRooms}
onSend={(e)=> {...}}
<!--Required For Load More Case-->
chatsPage={chatsPageNo}
chatRoomsPage={roomsPageNo}
isLoadMoreChats={Math.ceil(totalChats / pageLimit) > chatsPageNo}
isLoadMoreChatRooms={Math.ceil(totalRooms / pageLimit) > roomsPageNo}
onLoadMoreChats={async (e) => {
const pg = pageNo + 1;
await chatsApiCall(pg); // Note: Page must be updated after Api call
setPageNo(pg);
}}
onLoadMoreChatRooms={async () => {
const pg = pageNo + 1;
await chatRoomsApiCall(pg); // Note: Page must be updated after Api call
setRoomsPageNo(pg);
}}
/>
)}Props
| Name | Type | Description | Default |
|---|---|---|---|
| chatRooms | Array | Used for Chat Rooms Display | [] |
| onLoadMoreChatRooms | Promise | Must be promise function | undefined |
| renderChatRoomsLoader | Function | Must be React Component | default |
| selectedChatRoom | Object | Will be Selected Chat Room Object | undefined |
| onChatRoomSelection | Function | Will be Call on Selection of Chat Room | undefined |
| isLoadingChatRooms | Boolean | Must be Boolean value | false |
| isLoadMoreChatRooms | Boolean | Must be Boolean value | false |
| renderRoomsHeader | Function | Must be React Component | default |
| chats | Array | Used for Chats Display | [] |
| onLoadMoreChats | Promise | Must be promise function | undefined |
| renderChatsLoader | Function | Must be React Component | default |
| isLoadingChats | Boolean | Must be Boolean value | false |
| isLoadMoreChats | Boolean | Must be Boolean value | false |
| renderChatsHeader | Function | Must be React Component | default |
| onSend | Function | Must be promise function | undefined |
| onMoveToUnreadChat | Function | Must be function | undefined |
| containerClassName | String | Must be class | undefined |
| isShowUnReadChatsCount | Boolean | Must be Boolean value | false |
| totalUnReadCount | Number | Must be Number | 0 |
| user | Object | Must be Object with keys (Mentioned Below) | undefined |
| onBack | Function | Must be Function | default |
| isTyping | Boolean | Must be Boolean value | false |
| placeholder | String | Placeholder is for Chat Input | Enter message here... |
| unReadChatId | String | Must be the UnRead Chat Id of Chat Room | undefined |
| renderChatsHeader | Function | Must be React Component | default |
| ref | Ref | Must be React Ref Hook | undefined |
| renderChatTimeSeparator | Function | Must be React Component | default |
| chatsLimit | Number | Must be Number | 10 |
| chatsPage | Number | Must be Number | undefined |
| onTyping | Function | Must be Function | undefined |
| colors | Object | Must be Object with keys (Mentioned Below) | undefined |
| ref | Object | Must be React Ref Object | null |
chats(Array) - Chats to displayisTyping(Bool) - Typing Indicator state; defaultfalseplaceholder(String) - Placeholder whentextis empty; default is'Type a message...'user(Object) - User sending the messages:{ _id, name, avatar }onSend(Function) - Used when sending a messagechatRooms(Array) - Chat Rooms to displaycolors(Object) - Colors prop use for adjust UIonBack(function) - OnBackrequiredfor mobile devicesselectedChatRoom(Object) - selectedChatRoomrequiredfor openChat Room(note: It must benullby default)setSelectedChatRoom(Function) - setSelectedChatRoom used for openChat RoomonLoadMoreChats(Function) - onLoadMoreChats work on scroll up to load chatsroomsLoader(Function) - Custom Loader display when loadingChat RoomschatsLoader(Function) - Custom Loader display when loadingChatsscrollThreshold(Number) - Scroll Threshold value by default0.8unReadChatId(String) - Must be_idof chatisLoadingChatRooms(Bool) - Default value is falseisLoadingChats(Bool) - Default value is falsetotalChatsCount(Number) - It must be thetotal chats countof theChat RoomtotalUnReadChatsCount(Number) - It must be thetotal unread chats countof theChat RoomisShowUnReadChatsCount(Bool) - Default value is falseonMoveToUnreadChat(Function) - This function triggers after scroll to the unread chat
Examples
Message Object
const message={
_id:123,
createdAt:new Date(),
text:'Test Message',
user:{
_id:34,
avatar:'https://...',
name:"Test User"
}
}Chat Room Object
const chatRoom={
_id:123,
createdAt:new Date(),
user1:{
_id:34,
avatar:'https://...',
name:"Test User 1"
},
user2:{
_id:35,
avatar:'https://...',
name:"Test User 2"
},
lastMessage:{
_id:221,
createdAt:new Date(),
text:'Test Last Message',
user:{
_id:34,
avatar:'https://...',
name:"Test User 1"
}
},
unReadCount:2
}Colors Object
const customColors = {
chatRoom: {
name: '#000000',
message: 'grey',
time: 'black',
box: '#ffffff',
separator: '#ae93ff',
hoverBg: '#af94ff3b',
hoverText: '#fff',
unseenBg: '#ae93ff',
unseenText: 'white',
},
chatRoomSelected: {
name: '#000000',
message: 'grey',
time: '#ae93ff',
box: '#af94ff1a',
separator: '#ae93ff',
unseenBg: '#ae93ff',
unseenText: 'white',
},
myMessage: {
text: 'white',
time: 'white',
bg: '#ae93ff'
},
otherMessage: {
text: 'black',
time: 'black',
bg: '#d8d8d8',
},
chatRoomsHeader: {
bg: '#ae93ff',
text: 'white'
},
chatsHeader: {
bg: '#ae93ff',
text: 'white'
},
chatsFooter: {
bg: '#0000ff17',
btnBg: '#ae93ff',
btnText: 'white',
inputBg: '#fff',
inputText: 'black',
inputScrollTrack: '#d8d8d87d',
inputScrollBar: '#ae93ff',
},
chatsContent: {
bg: '#fff',
timeIndicator: '#d8d8d8',
timeText: 'black'
},
chatsScroller: {
track: '#d8d8d87d',
bar: '#ae93ff'
}
}Contributing
Contributions are always welcome!
See contributing.md for ways to get started.
Please adhere to this project's code of conduct.
1.0.0
3 years ago