Messages - 图1

Messages Component

Messages React component represents Messages component.

Messages Components

There are following components included:

  • **Messages** / **F7Messages** - main Messages container
  • **Message** / **F7Message** - single message element
  • **MessagesTitle** / **F7MessagesTitle** - single messages title element

Messages Properties

PropTypeDefaultDescription
<Messages> properties
initbooleantrueInitializes Messages component
newMessagesFirstbooleanfalseEnable if you want to use new messages on top, instead of having them on bottom
scrollMessagesbooleantrueEnable/disable messages autoscrolling when adding new message
scrollMessagesOnEdgebooleantrueIf enabled then messages autoscrolling will happen only when user is on top/bottom of the messages view
<Message> properties
typestringsentMessage type: sent (default) or received
textstringMessage text
avatarstringMessage user’s avatar URL
namestringMessage user’s name
imagestringMessage image URL
headerstringMessage header
footerstringMessage footer
textHeaderstringMessage text header
textFooterstringMessage text footer
firstbooleanfalseDefines that the message is first in the conversation
lastbooleanfalseDefines that the message is last in the conversation
tailbooleanfalseDefines that the message has visual “tail”. Usually last message in conversation
sameNamebooleanfalseDefines that this message sender name is the same as on previous message
sameHeaderbooleanfalseDefines that this message header text is the same as on previous message
sameFooterbooleanfalseDefines that this message footer text is the same as on previous message
sameAvatarbooleanfalseDefines that this message user’s avatar URL is the same as on previous message

Messages Methods

<Messages> methods
.scroll(duration, position);Scroll messages to top/bottom depending on newMessagesFirst parameter
  • duration - number scroll duration in ms
  • position - number scroll position in px
.showTyping(message);Show typing message indicator
.hideTyping()Hide typing message indicator

Messages Events

EventDescription
<Message> events
clickEvent will be triggered when user clicks on message bubble
clickNameEvent will be triggered when user clicks on message user’s name
clickTextEvent will be triggered when user clicks on message text
clickAvatarEvent will be triggered when user clicks on message user’s avatar
clickHeaderEvent will be triggered when user clicks on message header
clickFooterEvent will be triggered when user clicks on message footer
clickBubbleEvent will be triggered when user clicks on message bubble

Messages Slots

Single message React component (<Message>) has additional slots for custom elements:

  • **default** - element will be inserted as a child of <div class="message-bubble"> element in the end
  • **start** - element will be inserted in the beginning and direct child of main message element <div class="message">
  • **end** - element will be inserted in the end and direct child of main message element <div class="message">
  • **content-start** - element will be inserted in the beginning and direct child of the <div class="message-content"> element
  • **content-end** - element will be inserted in the end and direct child of the <div class="message-content"> element
  • **bubble-start** - element will be inserted in the beginning and direct child of the <div class="message-bubble"> element
  • **bubble-end** - element will be inserted in the end and direct child of the <div class="message-bubble"> element. Same as **default** slot

The following slots can be used inside of single message instead of same props if you need to pass there more complext layout:

  • **header** - element will be inserted in message header
  • **footer** - element will be inserted in message footer
  • **text** - element will be inserted in message text
  • **name** - element will be inserted in message name
  • **image** - element will be inserted in message image (supposed tp be an <img> element)
  • **text-header** - element will be inserted in message text header
  • **text-footer** - element will be inserted in message text footer
  1. <Message
  2. type="sent"
  3. text="Hello World"
  4. name="John Doe"
  5. avatar="path/to/image.jpg"
  6. >
  7. <div slot="start">Start</div>
  8. <div slot="end">End</div>
  9. <div slot="content-start">Content Start</div>
  10. <div slot="content-end">Content End</div>
  11. <div slot="bubble-start">Bubble Start</div>
  12. <div slot="bubble-end">Bubble End</div>
  13. </Message>
  14. {/* Renders to: */}
  15. <div class="message message-sent">
  16. <div>Start</div>
  17. <div class="message-avatar" style="background-image: url(path/to/image.jpg);"></div>
  18. <div class="message-content">
  19. <div>Content Start</div>
  20. <div class="message-name">John Doe</div>
  21. <div class="message-bubble">
  22. <div>Bubble Start</div>
  23. <div class="message-text">Hello World</div>
  24. <div>Bubble End</div>
  25. </div>
  26. <div>Content End</div>
  27. </div>
  28. <div>End</div>
  29. </div>

Access To Messages Instance

If you use automatic initalization to init Messages (with init={true} prop) and need to use Messages API you can access its initialized instance by accessing **.f7Messages** component’s property.

Examples

