Skip to content
This repository was archived by the owner on Nov 13, 2024. It is now read-only.

Commit 0825949

Browse files
feat(lib): return change event and expose onKeyPress callback (#115)
* feat(lib): return change event and expose onKeyPress callback * fix(lib): fix a test * PubNub SDK v0.24.0 release. --------- Co-authored-by: PubNub Release Bot <[email protected]>
1 parent dee503f commit 0825949

File tree

8 files changed

+56
-31
lines changed

8 files changed

+56
-31
lines changed

.pubnub.yml

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
---
22
name: pubnub-react-chat-components
3-
version: v0.23.0
3+
version: v0.24.0
44
scm: github.com/pubnub/react-chat-components
55
schema: 1
66
files:
77
- lib/dist/index.js
88
- lib/dist/index.es.js
99
changelog:
10+
- date: 2023-04-11
11+
version: v0.24.0
12+
changes:
13+
- type: feature
14+
text: "Return change event and expose onKeyPress callback."
1015
- date: 2023-04-04
1116
version: v0.23.0
1217
changes:

packages/common/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pubnub/common-chat-components",
3-
"version": "0.23.0",
3+
"version": "0.24.0",
44
"main": "src/index.ts",
55
"license": "MIT",
66
"scripts": {

packages/common/src/message-input/message-input.tsx

+3-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useEffect } from "react";
1+
import { useState, useEffect, ChangeEvent } from "react";
22
import { useAtom } from "jotai";
33
import { usePubNub } from "pubnub-react";
44
import uuid from "react-native-uuid";
@@ -28,8 +28,6 @@ export interface CommonMessageInputProps {
2828
disabled?: boolean;
2929
/** Custom UI component to override default display for the Send button. */
3030
sendButton?: JSX.Element | string;
31-
/** Callback to handle an event when the text value changes. */
32-
onChange?: (value: string) => void;
3331
/** Callback to modify message content before sending it. This only works for text messages, not files. */
3432
onBeforeSend?: (value: MessagePayload) => MessagePayload;
3533
/** Callback for extra actions after sending a message. */
@@ -125,22 +123,6 @@ export const useMessageInputCore = (props: CommonMessageInputProps) => {
125123
setText("");
126124
};
127125

128-
/*
129-
/* Event handlers
130-
*/
131-
132-
const handleInputChange = (newText: string) => {
133-
try {
134-
if (props.typingIndicator) {
135-
newText.length ? startTypingIndicator() : stopTypingIndicator();
136-
}
137-
props.onChange && props.onChange(newText);
138-
setText(newText);
139-
} catch (e) {
140-
onError(e);
141-
}
142-
};
143-
144126
/*
145127
/* Lifecycle
146128
*/
@@ -160,13 +142,14 @@ export const useMessageInputCore = (props: CommonMessageInputProps) => {
160142
clearInput,
161143
file,
162144
setFile,
163-
handleInputChange,
164145
isValidInputText,
165146
loader,
166147
onError,
167148
sendMessage,
168149
setText,
169150
text,
170151
theme,
152+
startTypingIndicator,
153+
stopTypingIndicator,
171154
};
172155
};

packages/react-native/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pubnub/react-native-chat-components",
3-
"version": "0.23.0",
3+
"version": "0.24.0",
44
"description": "PubNub Chat Components is a development kit of React Native components that aims to help you to easily build Chat applications using PubNub infrastructure. It removes the complexicity of picking an adequate Chat engine, learning its APIs and dealing with its low-level internals. As the same time it allows you to create apps of various use cases, with different functionalities and customizable looks.",
55
"author": "PubNub <[email protected]>",
66
"main": "dist/commonjs/index",

packages/react-native/src/message-input/message-input.tsx

+15-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export type MessageInputProps = CommonMessageInputProps & {
2222
modalVisible: boolean;
2323
setModalVisible: React.Dispatch<React.SetStateAction<boolean>>;
2424
}) => JSX.Element;
25+
onChange?: (newText: string) => void;
2526
};
2627

