この記事は 2025 年 6 月 22 日に投稿しました。
目次
リンク
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 Stepper from "@mui/material/Stepper"; import Step from "@mui/material/Step"; import StepLabel from "@mui/material/StepLabel"; import Typography from "@mui/material/Typography"; const steps = [ "Select campaign settings", "Create an ad group", "Create an ad", ]; export default function App() { const isStepFailed = (step: number) => { return step === 1; }; return ( <Box sx={{ width: "30%", margin: 8 }}> <Stepper activeStep={1}> {steps.map((label, index) => { const labelProps: { optional?: React.ReactNode; error?: boolean; } = {}; if (isStepFailed(index)) { labelProps.optional = ( <Typography variant="caption" color="error"> Alert message </Typography> ); labelProps.error = true; } return ( <Step key={label}> <StepLabel {...labelProps}>{label}</StepLabel> </Step> ); })} </Stepper> </Box> ); }
実行結果
リファレンス
3. おわりに
ラベルをアイコンの下に表示できると、表示幅を節約できます。
リンク
紹介している一部の記事のコードはGitlabで公開しています。
興味のある方は覗いてみてください。
私が勤務しているニューラルでは、主に組み込み系ソフトの開発を行っております。
弊社製品のハイブリッド OS Bi-OSは高い技術力を評価されており、特に制御系や通信系を得意としています。
私自身はiOS モバイルアプリやウィンドウズアプリを得意としております。
ソフトウェア開発に関して相談などございましたら、お気軽にご連絡ください。
また一緒に働きたい技術者の方も随時募集中です。
興味がありましたらご連絡ください。
EMAIL : info-nr@newral.co.jp / m-futamata@newral.co.jp
TEL : 042-523-3663
FAX : 042-540-1688