Here is how the full example of Messages page where it can be used together with Messagebar:

  1. export default class extends React.Component {
  2. constructor(props) {
  3. super(props);
  4. this.state = {
  5. attachments: [],
  6. sheetVisible: false,
  7. typingMessage: null,
  8. messagesData: [
  9. {
  10. type: 'sent',
  11. text: 'Hi, Kate',
  12. },
  13. {
  14. type: 'sent',
  15. text: 'How are you?',
  16. },
  17. {
  18. name: 'Kate',
  19. type: 'received',
  20. text: 'Hi, I am good!',
  21. avatar: 'https://cdn.framework7.io/placeholder/people-100x100-9.jpg',
  22. },
  23. {
  24. name: 'Blue Ninja',
  25. type: 'received',
  26. text: 'Hi there, I am also fine, thanks! And how are you?',
  27. avatar: 'https://cdn.framework7.io/placeholder/people-100x100-7.jpg',
  28. },
  29. {
  30. type: 'sent',
  31. text: 'Hey, Blue Ninja! Glad to see you ;)',
  32. },
  33. {
  34. type: 'sent',
  35. text: 'Hey, look, cutest kitten ever!',
  36. },
  37. {
  38. type: 'sent',
  39. image: 'https://cdn.framework7.io/placeholder/cats-200x260-4.jpg',
  40. },
  41. {
  42. name: 'Kate',
  43. type: 'received',
  44. text: 'Nice!',
  45. avatar: 'https://cdn.framework7.io/placeholder/people-100x100-9.jpg',
  46. },
  47. {
  48. name: 'Kate',
  49. type: 'received',
  50. text: 'Like it very much!',
  51. avatar: 'https://cdn.framework7.io/placeholder/people-100x100-9.jpg',
  52. },
  53. {
  54. name: 'Blue Ninja',
  55. type: 'received',
  56. text: 'Awesome!',
  57. avatar: 'https://cdn.framework7.io/placeholder/people-100x100-7.jpg',
  58. },
  59. ],
  60. images: [
  61. 'https://cdn.framework7.io/placeholder/cats-300x300-1.jpg',
  62. 'https://cdn.framework7.io/placeholder/cats-200x300-2.jpg',
  63. 'https://cdn.framework7.io/placeholder/cats-400x300-3.jpg',
  64. 'https://cdn.framework7.io/placeholder/cats-300x150-4.jpg',
  65. 'https://cdn.framework7.io/placeholder/cats-150x300-5.jpg',
  66. 'https://cdn.framework7.io/placeholder/cats-300x300-6.jpg',
  67. 'https://cdn.framework7.io/placeholder/cats-300x300-7.jpg',
  68. 'https://cdn.framework7.io/placeholder/cats-200x300-8.jpg',
  69. 'https://cdn.framework7.io/placeholder/cats-400x300-9.jpg',
  70. 'https://cdn.framework7.io/placeholder/cats-300x150-10.jpg',
  71. ],
  72. people: [
  73. {
  74. name: 'Kate Johnson',
  75. avatar: 'https://cdn.framework7.io/placeholder/people-100x100-9.jpg',
  76. },
  77. {
  78. name: 'Blue Ninja',
  79. avatar: 'https://cdn.framework7.io/placeholder/people-100x100-7.jpg',
  80. },
  81. ],
  82. answers: [
  83. 'Yes!',
  84. 'No',
  85. 'Hm...',
  86. 'I am not sure',
  87. 'And what about you?',
  88. 'May be ;)',
  89. 'Lorem ipsum dolor sit amet, consectetur',
  90. 'What?',
  91. 'Are you sure?',
  92. 'Of course',
  93. 'Need to think about it',
  94. 'Amazing!!!',
  95. ],
  96. responseInProgress: false,
  97. }
  98. }
  99. render() {
  100. return (
  101. <Page>
  102. <Navbar title="Messsages"></Navbar>
  103. <Messagebar
  104. placeholder={this.placeholder}
  105. ref={(el) => {this.messagebarComponent = el}}
  106. attachmentsVisible={this.attachmentsVisible}
  107. sheetVisible={this.state.sheetVisible}
  108. >
  109. <Link
  110. iconIos="f7:camera_fill"
  111. iconAurora="f7:camera_fill"
  112. iconMd="material:camera_alt"
  113. slot="inner-start"
  114. onClick={() => {this.setState({sheetVisible: !this.state.sheetVisible})}}
  115. ></Link>
  116. <Link
  117. iconIos="f7:arrow_up_fill"
  118. iconAurora="f7:arrow_up_fill"
  119. iconMd="material:send"
  120. slot="inner-end"
  121. onClick={this.sendMessage.bind(this)}
  122. ></Link>
  123. <MessagebarAttachments>
  124. {this.state.attachments.map((image, index) => (
  125. <MessagebarAttachment
  126. key={index}
  127. image={image}
  128. onAttachmentDelete={() => this.deleteAttachment(image)}
  129. ></MessagebarAttachment>
  130. ))}
  131. </MessagebarAttachments>
  132. <MessagebarSheet>
  133. {this.state.images.map((image, index) => (
  134. <MessagebarSheetImage
  135. key={index}
  136. image={image}
  137. checked={this.state.attachments.indexOf(image) >= 0}
  138. onChange={this.handleAttachment.bind(this)}
  139. ></MessagebarSheetImage>
  140. ))}
  141. </MessagebarSheet>
  142. </Messagebar>
  143. <Messages ref={(el) => {this.messagesComponent = el}}>
  144. <MessagesTitle><b>Sunday, Feb 9,</b> 12:58</MessagesTitle>
  145. {this.state.messagesData.map((message, index) => (
  146. <Message
  147. key={index}
  148. type={message.type}
  149. image={message.image}
  150. name={message.name}
  151. avatar={message.avatar}
  152. first={this.isFirstMessage(message, index)}
  153. last={this.isLastMessage(message, index)}
  154. tail={this.isTailMessage(message, index)}
  155. >
  156. {message.text && (
  157. <span slot="text" dangerouslySetInnerHTML={{__html: message.text}} />
  158. )}
  159. </Message>
  160. ))}
  161. {this.state.typingMessage && (
  162. <Message
  163. type="received"
  164. typing={true}
  165. first={true}
  166. last={true}
  167. tail={true}
  168. header={`${this.state.typingMessage.name} is typing`}
  169. avatar={this.state.typingMessage.avatar}
  170. ></Message>
  171. )}
  172. </Messages>
  173. </Page>
  174. )
  175. }
  176. get attachmentsVisible() {
  177. const self = this;
  178. return self.state.attachments.length > 0;
  179. }
  180. get placeholder() {
  181. const self = this;
  182. return self.state.attachments.length > 0 ? 'Add comment or Send' : 'Message';
  183. }
  184. componentDidMount() {
  185. const self = this;
  186. self.$f7ready(() => {
  187. self.messagebar = self.messagebarComponent.f7Messagebar;
  188. self.messages = self.messagesComponent.f7Messages;
  189. });
  190. }
  191. isFirstMessage(message, index) {
  192. const self = this;
  193. const previousMessage = self.state.messagesData[index - 1];
  194. if (message.isTitle) return false;
  195. if (!previousMessage || previousMessage.type !== message.type || previousMessage.name !== message.name) return true;
  196. return false;
  197. }
  198. isLastMessage(message, index) {
  199. const self = this;
  200. const nextMessage = self.state.messagesData[index + 1];
  201. if (message.isTitle) return false;
  202. if (!nextMessage || nextMessage.type !== message.type || nextMessage.name !== message.name) return true;
  203. return false;
  204. }
  205. isTailMessage(message, index) {
  206. const self = this;
  207. const nextMessage = self.state.messagesData[index + 1];
  208. if (message.isTitle) return false;
  209. if (!nextMessage || nextMessage.type !== message.type || nextMessage.name !== message.name) return true;
  210. return false;
  211. }
  212. deleteAttachment(image) {
  213. const self = this;
  214. const attachments = self.state.attachments;
  215. const index = attachments.indexOf(image);
  216. attachments.splice(index, 1);
  217. self.setState({ attachments });
  218. }
  219. handleAttachment(e) {
  220. const self = this;
  221. const attachments = self.state.attachments;
  222. const index = self.$$(e.target).parents('label.checkbox').index();
  223. const image = self.state.images[index];
  224. if (e.target.checked) {
  225. // Add to attachments
  226. attachments.unshift(image);
  227. } else {
  228. // Remove from attachments
  229. attachments.splice(attachments.indexOf(image), 1);
  230. }
  231. self.setState({ attachments });
  232. }
  233. sendMessage() {
  234. const self = this;
  235. const text = self.messagebar.getValue().replace(/\n/g, '<br>').trim();
  236. const messagesToSend = [];
  237. self.state.attachments.forEach((attachment) => {
  238. messagesToSend.push({
  239. image: attachment,
  240. });
  241. });
  242. if (text.trim().length) {
  243. messagesToSend.push({
  244. text,
  245. });
  246. }
  247. if (messagesToSend.length === 0) {
  248. return;
  249. }
  250. self.setState({
  251. // Reset attachments
  252. attachments: [],
  253. // Hide sheet
  254. sheetVisible: false,
  255. // Send message
  256. messagesData: [...self.state.messagesData, ...messagesToSend],
  257. });
  258. self.messagebar.clear();
  259. // Focus area
  260. if (text.length) self.messagebar.focus();
  261. // Mock response
  262. if (self.state.responseInProgress) return;
  263. self.setState({
  264. responseInProgress: true,
  265. })
  266. setTimeout(() => {
  267. const answer = self.state.answers[Math.floor(Math.random() * self.state.answers.length)];
  268. const person = self.state.people[Math.floor(Math.random() * self.state.people.length)];
  269. self.setState({
  270. typingMessage: {
  271. name: person.name,
  272. avatar: person.avatar,
  273. },
  274. });
  275. setTimeout(() => {
  276. self.setState({
  277. messagesData: [...self.state.messagesData, {
  278. text: answer,
  279. type: 'received',
  280. name: person.name,
  281. avatar: person.avatar,
  282. }],
  283. typingMessage: null,
  284. responseInProgress: false,
  285. });
  286. }, 4000);
  287. }, 1000);
  288. }
  289. };