この記事は 2025 年 6 月 24 日に投稿しました。
目次
リンク
1. はじめに
こんにちは、iOS のエディタアプリPWEditorとその後継PWEditor2の開発者の二俣です。
今回はReactのMaterial UIでモバイル用のステッパーを表示する方法についてです。
2. ReactのMaterial UIでモバイル用のステッパーを表示する
ReactのMaterail UIでモバイル用のステッパーを表示するには、以下のような実装にします。
実装例
※create-react-appで作成したTypeScriptのプロジェクトです。
index.tsx
import React from "react"; import ReactDOM from "react-dom/client"; import App from "./App"; const root = ReactDOM.createRoot( document.getElementById("root") as HTMLElement ); root.render( <React.StrictMode> <App /> </React.StrictMode> );
App.tsx
import * as React from "react"; import Box from "@mui/material/Box"; import { useTheme } from "@mui/material/styles"; 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"; const steps = [ { label: "Select campaign settings", description: `For each ad campaign that you create, you can control how much you're willing to spend on clicks and conversions, which networks and geographical locations you want your ads to show on, and more.`, }, { label: "Create an ad group", description: "An ad group contains one or more ads which target a shared set of keywords.", }, { label: "Create an ad", description: `Try out different ad text to see what brings in the most customers, and learn how to enhance your ads using features like ad extensions. If you run into any problems with your ads, find out how to tell if they're running and how to resolve approval issues.`, }, ]; export default function TextMobileStepper() { const theme = useTheme(); const [activeStep, setActiveStep] = React.useState(0); const maxSteps = steps.length; const handleNext = () => { setActiveStep((prevActiveStep) => prevActiveStep + 1); }; const handleBack = () => { setActiveStep((prevActiveStep) => prevActiveStep - 1); }; return ( <Box sx={{ maxWidth: 400, flexGrow: 1, margin: 4 }}> <Paper square elevation={0} sx={{ display: "flex", alignItems: "center", height: 50, pl: 2, bgcolor: "background.default", }} > <Typography>{steps[activeStep].label}</Typography> </Paper> <Box sx={{ height: 255, maxWidth: 400, width: "100%", p: 2 }}> {steps[activeStep].description} </Box> <MobileStepper variant="text" 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> ); }
実行結果
リファレンス
3. おわりに
モバイル用のステッパーは1つづつ表示できるようです。
リンク
紹介している一部の記事のコードはGitlabで公開しています。
興味のある方は覗いてみてください。
私が勤務しているニューラルでは、主に組み込み系ソフトの開発を行っております。
弊社製品のハイブリッド OS Bi-OSは高い技術力を評価されており、特に制御系や通信系を得意としています。
私自身はiOS モバイルアプリやウィンドウズアプリを得意としております。
ソフトウェア開発に関して相談などございましたら、お気軽にご連絡ください。
また一緒に働きたい技術者の方も随時募集中です。
興味がありましたらご連絡ください。
EMAIL : info-nr@newral.co.jp / m-futamata@newral.co.jp
TEL : 042-523-3663
FAX : 042-540-1688