Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added handleSubmit event #194

Merged
merged 1 commit into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ export default App;
|**launcherCloseLabel**|string|NO|'Close chat'|Alt value for the laucher when open|
|**sendButtonAlt**|string|NO|'Send'|Send button alt for a11y purposes|
|**handleTextInputChange**|(event) => any|NO| |Prop that triggers on input change|
|**handleSubmit**|(event) => any|NO| |Prop that triggers when a message is submitted, used for custom validation|

#### Styles

Expand Down
10 changes: 10 additions & 0 deletions dev/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { Component } from 'react';

import { Widget, addResponseMessage, setQuickButtons, toggleMsgLoader, addLinkSnippet } from '../index';
import { addUserMessage } from '..';

export default class App extends Component {
componentDidMount() {
Expand All @@ -25,6 +26,14 @@ export default class App extends Component {
setQuickButtons([]);
}

handleSubmit = (msgText: string) => {
if(msgText.length < 80) {
addUserMessage("Uh oh, please write a bit more.");
return false;
}
return true;
}

render() {
return (
<Widget
Expand All @@ -33,6 +42,7 @@ export default class App extends Component {
senderPlaceHolder="Escribe aquí ..."
handleNewUserMessage={this.handleNewUserMessage}
handleQuickButtonClicked={this.handleQuickButtonClicked}
handleSubmit={this.handleSubmit}
/>
);
}
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
setQuickButtons,
deleteMessages,
markAllAsRead,
setBadgeCount
setBadgeCount,
} from './src/store/dispatcher';

export {
Expand Down
8 changes: 6 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-chat-widget",
"version": "3.0.2",
"version": "3.0.3",
"description": "Chat web widget for React apps",
"main": "lib/index.js",
"repository": "git@github.com:Wolox/react-chat-widget.git",
Expand Down
15 changes: 11 additions & 4 deletions src/components/Widget/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Props = {
launcherCloseLabel: string;
sendButtonAlt: string;
showTimeStamp: boolean;
handleSubmit?: AnyFunction;
}

function Widget({
Expand All @@ -43,7 +44,8 @@ function Widget({
launcherOpenLabel,
launcherCloseLabel,
sendButtonAlt,
showTimeStamp
showTimeStamp,
handleSubmit
}: Props) {
const dispatch = useDispatch();

Expand All @@ -54,11 +56,16 @@ function Widget({
const handleMessageSubmit = (event) => {
event.preventDefault();
const userInput = event.target.message.value;
if (userInput.trim()) {

if (!userInput.trim()) {
return;
}

if(typeof handleSubmit === 'function' && handleSubmit(userInput)) {
dispatch(addUserMessage(userInput));
handleNewUserMessage(userInput);
}
event.target.message.value = '';
event.target.message.value = '';
}
}

const onQuickButtonClicked = (event, value) => {
Expand Down
5 changes: 4 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Props = {
launcherCloseLabel?: string,
sendButtonAlt?: string;
showTimeStamp?: boolean;
handleSubmit?: AnyFunction;
} & typeof defaultProps;

function ConnectedWidget({
Expand All @@ -44,7 +45,8 @@ function ConnectedWidget({
launcherOpenLabel,
launcherCloseLabel,
sendButtonAlt,
showTimeStamp
showTimeStamp,
handleSubmit
}: Props) {
return (
<Provider store={store}>
Expand All @@ -66,6 +68,7 @@ function ConnectedWidget({
launcherCloseLabel={launcherCloseLabel}
sendButtonAlt={sendButtonAlt}
showTimeStamp={showTimeStamp}
handleSubmit={handleSubmit}
/>
</Provider>
);
Expand Down