消息组件将帮助您在应用程序中可视化评论和消息系统。
包含以下组件
消息
消息
消息标题
名称 | 类型 | 默认值 | 描述 |
---|---|---|---|
组件 | 字符串 | 'div' | 组件的 HTML 元素 |
名称 | 类型 | 默认值 | 描述 |
---|---|---|---|
头像 | ReactNode | 消息用户头像 URL | |
颜色 | 对象 | ||
colors.bubbleReceivedIos | 字符串 | 'bg-[#e5e5ea] dark:bg-[#252525]' | |
colors.bubbleReceivedMd | 字符串 | 'dark:bg-md-dark-surface-variant bg-[#e5e5ea]' | |
colors.bubbleSentIos | 字符串 | 'bg-primary' | |
colors.bubbleSentMd | 字符串 | 'bg-md-light-primary dark:bg-md-dark-primary dark:text-md-dark-on-primary' | |
colors.messageNameIos | 字符串 | 'text-black text-opacity-45 dark:text-white dark:text-opacity-45' | |
colors.messageNameMd | 字符串 | 'text-md-light-on-surface-variant dark:text-md-dark-on-surface-variant' | |
colors.messageSent | 字符串 | 'text-white' | |
组件 | 字符串 | 'div' | 组件的 HTML 元素 |
页脚 | ReactNode | 消息页脚的内容 | |
页眉 | ReactNode | 消息页眉的内容 | |
id | 字符串 | 消息 id 属性 | |
名称 | 字符串 | 消息名称 | |
文本 | ReactNode | 消息文本 | |
页脚文本 | 字符串 | 消息页脚文本 | |
页眉文本 | 字符串 | 消息页眉文本 | |
类型 | 字符串 | '发送' |
|
onClick | function(e) | 消息点击处理程序 |
名称 | 类型 | 默认值 | 描述 |
---|---|---|---|
颜色 | 对象 | ||
colors.titleMd | 字符串 | 'text-md-light-on-surface-variant dark:text-md-dark-on-surface-variant' | |
组件 | 字符串 | 'div' | 组件的 HTML 元素 |
/* eslint-disable react/no-array-index-key */import { React, useState, useRef, useEffect } from 'react';import {Page,Navbar,NavbarBackLink,Messagebar,Messages,Message,MessagesTitle,Link,Icon,} from 'konsta/react';import { CameraFill, ArrowUpCircleFill } from 'framework7-icons/react';import { MdCameraAlt, MdSend } from 'react-icons/md';export default function MessagesPage() {const [messageText, setMessageText] = useState('');const [messagesData, setMessagesData] = useState([{type: 'sent',text: 'Hi, Kate',},{type: 'sent',text: 'How are you?',},{name: 'Kate',type: 'received',text: 'Hi, I am good!',avatar: 'https://cdn.framework7.io/placeholder/people-100x100-9.jpg',},{name: 'Blue Ninja',type: 'received',text: 'Hi there, I am also fine, thanks! And how are you?',avatar: 'https://cdn.framework7.io/placeholder/people-100x100-7.jpg',},{type: 'sent',text: 'Hey, Blue Ninja! Glad to see you ;)',},{type: 'sent',text: 'How do you feel about going to the movies today?',},{name: 'Kate',type: 'received',text: ' Oh, great idea!',avatar: 'https://cdn.framework7.io/placeholder/people-100x100-9.jpg',},{name: 'Kate',type: 'received',text: ' What cinema are we going to?',avatar: 'https://cdn.framework7.io/placeholder/people-100x100-9.jpg',},{name: 'Blue Ninja',type: 'received',text: 'Great. And what movie?',avatar: 'https://cdn.framework7.io/placeholder/people-100x100-7.jpg',},{name: 'Blue Ninja',type: 'received',text: 'What time?',avatar: 'https://cdn.framework7.io/placeholder/people-100x100-7.jpg',},]);const pageRef = useRef();const initiallyScrolled = useRef(false);const scrollToBottom = () => {const pageElement = pageRef.current.current || pageRef.current.el;pageElement.scrollTo({top: pageElement.scrollHeight - pageElement.offsetHeight,behavior: initiallyScrolled.current ? 'smooth' : 'auto',});};useEffect(() => {scrollToBottom();initiallyScrolled.current = true;}, [messagesData]);const handleSendClick = () => {const text = messageText.replace(//g, '<br>').trim();const type = 'sent';const messagesToSend = [];if (text.length) {messagesToSend.push({text,type,});}if (messagesToSend.length === 0) {return;}setMessagesData([...messagesData, ...messagesToSend]);setMessageText('');};const inputOpacity = messageText ? 1 : 0.3;const isClickable = messageText.trim().length > 0;const currentDate = new Intl.DateTimeFormat('en-US', {weekday: 'long',month: 'short',day: 'numeric',hour12: false,hour: '2-digit',minute: '2-digit',}).formatToParts(new Date()).map((part) => {if (['weekday', 'month', 'day'].includes(part.type)) {return <b key={part.type}>{part.value}</b>;}return part.value;});return (<Page className="ios:bg-white ios:dark:bg-black" ref={pageRef}><Navbartitle="Messages"/><Messages><MessagesTitle>{currentDate}</MessagesTitle>{messagesData.map((message, index) => (<Messagekey={index}type={message.type}name={message.name}text={message.text}avatar={message.type === 'received' && (<imgalt="avatar"src={message.avatar}className="w-8 h-8 rounded-full"/>)}/>))}</Messages><Messagebarplaceholder="Message"value={messageText}onInput={(e) => setMessageText(e.target.value)}left={<Link onClick={() => console.log('click')} toolbar iconOnly><Iconios={<CameraFill className="w-7 h-7" />}material={<MdCameraAlt className="w-6 h-6 fill-black dark:fill-md-dark-on-surface" />}/></Link>}right={<LinkonClick={isClickable ? handleSendClick : undefined}toolbarstyle={{opacity: inputOpacity,cursor: isClickable ? 'pointer' : 'default',}}><Iconios={<ArrowUpCircleFill className="w-7 h-7" />}material={<MdSend className="w-6 h-6 fill-black dark:fill-md-dark-on-surface" />}/></Link>}/></Page>);}