import * as React from "react";
import { useTheme } from "@mui/material/styles";
import Box from "@mui/material/Box";
import MobileStepper from "@mui/material/MobileStepper";
import Paper from "@mui/material/Paper";
import Typography from "@mui/material/Typography";
import Button from "@mui/material/Button";
import KeyboardArrowLeft from "@mui/icons-material/KeyboardArrowLeft";
import KeyboardArrowRight from "@mui/icons-material/KeyboardArrowRight";
import SwipeableViews from "react-swipeable-views";
import { autoPlay } from "react-swipeable-views-utils";
const AutoPlaySwipeableViews = autoPlay(SwipeableViews);
const images = [
{
label: "San Francisco – Oakland Bay Bridge, United States",
imgPath:
"https://images.unsplash.com/photo-1537944434965-cf4679d1a598?auto=format&fit=crop&w=400&h=250&q=60",
},
{
label: "Bird",
imgPath:
"https://images.unsplash.com/photo-1538032746644-0212e812a9e7?auto=format&fit=crop&w=400&h=250&q=60",
},
{
label: "Bali, Indonesia",
imgPath:
"https://images.unsplash.com/photo-1537996194471-e657df975ab4?auto=format&fit=crop&w=400&h=250",
},
{
label: "Goč, Serbia",
imgPath:
"https://images.unsplash.com/photo-1512341689857-198e7e2f3ca8?auto=format&fit=crop&w=400&h=250&q=60",
},
];
export default function Hero() {
const theme = useTheme();
const [activeStep, setActiveStep] = React.useState(0);
const maxSteps = images.length;
const handleNext = () => {
setActiveStep((prevActiveStep) => prevActiveStep + 1);
};
const handleBack = () => {
setActiveStep((prevActiveStep) => prevActiveStep - 1);
};
const handleStepChange = (step: number) => {
setActiveStep(step);
};
return (
<Box sx={{ maxWidth: 400, flexGrow: 1 }}>
<Paper
square
elevation={0}
sx={{
display: "flex",
alignItems: "center",
height: 50,
pl: 2,
bgcolor: "background.default",
}}
>
<Typography>{images[activeStep].label}</Typography>
</Paper>
<AutoPlaySwipeableViews
axis={theme.direction === "rtl" ? "x-reverse" : "x"}
index={activeStep}
onChangeIndex={handleStepChange}
enableMouseEvents
>
{images.map((step, index) => (
<div key={step.label}>
{Math.abs(activeStep - index) <= 2 ? (
<Box
component="img"
sx={{
height: 255,
display: "block",
maxWidth: 400,
overflow: "hidden",
width: "100%",
}}
src={step.imgPath}
alt={step.label}
/>
) : null}
</div>
))}
</AutoPlaySwipeableViews>
<MobileStepper
steps={maxSteps}
position="static"
activeStep={activeStep}
nextButton={
<Button
size="small"
onClick={handleNext}
disabled={activeStep === maxSteps - 1}
>
Next
{theme.direction === "rtl" ? (
<KeyboardArrowLeft />
) : (
<KeyboardArrowRight />
)}
</Button>
}
backButton={
<Button size="small" onClick={handleBack} disabled={activeStep === 0}>
{theme.direction === "rtl" ? (
<KeyboardArrowRight />
) : (
<KeyboardArrowLeft />
)}
Back
</Button>
}
/>
</Box>
);
}
这是我想在 App.tsx 中实现的组件,但它不起作用。当我尝试这样做时,我收到了此错误。
{
"name": "my-typescript-project",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
},
"dependencies": {
"@emotion/react": "^11.11.4",
"@emotion/styled": "^11.11.0",
"@mui/icons-material": "^5.15.11",
"@mui/material": "^5.15.11",
"@mui/styled-engine-sc": "^6.0.0-alpha.17",
"@types/react-swipeable-views": "^0.14.0",
"@types/react-swipeable-views-utils": "^0.14.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-hook-form": "^7.51.0",
"react-router-dom": "^6.22.2",
"react-swipeable-views-react-18-fix": "^0.14.1",
"styled-components": "^6.1.8",
"zod": "^3.22.4"
},
"devDependencies": {
"@types/react": "^18.2.56",
"@types/react-dom": "^18.2.19",
"@typescript-eslint/eslint-plugin": "^7.0.2",
"@typescript-eslint/parser": "^7.0.2",
"@vitejs/plugin-react": "^4.2.1",
"autoprefixer": "^10.4.18",
"eslint": "^8.56.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.5",
"postcss": "^8.4.35",
"tailwindcss": "^3.4.1",
"typescript": "^5.2.2",
"vite": "^5.1.4"
}
}
这是我的package.json,我尝试更改了很多版本,但仍然不起作用。
如果有人有同样的问题或知道如何解决这个问题。请告诉我。谢谢。
您仅安装了
react-swipeable-views
包的类型定义。您需要安装软件包本身。
将其添加到您的 package.json
dependencies
:
"react-swipeable-views":"~0.14.0",
我还建议将类型定义移至您的
devDependencies
:
"@types/react-swipeable-views": "^0.14.0",
"@types/react-swipeable-views-utils": "^0.14.0",