|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { Form, Input, InputNumber, Steps, Button, Flex, Result } from '@tiny-design/react'; |
| 3 | + |
| 4 | +const stepFields: string[][] = [ |
| 5 | + ['username', 'email'], |
| 6 | + ['fullName', 'age', 'phone'], |
| 7 | +]; |
| 8 | + |
| 9 | +export default function StepFormDemo() { |
| 10 | + const [form] = Form.useForm({ |
| 11 | + username: '', |
| 12 | + email: '', |
| 13 | + fullName: '', |
| 14 | + age: '', |
| 15 | + phone: '', |
| 16 | + }); |
| 17 | + const [current, setCurrent] = useState(0); |
| 18 | + |
| 19 | + const validateStep = (step: number): boolean => { |
| 20 | + const fields = stepFields[step]; |
| 21 | + if (!fields) return true; |
| 22 | + fields.forEach((name) => form.validateField(name)); |
| 23 | + return fields.every((name) => !form.getFieldError(name)); |
| 24 | + }; |
| 25 | + |
| 26 | + const handleNext = () => { |
| 27 | + if (validateStep(current)) { |
| 28 | + setCurrent(current + 1); |
| 29 | + } |
| 30 | + }; |
| 31 | + |
| 32 | + const handlePrev = () => { |
| 33 | + setCurrent(current - 1); |
| 34 | + }; |
| 35 | + |
| 36 | + const handleFinish = () => { |
| 37 | + setCurrent(3); |
| 38 | + }; |
| 39 | + |
| 40 | + const handleReset = () => { |
| 41 | + form.resetFields(); |
| 42 | + setCurrent(0); |
| 43 | + }; |
| 44 | + |
| 45 | + const stepStyle = (step: number): React.CSSProperties => |
| 46 | + current !== step ? { display: 'none' } : {}; |
| 47 | + |
| 48 | + return ( |
| 49 | + <div style={{ maxWidth: 600 }}> |
| 50 | + <Steps current={current} style={{ marginBottom: 24 }}> |
| 51 | + <Steps.Step title="Account" description="Login credentials" /> |
| 52 | + <Steps.Step title="Profile" description="Personal info" /> |
| 53 | + <Steps.Step title="Confirm" description="Review & submit" /> |
| 54 | + <Steps.Step title="Done" description="Registration complete" /> |
| 55 | + </Steps> |
| 56 | + |
| 57 | + {current < 3 ? ( |
| 58 | + <Form form={form} onFinish={handleFinish} noValidate> |
| 59 | + <div style={stepStyle(0)}> |
| 60 | + <Form.Item |
| 61 | + label="Username" |
| 62 | + name="username" |
| 63 | + rules={[ |
| 64 | + { required: true, message: 'Username is required' }, |
| 65 | + { min: 3, message: 'At least 3 characters' }, |
| 66 | + ]} |
| 67 | + > |
| 68 | + <Input placeholder="Enter username" /> |
| 69 | + </Form.Item> |
| 70 | + <Form.Item |
| 71 | + label="Email" |
| 72 | + name="email" |
| 73 | + rules={[ |
| 74 | + { required: true, message: 'Email is required' }, |
| 75 | + { |
| 76 | + pattern: /^[^\s@]+@[^\s@]+\.[^\s@]+$/, |
| 77 | + message: 'Please enter a valid email', |
| 78 | + }, |
| 79 | + ]} |
| 80 | + > |
| 81 | + <Input placeholder="Enter email" /> |
| 82 | + </Form.Item> |
| 83 | + </div> |
| 84 | + |
| 85 | + <div style={stepStyle(1)}> |
| 86 | + <Form.Item |
| 87 | + label="Full Name" |
| 88 | + name="fullName" |
| 89 | + rules={[{ required: true, message: 'Full name is required' }]} |
| 90 | + > |
| 91 | + <Input placeholder="Enter full name" /> |
| 92 | + </Form.Item> |
| 93 | + <Form.Item |
| 94 | + label="Age" |
| 95 | + name="age" |
| 96 | + rules={[ |
| 97 | + { required: true, message: 'Age is required' }, |
| 98 | + ]} |
| 99 | + > |
| 100 | + <InputNumber min={1} max={120} placeholder="Enter age" /> |
| 101 | + </Form.Item> |
| 102 | + <Form.Item |
| 103 | + label="Phone" |
| 104 | + name="phone" |
| 105 | + rules={[ |
| 106 | + { required: true, message: 'Phone number is required' }, |
| 107 | + { pattern: /^\d{7,15}$/, message: 'Please enter a valid phone number' }, |
| 108 | + ]} |
| 109 | + > |
| 110 | + <Input placeholder="Enter phone number" /> |
| 111 | + </Form.Item> |
| 112 | + </div> |
| 113 | + |
| 114 | + <div style={stepStyle(2)}> |
| 115 | + <Form.Item label="Username"> |
| 116 | + <span>{form.getFieldValue('username')}</span> |
| 117 | + </Form.Item> |
| 118 | + <Form.Item label="Email"> |
| 119 | + <span>{form.getFieldValue('email')}</span> |
| 120 | + </Form.Item> |
| 121 | + <Form.Item label="Full Name"> |
| 122 | + <span>{form.getFieldValue('fullName')}</span> |
| 123 | + </Form.Item> |
| 124 | + <Form.Item label="Age"> |
| 125 | + <span>{form.getFieldValue('age')}</span> |
| 126 | + </Form.Item> |
| 127 | + <Form.Item label="Phone"> |
| 128 | + <span>{form.getFieldValue('phone')}</span> |
| 129 | + </Form.Item> |
| 130 | + </div> |
| 131 | + |
| 132 | + <Form.Item> |
| 133 | + <Flex gap="sm"> |
| 134 | + {current > 0 && ( |
| 135 | + <Button type="button" onClick={handlePrev}> |
| 136 | + Previous |
| 137 | + </Button> |
| 138 | + )} |
| 139 | + {current < 2 && ( |
| 140 | + <Button btnType="primary" type="button" onClick={handleNext}> |
| 141 | + Next |
| 142 | + </Button> |
| 143 | + )} |
| 144 | + {current === 2 && ( |
| 145 | + <Button btnType="primary" type="submit"> |
| 146 | + Submit |
| 147 | + </Button> |
| 148 | + )} |
| 149 | + </Flex> |
| 150 | + </Form.Item> |
| 151 | + </Form> |
| 152 | + ) : ( |
| 153 | + <Result |
| 154 | + status="success" |
| 155 | + title="Registration Successful!" |
| 156 | + subtitle={`Welcome, ${form.getFieldValue('fullName')}. Your account "${form.getFieldValue('username')}" has been created.`} |
| 157 | + extra={ |
| 158 | + <Button btnType="primary" onClick={handleReset}> |
| 159 | + Register Another |
| 160 | + </Button> |
| 161 | + } |
| 162 | + /> |
| 163 | + )} |
| 164 | + </div> |
| 165 | + ); |
| 166 | +} |
0 commit comments