news/app/components/list/NewsList.tsx

88 lines
2.9 KiB
TypeScript
Raw Normal View History

2025-11-18 15:17:17 +08:00
import React from 'react';
import { mockNewsData } from './NewsData'; // 导入新闻数据
interface NewsProps {
2025-11-18 16:20:33 +08:00
title?: string;
time?: string;
2025-11-18 15:17:17 +08:00
url?: string;
}
const NewsItem: React.FC<NewsProps> = ({ title = '', time = '', url = '' }) => {
return (
2025-11-18 16:20:33 +08:00
<div className="mb-4">
<div
2025-11-19 12:39:43 +08:00
onClick={() => url && window.open(url)} // 点击时打开链接
2025-11-18 16:20:33 +08:00
className="flex items-center justify-between hover:text-blue-600 cursor-pointer transition duration-300 ease-in-out"
>
2025-11-19 12:39:43 +08:00
<h3 className="text-lg font-semibold text-gray-800 hover:text-blue-600 transition duration-300 ease-in-out">{title}</h3>
2025-11-18 16:20:33 +08:00
<p className="text-sm text-gray-500">{time}</p>
2025-11-18 15:17:17 +08:00
</div>
</div>
);
};
// 使用新闻数据渲染列表
const NewsList: React.FC = () => {
2025-11-19 08:10:19 +08:00
// 按类型过滤新闻
const techNews = mockNewsData.filter((news) => news.type === "科技");
const educationNews = mockNewsData.filter((news) => news.type === "教育");
2025-11-18 15:17:17 +08:00
return (
2025-11-19 16:06:11 +08:00
<div className=" p-8 rounded-2xl w-4/5 mx-auto">
2025-11-19 12:39:43 +08:00
{/* 使用 Flexbox 将两个列表放在一行 */}
<div className="flex gap-8">
2025-11-18 16:20:33 +08:00
{/* 科技新闻 */}
2025-11-19 12:39:43 +08:00
<div className="flex-1 bg-white rounded-lg shadow-md">
{/* 标题栏:独立于列表之外 */}
<div className="flex items-center justify-between mb-6">
2025-11-19 15:35:19 +08:00
<div className="bg-[#1c6cab] text-white px-6 py-3 font-bold text-4xl">
2025-11-19 12:39:43 +08:00
</div>
2025-11-19 15:35:19 +08:00
<button className="text-base text-blue-200 hover:text-blue-400 transition duration-200 pl-6 pr-6">
2025-11-18 16:20:33 +08:00
</button>
</div>
2025-11-19 12:39:43 +08:00
{/* 新闻列表 */}
<ul className="space-y-4 pl-6 pr-6 pb-6">
2025-11-19 08:10:19 +08:00
{techNews.map((news) => (
2025-11-19 12:39:43 +08:00
<NewsItem
key={news.id}
title={news.title}
time={news.time}
url={news.url} // 确保 mockNewsData 中有 url 字段
/>
2025-11-18 16:20:33 +08:00
))}
</ul>
</div>
2025-11-19 08:10:19 +08:00
{/* 教育新闻 */}
2025-11-19 12:39:43 +08:00
<div className="flex-1 bg-white rounded-lg shadow-md">
{/* 标题栏:独立于列表之外 */}
<div className="flex items-center justify-between mb-6">
2025-11-19 15:35:19 +08:00
<div className="bg-[#1c6cab] text-white px-6 py-3 font-bold text-4xl">
2025-11-19 12:39:43 +08:00
</div>
2025-11-19 15:35:19 +08:00
<button className="text-base text-blue-200 hover:text-blue-400 transition duration-200 pl-6 pr-6">
2025-11-18 16:20:33 +08:00
</button>
</div>
2025-11-19 12:39:43 +08:00
{/* 新闻列表 */}
<ul className="space-y-4 pl-6 pr-6 pb-6">
2025-11-19 08:10:19 +08:00
{educationNews.map((news) => (
2025-11-19 12:39:43 +08:00
<NewsItem
key={news.id}
title={news.title}
time={news.time}
url={news.url} // 确保 mockNewsData 中有 url 字段
/>
2025-11-18 16:20:33 +08:00
))}
</ul>
</div>
</div>
2025-11-18 15:17:17 +08:00
</div>
);
};
export default NewsList;