news/app/components/news/list/NewsList.tsx

88 lines
2.9 KiB
TypeScript

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