Skip to content

Commit

Permalink
feat add keep replies fonctionnality
Browse files Browse the repository at this point in the history
  • Loading branch information
xcarpentier committed May 13, 2019
1 parent 5875a34 commit b2bfa50
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
3 changes: 2 additions & 1 deletion example-expo/data/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ export default [
// },
{
_id: 1,
text: 'This is a quick reply. Do you love Gifted Chat? (radio)',
text: 'This is a quick reply. Do you love Gifted Chat? (radio) KEEP IT',
createdAt: new Date(),
quickReplies: {
type: 'radio', // or 'checkbox',
keepIt: true,
values: [
{
title: '😋 Yes',
Expand Down
5 changes: 1 addition & 4 deletions src/Bubble.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -281,15 +281,12 @@ export default class Bubble extends React.Component<BubbleProps> {

renderQuickReplies() {
const { currentMessage, onQuickReply, nextMessage } = this.props
if (nextMessage && nextMessage._id) {
return null
}
if (currentMessage && currentMessage.quickReplies) {
const { containerStyle, wrapperStyle, ...quickReplyProps } = this.props
if (this.props.renderQuickReplies) {
return this.props.renderQuickReplies(quickReplyProps)
}
return <QuickReplies {...{ currentMessage, onQuickReply }} />
return <QuickReplies {...{ currentMessage, onQuickReply, nextMessage }} />
}
return null
}
Expand Down
2 changes: 2 additions & 0 deletions src/GiftedChat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
DATE_FORMAT,
} from './Constant'
import { IMessage, User, Reply } from './types'
import QuickReplies from './QuickReplies'

const GiftedActionSheet = ActionSheet as any

Expand Down Expand Up @@ -147,6 +148,7 @@ export interface GiftedChatProps<TMessage extends IMessage = IMessage> {
/* Custom parse patterns for react-native-parsed-text used to linking message content (like URLs and phone numbers) */
parsePatterns?(): React.ReactNode
onQuickReply?(replies: Reply[]): void
renderQuickReplies?(quickReplies: QuickReplies['props']): React.ReactNode
}

interface GiftedChatState {
Expand Down
29 changes: 27 additions & 2 deletions src/QuickReplies.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const styles = StyleSheet.create({
})

interface QuickRepliesProps {
nextMessage?: IMessage
currentMessage?: IMessage
color?: string
sendText?: string
Expand Down Expand Up @@ -58,6 +59,7 @@ export default class QuickReplies extends Component<
onQuickReply: () => {},
color: Color.peterRiver,
sendText: 'Send',
keepReplies: false,
}

static propTypes = {
Expand Down Expand Up @@ -101,18 +103,41 @@ export default class QuickReplies extends Component<
}

handleSend = (replies: Reply[]) => () => {
const { currentMessage } = this.props
if (this.props.onQuickReply) {
this.props.onQuickReply(replies)
this.props.onQuickReply(
replies.map((reply: Reply) => ({
...reply,
messageId: currentMessage!._id,
})),
)
}
}

shouldComponentDisplay = () => {
const { currentMessage, nextMessage } = this.props
const hasReplies = !!currentMessage && !!currentMessage!.quickReplies
const hasNext = !!nextMessage && !!nextMessage!._id

if (hasReplies && !hasNext) {
return true
}
if (hasReplies && hasNext && currentMessage!.quickReplies!.keepIt) {
return true
}
return false
}

render() {
const { currentMessage, color, sendText } = this.props
const { replies } = this.state
if (!currentMessage && !currentMessage!.quickReplies) {

if (!this.shouldComponentDisplay()) {
return null
}

const { type } = currentMessage!.quickReplies!

return (
<View style={styles.container}>
{currentMessage!.quickReplies!.values.map((reply: Reply) => {
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ export interface User {
export interface Reply {
title: string
value: string
messageId?: string
}

export interface QuickReplies {
type: 'radio' | 'checkbox'
values: Reply[]
keepIt?: boolean
}

export interface IMessage {
Expand Down

0 comments on commit b2bfa50

Please sign in to comment.