Home.jsx 4.65 KB
import React, { useState } from 'react';
import { Link, useNavigate } from 'react-router-dom';

const Home = () => {
  const [searchTerm, setSearchTerm] = useState('');
  const navigate = useNavigate();

  const handleSearch = (e) => {
    e.preventDefault();
    if (searchTerm.trim()) {
      navigate(`/products?search=${encodeURIComponent(searchTerm)}`);
    }
  };

  return (
    <div style={containerStyle}>
      <section style={heroStyle}>
        <h1 style={heroTitleStyle}>欢迎来到普罗米修甄选水果平台</h1>
        
        {/* 搜索框 */}
        <div style={searchContainerStyle}>
          <form onSubmit={handleSearch} style={searchFormStyle}>
            <input
              type="text"
              placeholder="搜索水果..."
              value={searchTerm}
              onChange={(e) => setSearchTerm(e.target.value)}
              style={searchInputStyle}
            />
            <button type="submit" style={searchButtonStyle}>
              搜索
            </button>
          </form>
        </div>
      </section>

      <section style={categoriesStyle}>
        <h2 style={sectionTitleStyle}>热门果蔬</h2>
        <div style={categoriesGridStyle}>
          <Link to="/products?category=electronics" style={categoryCardStyle}>
            <h3>秦岭猕猴桃</h3>
            <p>秀色可餐,果香浓郁</p>
          </Link>
          <Link to="/products?category=clothing" style={categoryCardStyle}>
            <h3>栖霞红富士苹果</h3>
            <p>红艳诱人,脆爽多汁</p>
          </Link>
          <Link to="/products?category=home" style={categoryCardStyle}>
            <h3>长安石榴</h3>
            <p>晶莹剔透,粉黛抹腮</p>
          </Link>
          <Link to="/products?category=sports" style={categoryCardStyle}>
            <h3>鄠邑葡萄</h3>
            <p>甜而不腻,皮薄肉厚</p>
          </Link>
        </div>
      </section>
    </div>
  );
};

const containerStyle = {
  minHeight: 'calc(100vh - 200px)',
};

const heroStyle = {
  background: 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)',
  color: 'white',
  padding: '1.5rem 1rem', // 降低内边距
  textAlign: 'center',
  minHeight: '35vh', // 降低高度
  display: 'flex',
  flexDirection: 'column',
  justifyContent: 'center',
  alignItems: 'center',
};

const heroTitleStyle = {
  fontSize: '2rem', // 减小字体
  marginBottom: '0.8rem', // 减小边距
  fontWeight: 'bold',
};

const heroSubtitleStyle = {
  fontSize: '1rem', // 减小字体
  marginBottom: '1.5rem', // 减小边距
  opacity: 0.9,
  maxWidth: '500px', // 减小宽度
  margin: '0 auto 1.5rem', // 减小边距
  lineHeight: '1.5',
};

// 搜索框样式
const searchContainerStyle = {
  width: '100%',
  maxWidth: '500px', // 减小宽度
  margin: '0 auto',
};

const searchFormStyle = {
  display: 'flex',
  background: 'white',
  borderRadius: '50px',
  overflow: 'hidden',
  boxShadow: '0 8px 25px rgba(0, 0, 0, 0.15)',
  transition: 'transform 0.3s ease, box-shadow 0.3s ease',
};

const searchInputStyle = {
  flex: 1,
  border: 'none',
  outline: 'none',
  padding: '0.8rem 1.2rem', // 减小内边距
  fontSize: '0.9rem', // 减小字体
  color: '#333',
  background: 'transparent',
};

const searchButtonStyle = {
  background: 'linear-gradient(135deg, #ff6b6b, #ee5a52)',
  color: 'white',
  border: 'none',
  padding: '0.8rem 1.5rem', // 减小内边距
  fontSize: '0.9rem', // 减小字体
  fontWeight: 'bold',
  cursor: 'pointer',
  transition: 'all 0.3s ease',
};

const categoriesStyle = {
  padding: '3rem 1rem', // 保持适当内边距
  backgroundColor: '#f8f9fa',
  minHeight: '50vh', // 增加高度
};

const sectionTitleStyle = {
  textAlign: 'center',
  fontSize: '2.2rem', // 稍微调整字体
  marginBottom: '2.5rem', // 调整边距
  color: '#333',
};

const categoriesGridStyle = {
  display: 'grid',
  gridTemplateColumns: 'repeat(auto-fit, minmax(250px, 1fr))',
  gap: '2rem',
  maxWidth: '1200px',
  margin: '0 auto',
};

const categoryCardStyle = {
  backgroundColor: 'white',
  padding: '2rem',
  borderRadius: '10px',
  textAlign: 'center',
  textDecoration: 'none',
  color: '#333',
  boxShadow: '0 4px 6px rgba(0, 0, 0, 0.1)',
  transition: 'transform 0.3s, box-shadow 0.3s',
  border: '1px solid #e0e0e0',
};

// 添加悬停效果
Object.assign(categoryCardStyle, {
  ':hover': {
    transform: 'translateY(-5px)',
    boxShadow: '0 8px 25px rgba(0, 0, 0, 0.15)',
  }
});

Object.assign(searchFormStyle, {
  ':hover': {
    transform: 'translateY(-2px)',
    boxShadow: '0 12px 35px rgba(0, 0, 0, 0.2)',
  }
});

Object.assign(searchButtonStyle, {
  ':hover': {
    background: 'linear-gradient(135deg, #ff5252, #e53935)',
  }
});

export default Home;