Skip to main content
Version: v5

Users

Overview

The Users is a Component, showcasing an accessible list of all available users. It provides an integral search functionality, allowing you to locate any specific user swiftly and easily. For each user listed, the widget displays the user's name by default, in conjunction with their avatar when available. Furthermore, it includes a status indicator, visually informing you whether a user is currently online or offline.

Image

The Users component is composed of the following BaseComponents:

ComponentsDescription
CometChatListA reusable container component having title, search box, customisable background and a list view.
CometChatListItemA component that renders data obtained from a User object on a Tile having a title, subtitle, leading and trailing view.

Usage

Integration

The following code snippet illustrates how you can directly incorporate the Users component into your Application.

import { CometChatUsers } from "@cometchat/chat-uikit-react";

function UsersDemo() {
return <CometChatUsers />;
}

export default UsersDemo;

Actions

Actions dictate how a component functions. They are divided into two types: Predefined and User-defined. You can override either type, allowing you to tailor the behavior of the component to fit your specific needs.

1. onSelect

The onSelect action is activated when you select the done icon while in selection mode. This returns a list of all the users that you have selected.

This action does not come with any predefined behavior. However, you have the flexibility to override this event and tailor it to suit your needs using the following code snippet.

import { CometChatUsers, SelectionMode } from "@cometchat/chat-uikit-react";

function UsersDemo() {
function handleOnSelect(users: CometChat.User, selected: boolean): void {
console.log(users);
//your custom onSelect actions
}

return (
<CometChatUsers
selectionMode={SelectionMode.multiple}
onSelect={handleOnSelect}
/>
);
}

export default UsersDemo;
2. onItemClick

The OnItemClick event is activated when you click on the UserList item. This action does not come with any predefined behavior. However, you have the flexibility to override this event and tailor it to suit your needs using the following code snippet.

import { CometChatUsers } from "@cometchat/chat-uikit-react";

function UsersDemo() {
function handleOnItemClick(user: CometChat.User): void {
console.log(user, "your custom on item click action");
}
return <CometChatUsers onItemClick={handleOnItemClick} />;
}

export default UsersDemo;
3. onEmpty

This action allows you to specify a callback function to be executed when a certain condition, typically the absence of data or content, is met within the component or element.

import { CometChatUsers } from "@cometchat/chat-uikit-react";

function UsersDemo() {
function handleOnEmpty(): void {
console.log("your custom on Empty action");
}
return <CometChatUsers onEmpty={handleOnEmpty} />;
}

export default UsersDemo;
4. onError

This action doesn't change the behavior of the component but rather listens for any errors that occur in the User component.

import { CometChatUsers } from "@cometchat/chat-uikit-react";

function UsersDemo() {
const handleOnError = (error: CometChat.CometChatException) => {
console.log("Your custom on error actions");
};
return <CometChatUsers onError={handleOnError} />;
}

export default UsersDemo;

Filters

Filters allow you to customize the data displayed in a list within a Component. You can filter the list based on your specific criteria, allowing for a more customized. Filters can be applied using RequestBuilders of Chat SDK.

1. UserRequestBuilder

The UserRequestBuilder enables you to filter and customize the user list based on available parameters in UserRequestBuilder. This feature allows you to create more specific and targeted queries when fetching users. The following are the parameters available in UserRequestBuilder

MethodsTypeDescription
setLimitnumbersets the number users that can be fetched in a single request, suitable for pagination
setSearchKeywordStringused for fetching users matching the passed string
hideBlockedUsersbooleanused for fetching all those users who are not blocked by the logged in user
friendsOnlybooleanused for fetching only those users in which logged in user is a member
setRolesList<String>used for fetching users containing the passed tags
setTagsList<String>used for fetching users containing the passed tags
withTagsbooleanused for fetching users containing tags
setStatusStringused for fetching users by their status online or offline
setUIDsList<String>used for fetching users containing the passed users

Example

In the example below, we are applying a filter to the UserList based on Friends.

import { CometChat } from "@cometchat/chat-sdk-javascript";
import { CometChatUsers } from "@cometchat/chat-uikit-react";

