Certainly! Here's how you could implement this multi-step form in React using TypeScript and CSS modules. We'll create a form with different components for each step and manage the state and navigation between steps in a parent component.
First, ensure you have the required dependencies installed:
npm install react react-dom typescript @types/react @types/react-dom
npx create-react-app my-app --template typescript
cd my-app
npm start
Here's the structure of the application and necessary files:
src/
components/
ChooseProjectType.tsx
ChooseTools.tsx
TeamSizePayment.tsx
App.tsx
App.module.css
index.tsx
App.module.css
.container {
display: flex;
flex-direction: column;
align-items: center;
width: 80%;
margin: auto;
}
.button {
background-color: purple;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
margin-top: 20px;
}
.button:disabled {
background-color: grey;
cursor: not-allowed;
}
input, select {
padding: 10px;
margin: 10px 0;
width: 100%;
max-width: 300px;
}
h2 {
color: purple;
}
App.tsx
import React, { useState } from 'react';
import styles from './App.module.css';
import ChooseProjectType from './components/ChooseProjectType';
import ChooseTools from './components/ChooseTools';
import TeamSizePayment from './components/TeamSizePayment';
const App: React.FC = () => {
const [step, setStep] = useState(1);
const [projectType, setProjectType] = useState<string | null>(null);
const [tools, setTools] = useState<string[]>([]);
const [teamSize, setTeamSize] = useState<number>(2);
const [paymentMethod, setPaymentMethod] = useState<string | null>(null);
const nextStep = () => setStep(prev => prev + 1);
const prevStep = () => setStep(prev => Math.max(prev - 1, 1));
return (
<div className={styles.container}>
{step === 1 && (
<ChooseProjectType
projectType={projectType}
setProjectType={setProjectType}
nextStep={nextStep}
/>
)}
{step === 2 && (
<ChooseTools
tools={tools}
setTools={setTools}
nextStep={nextStep}
prevStep={prevStep}
/>
)}
{step === 3 && (
<TeamSizePayment
teamSize={teamSize}
setTeamSize={setTeamSize}
paymentMethod={paymentMethod}
setPaymentMethod={setPaymentMethod}
prevStep={prevStep}
/>
)}
</div>
);
}
export default App;
ChooseProjectType.tsx
import React from 'react';
import styles from './ChooseProjectType.module.css';
interface Props {
projectType: string | null;
setProjectType: (type: string) => void;
nextStep: () => void;
}
const ChooseProjectType: React.FC<Props> = ({ projectType, setProjectType, nextStep }) => {
const projectTypes = [
'Mobile App', 'Web App', 'Data Science', 'Gen AI Feature',
'Rapid Prototype', 'Marketing Website', 'Technology Migration', 'System Architecture'
];
const handleClick = (type: string) => {
setProjectType(type);
};
return (
<div>
<h2>Choose Project Type</h2>
<ul className={styles.list}>
{projectTypes.map(type => (
<li key={type} className={styles.listItem}>
<button
className={styles.button}
onClick={() => handleClick(type)}
style={{ backgroundColor: projectType === type ? 'green' : 'purple' }}
>
{type}
</button>
</li>
))}
</ul>
<button
className={styles.button}
onClick={nextStep}
disabled={!projectType}
>
Next
</button>
</div>
);
};
export default ChooseProjectType;
ChooseTools.tsx
import React from 'react';
import styles from './ChooseTools.module.css';
interface Props {
tools: string[];
setTools: (tools: string[]) => void;
nextStep: () => void;
prevStep: () => void;
}
const ChooseTools: React.FC<Props> = ({ tools, setTools, nextStep, prevStep }) => {
const availableTools = ['Python', 'Java', 'JavaScript', 'TypeScript', 'Node.js', 'React', 'AWS', 'Azure', 'Fullstack'];
const handleClick = (tool: string) => {
if (tools.includes(tool)) {
setTools(tools.filter(t => t !== tool));
} else {
if (tools.length < 10) setTools([...tools, tool]);
}
};
return (
<div>
<h2>Choose Tools (max. 10)</h2>
<ul className={styles.list}>
{availableTools.map(tool => (
<li key={tool} className={styles.listItem}>
<button
className={styles.button}
onClick={() => handleClick(tool)}
style={{ backgroundColor: tools.includes(tool) ? 'green' : 'purple' }}
>
{tool}
</button>
</li>
))}
</ul>
<button className={styles.button} onClick={prevStep}>
Back
</button>
<button className={styles.button} onClick={nextStep} disabled={tools.length === 0}>
Next
</button>
</div>
);
};
export default ChooseTools;
TeamSizePayment.tsx
import React from 'react';
import styles from './TeamSizePayment.module.css';
interface Props {
teamSize: number;
setTeamSize: (size: number) => void;
paymentMethod: string | null;
setPaymentMethod: (method: string) => void;
prevStep: () => void;
}
const TeamSizePayment: React.FC<Props> = ({ teamSize, setTeamSize, paymentMethod, setPaymentMethod, prevStep }) => {
const paymentMethods = ['Credit Card', 'PayPal', 'Crypto'];
return (
<div>
<h2>Team size (min. 2, max. 20) and payment</h2>
<label>
Number of team members:
<input
type="number"
value={teamSize}
min={2}
max={20}
onChange={(e) => setTeamSize(Number(e.target.value))}
/>
</label>
<label>
Payment method:
<select value={paymentMethod || ''} onChange={(e) => setPaymentMethod(e.target.value)}>
<option value="" disabled>Select payment method</option>
{paymentMethods.map(method => (
<option key={method} value={method}>{method}</option>
))}
</select>
</label>
<button className={styles.button} onClick={prevStep}>
Back
</button>
<button className={styles.button} onClick={() => alert('Team created successfully!')} disabled={!paymentMethod}>
Create Team
</button>
</div>
);
};
export default TeamSizePayment;
This should cover all the requirements and functionality for the multi-step form in a React + TypeScript application using CSS modules for styling.