Messagebar - 图1

Messagebar React Component

Messagebar React component represents Framework7’s Messagebar component.

Messagebar Components

There are following components included:

  • **Messagebar** / **F7Messagebar** - main messagebar element
  • **MessagebarSheet** / **F7MessagebarSheet** - messagebar sheet element
  • **MessagebarSheetImage** / **F7MessagebarSheetImage** - messagebar sheet image item
  • **MessagebarSheetItem** / **F7MessagebarSheetItem** - messagebar sheet item
  • **MessagebarAttachments** / **F7MessagebarAttachments** - messagebar attachments element
  • **MessagebarAttachment** / **F7MessagebarAttachment** - single messagebar attachment element

Messagebar - 图2

Messagebar Properties

PropTypeDefaultDescription
<Messagebar> properties
initbooleantrueInitializes Messagebar
namestringTextarea “name” attribute
textareaIdstring
number
Textarea “id” attribute
placeholderstringMessageTextarea placeholder text
valuestring
number
Textarea value
readonlybooleanfalseSets “readonly” textarea attribute
disabledbooleanfalseSets “disbled” textarea attribute
sendLinkstringEnables Send link and specifies its text. This property will be ignored in case you use send-link slot
maxHeightnumberDefines resizeable textarea max height
resizableboleantrueEnables resizeable textarea
sheetVisiblebooleanfalseMakes messagebar sheet visible/active
attachmentsVisiblebooleanfalseMakes messagebar attachments visible/active
resizePagebooleantrueWhen enabled, it will resize messages page when messagebar textarea size changed
<MessagebarSheetImage> properties
imagestringSheet image URL
checkedbooleanfalseIndicates whether this sheet image-item is checked or not
<MessagebarAttachment> properties
imagestringAttachment image URL
deletablebooleantrueDefines whether the attachment is deletable or not. In case of deletable the additional delete button will be rendered

Messagebar Methods

<Messagebar> methods
.clear()Clear textarea and update/reset its size
.setValue(newValue)Set messagebar textarea value/text
.getValue()Return messagebar textarea value
.resize()Force Messagebar to resize messages page depending on messagebar height/size
.focus()Focus messagebar textarea
.blur()Remove focus from messagebar textarea

Messagebar Events

EventArgumentsDescription
<Messagebar> events
change(event)Event will be triggered when “change” event occurs on messagebar textarea element
input(event)Event will be triggered when “input” event occurs on messagebar textarea element
focus(event)Event will be triggered when “focus” event occurs on messagebar textarea element
blur(event)Event will be triggered when “blur” event occurs on messagebar textarea element
submit
send
(value, clear)Event will be triggered when user clicks on messagebar “send link”
<MessagebarSheetImage> events
change(event)Event will be triggered on sheet item checkbox change
<MessagebarAttachment> events
attachmentClick(event)Event will be triggered on attachment click
attachmentDelete(event)Event will be triggered on attachment delete button click

Messagebar Slots

Messagebar React component has additional slots for custom elements:

  • **default** - element will be inserted in the end of <div class="toolbar-inner">. element
  • **before-area** - element will be inserted right before textarea. Messagebar attachments go here
  • **after-area** - element will be inserted right after textarea
  • **send-link** - element will be inserted inside of send link
  • **before-inner** - element will be inserted right before <div class="toolbar-inner">. element
  • **after-inner** - element will be inserted right after <div class="toolbar-inner">. element. Messagebar sheet goes here
  • **inner-start** - element will be inserted in the beginning of <div class="toolbar-inner">. element
  • **inner-end** - element will be inserted in the end of <div class="toolbar-inner">. element. Same as default slot
  1. <Messagebar placeholder="Message" onSubmit="this.onSubmit">
  2. <div slot="before-inner">Before inner</div>
  3. <div slot="after-inner">After inner</div>
  4. <div slot="before-area">Before textarea</div>
  5. <div slot="after-area">After textarea</div>
  6. <Icon ios="f7:arrow_up_fill" aurora="f7:arrow_up_fill" md="material:send" slot="send-link"></Icon>
  7. <div>Default slot</div>
  8. </Messagebar>

Renders to:

  1. <div class="toolbar messagebar">
  2. <div>Before inner</div>
  3. <div class="toolbar-inner">
  4. <div class="messagebar-area">
  5. <div>Before textarea</div>
  6. <textarea placeholder="Message"></textarea>
  7. <div>After textarea</div>
  8. </div>
  9. <a href="#" class="link"><i class="icon f7-icons">arrow_up_fill</i></a>
  10. <div>Default slot</div>
  11. </div>
  12. <div>After inner</div>
  13. </div>

Access To Messagebar Instance

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

Examples

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

  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. };