function UsersDemo() {
return (
<CometChatUsers
usersRequestBuilder={new CometChat.UsersRequestBuilder().friendsOnly(
true
)}
/>
);
}

export default UsersDemo;
2. SearchRequestBuilder

The SearchRequestBuilder uses UserRequestBuilder enables you to filter and customize the search list based on available parameters in UserRequestBuilder. This feature allows you to keep uniformity between the displayed UserList and searched UserList.

Example

import { CometChat } from "@cometchat/chat-sdk-javascript";
import { CometChatUsers } from "@cometchat/chat-uikit-react";

function UsersDemo() {
return (
<CometChatUsers
searchRequestBuilder={new CometChat.UsersRequestBuilder().setSearchKeyword(
"**"
)}
/>
);
}

export default UsersDemo;

Events

Events are emitted by a Component. By using event you can extend existing functionality. Being global events, they can be applied in Multiple Locations and are capable of being Added or Removed.

To handle events supported by Users you have to add corresponding listeners by using CometChatUserEvents

The Users component does not produce any events directly.


Customization

To fit your app's design requirements, you can customize the appearance of the Users component. We provide exposed methods that allow you to modify the experience and behavior according to your specific needs.

Style

Using CSS you can customize the look and feel of the component in your app like the color, size, shape, and fonts.

Example

Image
import { CometChatUsers } from "@cometchat/chat-uikit-react";

function UsersDemo() {
return <CometChatUsers showSectionHeader={false} />;
}

export default UsersDemo;

Functionality

These are a set of small functional customizations that allow you to fine-tune the overall experience of the component. With these, you can change text, set custom icons, and toggle the visibility of UI elements.

import { CometChatUsers, TitleAlignment } from "@cometchat/chat-uikit-react";

function UsersDemo() {
return (
<CometChatUsers
title="Your Custom Title"
showSectionHeader={true}
tileAlignment={TitleAlignment.center}
/>
);
}

export default UsersDemo;

Below is a list of customizations along with corresponding code snippets

PropertyDescriptionCode
Hide SearchHides the default search bar.hideSearch={true}
Hide ErrorHides both the default and custom error view passed in errorView prop.hideError={true}
Disable Users PresenceUsed to toggle functionality to show user's presence.disableUsersPresence={true}
Active UserUsed to set the active user. If set to true, the status indicator of the default list item view is not displayed.activeUser={chatUser}
Hide User StatusHides the user's online/offline status indicator.hideUserStatus={UserPresencePlacement.right}
Search KeywordThe search keyword used to filter the user list.searchKeyword="Alice"
Show Section HeaderDisplays an alphabetical section header for the user list.showSectionHeader={true}
Section Header KeyThe property on the user object used to extract the section header character.sectionHeaderKey={getName}
Selection ModeSelection mode to use for the default trailing view.selectionMode={SelectionMode.multiple}
Loading ViewA custom view to display during the loading state.loadingView={<>Custom Loading View</>}
Empty ViewA custom view to display when no users are available in the list.emptyView={<>Custom Empty View</>}
Error ViewA custom view to display when an error occurs.errorView={<>Custom Error View</>}

Advanced

For advanced-level customization, you can set custom views to the component. This lets you tailor each aspect of the component to fit your exact needs and application aesthetics. You can create and define your views, layouts, and UI elements and then incorporate those into the component.


ItemView

A custom view to render for each user in the fetched list.

Shown below is the default chat interface.

Image

The customized chat interface is displayed below.

Image

Use the following code to achieve the customization shown above.

import { CometChat } from "@cometchat/chat-sdk-javascript";
import { CometChatUsers } from "@cometchat/chat-uikit-react";

function UsersDemo() {
const getItemView = (user: CometChat.User) => {
const status = user.getStatus();

return (
<div
className={`cometchat-users__list-item cometchat-users__list-item-${status}
${status === "online" ? `cometchat-users__list-item-active` : ""}
`}
>
<CometChatListItem
title={user.getName()}
subtitleView={user.getStatus()}
avatarURL={user.getAvatar()}
avatarName={user.getName()}
/>
</div>
);
};

return <CometChatUsers itemView={getItemView} />;
}

