/
githubmirror
/
material-ui
Обзор
Документация
Войти
/
githubmirror
/
material-ui
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
docs/data/material/components/steppers/ProgressMobileStepper.tsx
83 строки
2 KB
Silviu Alexandru Avram
[docs][stepper] Fix focus management in examples (#48494)
11 май 2026, 20:28
Не верифицирован
11 май 2026, 20:28
0c0f7a2
Код
Авторство
О чём код?
import * as React from 'react'; import { useTheme } from '@mui/material/styles'; import MobileStepper from '@mui/material/MobileStepper'; import Button from '@mui/material/Button'; import KeyboardArrowLeft from '@mui/icons-material/KeyboardArrowLeft'; import KeyboardArrowRight from '@mui/icons-material/KeyboardArrowRight'; export default function ProgressMobileStepper() { const theme = useTheme(); const [activeStep, setActiveStep] = React.useState(0); const handleNext = () => { setActiveStep((prevActiveStep) => prevActiveStep + 1); }; const handleBack = () => { setActiveStep((prevActiveStep) => prevActiveStep - 1); }; const nextButtonRef = React.useRef<HTMLButtonElement>(null); const backButtonRef = React.useRef<HTMLButtonElement>(null); const previousActiveStepRef = React.useRef(activeStep); // Manage focus when the active step changes. React.useEffect(() => { const previousActiveStep = previousActiveStepRef.current; if (activeStep === 0 && previousActiveStep === 1) { // If the user is going back to the first step, focus the "Next" button. nextButtonRef.current!.focus(); } else if (activeStep === 5 && previousActiveStep === 4) { // If the user is going to the last step, focus the "Back" button. backButtonRef.current!.focus(); } previousActiveStepRef.current = activeStep; }, [activeStep]); return ( <MobileStepper variant="progress" steps={6} position="static" activeStep={activeStep} sx={{ maxWidth: 400, flexGrow: 1 }} slotProps={{ progress: { 'aria-label': 'stepper linear progress', }, }} nextButton={ <Button size="small" onClick={handleNext} disabled={activeStep === 5} ref={nextButtonRef} > Next {theme.direction === 'rtl' ? ( <KeyboardArrowLeft /> ) : ( <KeyboardArrowRight /> )} </Button> } backButton={ <Button size="small" onClick={handleBack} disabled={activeStep === 0} ref={backButtonRef} > {theme.direction === 'rtl' ? ( <KeyboardArrowRight /> ) : ( <KeyboardArrowLeft /> )} Back </Button> } /> ); }