2728
/**
@@ -30,7 +31,8 @@ export type MessageInputProps = CommonMessageInputProps & {
3031
*/
3132
export const MessageInput: FC<MessageInputProps> = (props: MessageInputProps) => {
3233
const {
33-
handleInputChange,
34+
startTypingIndicator,
35+
stopTypingIndicator,
3436
isValidInputText,
3537
loader,
3638
sendMessage,
@@ -106,6 +108,18 @@ export const MessageInput: FC<MessageInputProps> = (props: MessageInputProps) =>
106108
setText("");
107109
};
108110

111+
const handleInputChange = (newText: string) => {
112+
try {
113+
if (props.typingIndicator) {
114+
newText.length ? startTypingIndicator() : stopTypingIndicator();
115+
}
116+
props.onChange && props.onChange(newText);
117+
setText(newText);
118+
} catch (e) {
119+
onError(e);
120+
}
121+
};
122+
109123
/*
110124
/* Renderers
111125
*/

packages/react/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pubnub/react-chat-components",
3-
"version": "0.23.0",
3+
"version": "0.24.0",
44
"description": "PubNub Chat Components is a development kit of React components that aims to help you to easily build Chat applications using PubNub infrastructure. It removes the complexicity of picking an adequate Chat engine, learning its APIs and dealing with its low-level internals. As the same time it allows you to create apps of various use cases, with different functionalities and customizable looks.",
55
"author": "PubNub <[email protected]>",
66
"main": "dist/index.js",

packages/react/src/message-input/message-input.tsx

+22-5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ export type MessageInputProps = CommonMessageInputProps & {
2424
hideSendButton?: boolean;
2525
/** Option to pass in an emoji picker if you want it to be rendered in the input. For more details, refer to the Emoji Pickers section in the docs. */
2626
emojiPicker?: ReactElement<EmojiPickerElementProps>;
27+
/** Callback to handle an event when the text value changes. */
28+
onChange?: (event: ChangeEvent<HTMLTextAreaElement>) => void;
29+
/** Callback to handle an event when the key is pressed in textarea. */
30+
onKeyPress?: (event: KeyboardEvent<HTMLTextAreaElement>) => void;
2731
};
2832

2933
/**
@@ -35,14 +39,15 @@ export const MessageInput: FC<MessageInputProps> = (props: MessageInputProps) =>
3539
clearInput,
3640
file,
3741
setFile,
38-
handleInputChange,
3942
isValidInputText,
4043
loader,
4144
onError,
4245
sendMessage,
4346
setText,
4447
text,
4548
theme,
49+
startTypingIndicator,
50+
stopTypingIndicator,
4651
} = useMessageInputCore(props);
4752

4853
const [emojiPickerShown, setEmojiPickerShown] = useState(false);
@@ -73,13 +78,27 @@ export const MessageInput: FC<MessageInputProps> = (props: MessageInputProps) =>
7378
/* Event handlers
7479
*/
7580

81+
const handleInputChange = (event: ChangeEvent<HTMLTextAreaElement>) => {
82+
try {
83+
const { value: newText } = event.target;
84+
if (props.typingIndicator) {
85+
newText.length ? startTypingIndicator() : stopTypingIndicator();
86+
}
87+
props.onChange && props.onChange(event);
88+
setText(newText);
89+
} catch (e) {
90+
onError(e);
91+
}
92+
};
93+
7694
const handleKeyPress = (event: KeyboardEvent<HTMLTextAreaElement>) => {
7795
try {
7896
if (event.key === "Enter" && !event.shiftKey) {
7997
event.preventDefault();
8098
sendMessage();
8199
if (fileRef.current) fileRef.current.value = "";
82100
}
101+
props.onKeyPress(event);
83102
} catch (e) {
84103
onError(e);
85104
}
@@ -188,10 +207,8 @@ export const MessageInput: FC<MessageInputProps> = (props: MessageInputProps) =>
188207
className="pn-msg-input__textarea"
189208
data-testid="message-input"
190209
disabled={props.disabled || !!file}
191-
onChange={(e) => {
192-
handleInputChange(e.target.value);
193-
}}
194-
onKeyPress={(e) => handleKeyPress(e)}
210+
onChange={handleInputChange}
211+
onKeyPress={handleKeyPress}
195212
placeholder={props.placeholder}
196213
ref={inputRef}
197214
rows={1}

packages/react/test/message-input.test.tsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ describe("Message Input", () => {
3030

3131
await userEvent.type(screen.getByPlaceholderText("Send message"), "Changed Value");
3232

33-
expect(handleChange).toHaveBeenCalledWith("Changed Value");
33+
expect(handleChange).toHaveBeenCalledWith(
34+
expect.objectContaining({
35+
target: expect.objectContaining({
36+
value: "Changed Value",
37+
}),
38+
})
39+
);
3440
});
3541

3642
test("renders custom placeholders", () => {

0 commit comments

Comments
 (0)