🔥 认识我们的新项目 t0ggles - 你终极的项目管理工具! 🔥

消息栏 React 组件

消息栏是与消息一起使用的工具栏

消息栏组件

以下组件包含在内

  • 消息栏

消息栏属性

名称类型默认描述
颜色对象
colors.bgIos字符串'bg-white dark:bg-black'
colors.bgMaterial字符串'bg-md-light-surface dark:bg-md-dark-surface'
colors.borderIos字符串'border-[#c8c8cd] dark:border-white dark:border-opacity-30'
colors.inputBgIos字符串'bg-transparent'
colors.inputBgMd字符串'bg-md-light-surface-2 dark:bg-md-dark-surface-variant'
colors.placeholderIos字符串'dark:placeholder-white dark:placeholder-opacity-40'
colors.placeholderMd字符串'placeholder-md-light-on-surface-variant dark:placeholder-md-dark-on-surface-variant'
colors.toolbarIconIos字符串'fill-primary dark:fill-md-dark-primary'
colors.toolbarIconMd字符串'fill-black'
组件字符串'div'

组件的 HTML 元素

禁用布尔值未定义

设置 "disabled" 文本区域属性

ID字符串

消息栏 id 属性

ReactNode

消息栏 "左侧" 区域的内容

leftClassName字符串

附加左侧样式

名称字符串

消息栏名称

大纲布尔值

呈现外部发际线(边框)。如果未指定,将为 iOS 主题启用

占位符字符串 | 数字'Message'

消息栏占位符

ReactNode

消息栏 "右侧" 区域的内容

rightClassName字符串

附加右侧样式

大小字符串 | 数字

文本区域的原生 "size" 属性的值

样式CSSProperties

附加消息栏类

textareaId字符串

文本区域 "id" 属性

价值任何

消息栏值

onChangefunction(e)

change 事件处理程序

onInputfunction(e)

input 事件处理程序

例子

Messages.jsx
/* 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}>
<Navbar
title="Messages"
/>
<Messages>
<MessagesTitle>{currentDate}</MessagesTitle>
{messagesData.map((message, index) => (
<Message
key={index}
type={message.type}
name={message.name}
text={message.text}
avatar={
message.type === 'received' && (
<img
alt="avatar"
src={message.avatar}
className="w-8 h-8 rounded-full"
/>
)
}
/>
))}
</Messages>
<Messagebar
placeholder="Message"
value={messageText}
onInput={(e) => setMessageText(e.target.value)}
left={
<Link onClick={() => console.log('click')} toolbar iconOnly>
<Icon
ios={<CameraFill className="w-7 h-7" />}
material={
<MdCameraAlt className="w-6 h-6 fill-black dark:fill-md-dark-on-surface" />
}
/>
</Link>
}
right={
<Link
onClick={isClickable ? handleSendClick : undefined}
toolbar
style={{
opacity: inputOpacity,
cursor: isClickable ? 'pointer' : 'default',
}}
>
<Icon
ios={<ArrowUpCircleFill className="w-7 h-7" />}
material={
<MdSend className="w-6 h-6 fill-black dark:fill-md-dark-on-surface" />
}
/>
</Link>
}
/>
</Page>
);
}
代码许可证下 MIT.
2022 © Konsta UI by nolimits4web.