export default UsersDemo;

TitleView

The customized chat interface is displayed below.

Image

Use the following code to achieve the customization shown above.

import React from "react";
import { CometChat } from "@cometchat/chat-sdk-javascript";
import { CometChatUsers } from "@cometchat/chat-uikit-react";

// Custom title view component
const customTitleView = (user: CometChat.User) => {
return <div className={`users__title-view users__title-view-${user.getRole()}`}>
<span className="users__title-view-name">{user.getName()}</span>
<span className="users__title-view-type"><img src="ICON_HERE"></img>{user.getRole()}</span>
</div>;
}

<CometChatUsers titleView={customTitleView} />;

SubtitleView

A custom view to render the subtitle for each user.

Shown below is the default chat interface.

Image

The customized chat interface is displayed below.

Image

Use the following code to achieve the customization shown above.

import { CometChat } from "@cometchat/chat-sdk-javascript";
import { CometChatUsers } from "@cometchat/chat-uikit-react";

function UsersDemo() {
function formatTime(timestamp: number) {
const date = new Date(timestamp * 1000);
return date.toLocaleString();
}

const getSubtitleView = (user: CometChat.User): JSX.Element => {
if (!(user instanceof CometChat.User)) {
return <></>;
}

return (
<div className="users-subtitle">
Last Active At: {formatTime(user.getLastActiveAt())}
</div>
);
};

return <CometChatUsers subtitleView={getSubtitleView} />;
}

export default UsersDemo;

HeaderView

You can set the Custom headerView to add more options to the Users component.

The customized chat interface is displayed below.

Image

Use the following code to achieve the customization shown above.

import { CometChat } from "@cometchat/chat-sdk-javascript";
import { CometChatUsers,localize,CometChatButton } from "@cometchat/chat-uikit-react";
import React from "react";

const getHeaderView = () => {
return (
<div className="cometchat-users__header">
<div className="cometchat-users__header__title">
{localize("USERS")}
</div>
<CometChatButton
onClick={() => {
// logic here
}}
iconURL={ICON_URL}
/>
</div>
);
};

<CometChatUsers headerView={getHeaderView()} />

LeadingView

The customized chat interface is displayed below.

Image

Use the following code to achieve the customization shown above.

import React from "react";
import { CometChat } from "@cometchat/chat-sdk-javascript";
import { CometChatUsers,CometChatAvatar } from "@cometchat/chat-uikit-react";

const customLeadingView = (user: CometChat.User): JSX.Element => {
return (
<div className={`users__leading-view users__leading-view-${user.getRole()}`}>
<CometChatAvatar
image={user?.getAvatar()}
name={user?.getName()}
/>
<span className="users__leading-view-role"><img src={"ICON_URL"}/></span>
</div>
);
};
<CometChatUsers leadingView={customLeadingView} />

TrailingView

The customized chat interface is displayed below.

Image

Use the following code to achieve the customization shown above.

import React from "react";
import { CometChat } from "@cometchat/chat-sdk-javascript";
import {
CometChatUsers,
} from "@cometchat/chat-uikit-react";

const customTrailingButtonView = (user:CometChat.User) => {
let tag = user?.getTags() ? user?.getTags()[0] : "";
return <div className="users__trailing-view">
<span className="users__trailing-view-icon">
<img src="ICON_HERE"/>
</span>
<span className="users__trailing-view-text">{tag}</span>
</div>
}

<CometChatUsers trailingView={customTrailingButtonView} />;

Options

A function that returns a list of actions available when hovering over a user item.

Shown below is the default chat interface.

Image

The customized chat interface is displayed below.

Image

Use the following code to achieve the customization shown above.

import { CometChatUsers, CometChatOption } from "@cometchat/chat-uikit-react";

function UsersDemo() {
const getOptions = (user: CometChat.User): CometChatOption[] => {
return [
new CometChatOption({
id: "delete",
title: "delete",
onClick: () => {
console.log("Custom option clicked for user:", user);
},
}),
];
};

return <CometChatUsers options={getOptions} />;
}

export default UsersDemo;