Debian Codegraph @

node/@patternfly/react-core/src/deprecated/components/Wizard/examples/Wizard.md

raw
1 ---
2 id: Wizard
3 section: components
4 cssPrefix: pf-v6-c-wizard
5 propComponents:
6 ['Wizard', 'WizardNav', 'WizardNavItem', 'WizardHeader', 'WizardBody', 'WizardFooter', 'WizardToggle', 'WizardStep']
7 ouia: true
8 source: react-deprecated
9 deprecated: true
10 ---
11
12 import { Fragment, useEffect, useRef, useState } from 'react';
13 import { Button, Drawer, DrawerActions, DrawerCloseButton, DrawerColorVariant,
14 DrawerContent, DrawerContentBody, DrawerHead, DrawerPanelContent, DrawerSection, ModalVariant, Alert, EmptyState, EmptyStateFooter, EmptyStateBody, EmptyStateActions, Title, Progress, Form, FormGroup, TextInput } from '@patternfly/react-core';
15 import { Wizard as WizardDeprecated, WizardFooter as WizardFooterDeprecated, WizardContextConsumer as WizardContextConsumerDeprecated } from '@patternfly/react-core/deprecated';
16 import RhMicronsExternalLinkIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-external-link-icon';
17 import SlackHashIcon from '@patternfly/react-icons/dist/esm/icons/slack-hash-icon';
18 import CogsIcon from '@patternfly/react-icons/dist/esm/icons/cogs-icon';
19
20 If you seek a wizard solution that allows for more composition, see the [React](/components/wizard) tab.
21
22 ## Examples
23
24 ### Basic
25
26 ```js file="./WizardBasic.tsx"
27
28 ```
29
30 ### Basic with disabled steps
31
32 ```js file="./WizardBasicWithDisabledSteps.tsx"
33
34 ```
35
36 ### Anchors for nav items
37
38 ```js file="./WizardAnchorsForNavItems.tsx"
39
40 ```
41
42 ### Incrementally enabled steps
43
44 ```js file="./WizardIncrementallyEnabledSteps.tsx"
45
46 ```
47
48 ### Expandable steps
49
50 ```js file="./WizardExpandableSteps.tsx"
51
52 ```
53
54 ### Finished
55
56 ```js file="./WizardFinished.tsx"
57
58 ```
59
60 ### Enabled on form validation
61
62 ```js file="./WizardEnabledOnFormValidation.tsx"
63
64 ```
65
66 ### Validate on button press
67
68 This example demonstrates how to use the `WizardContextConsumer` to consume the `WizardContext`. `WizardContext` can be used to imperatively move to a specific wizard step.
69
70 The definition of the `WizardContext` is as follows:
71
72 ```
73 interface WizardContext {
74 goToStepById: (stepId: number | string) => void;
75 goToStepByName: (stepName: string) => void;
76 onNext: () => void;
77 onBack: () => void;
78 onClose: () => void;
79 activeStep: WizardStep;
80 }
81 ```
82
83 ```js file="./WizardValidateOnButtonPress.tsx"
84
85 ```
86
87 ### Progressive steps
88
89 ```js
90 import { Button, Radio, Alert } from '@patternfly/react-core';
91 import {
92 Wizard as WizardDeprecated,
93 WizardFooter as WizardFooterDeprecated,
94 WizardContextConsumer as WizardContextConsumerDeprecated
95 } from '@patternfly/react-core/deprecated';
96
97 class ProgressiveWizard extends React.Component {
98 constructor(props) {
99 super(props);
100 this.state = {
101 showCreateStep: false,
102 showUpdateStep: false,
103 showOptionsStep: false,
104 showReviewStep: false,
105 getStartedStepRadio: 'Create',
106 createStepRadio: 'Quick',
107 updateStepRadio: 'Quick'
108 };
109 this.closeWizard = () => {
110 console.log('close wizard');
111 };
112 this.onGoToStep = ({ id, name }, { prevId, prevName }) => {
113 // Remove steps after the currently clicked step
114 if (name === 'Get started') {
115 this.setState({
116 showReviewStep: false,
117 showOptionsStep: false,
118 showCreateStep: false,
119 showUpdateStep: false
120 });
121 } else if (name === 'Create options' || name === 'Update options') {
122 this.setState({
123 showReviewStep: false,
124 showOptionsStep: false
125 });
126 } else if (name.indexOf('Substep') > -1) {
127 this.setState({
128 showReviewStep: false
129 });
130 }
131 };
132 this.getNextStep = (activeStep, callback) => {
133 if (activeStep.name === 'Get started') {
134 if (this.state.getStartedStepRadio === 'Create') {
135 this.setState(
136 {
137 showCreateStep: true,
138 showUpdateStep: false,
139 showOptionsStep: false,
140 showReviewStep: false
141 },
142 () => {
143 callback();
144 }
145 );
146 } else {
147 this.setState(
148 {
149 showCreateStep: false,
150 showUpdateStep: true,
151 showOptionsStep: false,
152 showReviewStep: false
153 },
154 () => {
155 callback();
156 }
157 );
158 }
159 } else if (activeStep.name === 'Create options' || activeStep.name === 'Update options') {
160 this.setState(
161 {
162 showOptionsStep: true,
163 showReviewStep: false
164 },
165 () => {
166 callback();
167 }
168 );
169 } else if (activeStep.name === 'Substep 3') {
170 this.setState(
171 {
172 showReviewStep: true
173 },
174 () => {
175 callback();
176 }
177 );
178 } else {
179 callback();
180 }
181 };
182 this.getPreviousStep = (activeStep, callback) => {
183 if (activeStep.name === 'Review') {
184 this.setState(
185 {
186 showReviewStep: false
187 },
188 () => {
189 callback();
190 }
191 );
192 } else if (activeStep.name === 'Substep 1') {
193 this.setState(
194 {
195 showOptionsStep: false
196 },
197 () => {
198 callback();
199 }
200 );
201 } else if (activeStep.name === 'Create options') {
202 this.setState(
203 {
204 showCreateStep: false
205 },
206 () => {
207 callback();
208 }
209 );
210 } else if (activeStep.name === 'Update options') {
211 this.setState(
212 {
213 showUpdateStep: false
214 },
215 () => {
216 callback();
217 }
218 );
219 } else {
220 callback();
221 }
222 };
223 }
224 render() {
225 const {
226 stepsValid,
227 getStartedStepRadio,
228 createStepRadio,
229 updateStepRadio,
230 showCreateStep,
231 showUpdateStep,
232 showOptionsStep,
233 showReviewStep
234 } = this.state;
235 const getStartedStep = {
236 name: 'Get started',
237 component: (
238 <div>
239 <Radio
240 value="Create"
241 isChecked={getStartedStepRadio === 'Create'}
242 onChange={(event) => this.setState({ getStartedStepRadio: event.currentTarget.value })}
243 label="Create a new thing"
244 name="radio-step-start"
245 id="radio-step-start-1"
246 />{' '}
247 <Radio
248 value="Update"
249 isChecked={getStartedStepRadio === 'Update'}
250 onChange={(event) => this.setState({ getStartedStepRadio: event.currentTarget.value })}
251 label="Update an existing thing"
252 name="radio-step-start"
253 id="radio-step-start-2"
254 />
255 </div>
256 )
257 };
258 const createStep = {
259 name: 'Create options',
260 component: (
261 <div>
262 <Radio
263 value="Quick"
264 isChecked={createStepRadio === 'Quick'}
265 onChange={(event) => this.setState({ createStepRadio: event.currentTarget.value })}
266 label="Quick create"
267 name="radio-step-create"
268 id="radio-step-create-1"
269 />{' '}
270 <Radio
271 value="Custom"
272 isChecked={createStepRadio === 'Custom'}
273 onChange={(event) => this.setState({ createStepRadio: event.currentTarget.value })}
274 label="Custom create"
275 name="radio-step-create"
276 id="radio-step-create-2"
277 />
278 </div>
279 )
280 };
281 const updateStep = {
282 name: 'Update options',
283 component: (
284 <div>
285 <Radio
286 value="Quick"
287 isChecked={updateStepRadio === 'Quick'}
288 onChange={(event) => this.setState({ updateStepRadio: event.currentTarget.value })}
289 label="Quick update"
290 name="radio-step-update"
291 id="radio-step-update-1"
292 />{' '}
293 <Radio
294 value="Custom"
295 isChecked={updateStepRadio === 'Custom'}
296 onChange={(event) => this.setState({ updateStepRadio: event.currentTarget.value })}
297 label="Custom update"
298 name="radio-step-update"
299 id="radio-step-update-2"
300 />
301 </div>
302 )
303 };
304 const optionsStep = {
305 name: showCreateStep ? `${createStepRadio} Options` : `${updateStepRadio} Options`,
306 steps: [
307 {
308 name: 'Substep 1',
309 component: 'Substep 1'
310 },
311 {
312 name: 'Substep 2',
313 component: 'Substep 2'
314 },
315 {
316 name: 'Substep 3',
317 component: 'Substep 3'
318 }
319 ]
320 };
321 const reviewStep = {
322 name: 'Review',
323 component: (
324 <div>
325 <div>First choice: {getStartedStepRadio}</div>
326 <div>Second choice: {showCreateStep ? createStepRadio : updateStepRadio}</div>
327 </div>
328 )
329 };
330 const steps = [
331 getStartedStep,
332 ...(showCreateStep ? [createStep] : []),
333 ...(showUpdateStep ? [updateStep] : []),
334 ...(showOptionsStep ? [optionsStep] : []),
335 ...(showReviewStep ? [reviewStep] : [])
336 ];
337 const CustomFooter = (
338 <WizardFooterDeprecated>
339 <WizardContextConsumerDeprecated>
340 {({ activeStep, goToStepByName, goToStepById, onNext, onBack, onClose }) => {
341 return (
342 <>
343 <Button
344 variant="secondary"
345 onClick={() => this.getPreviousStep(activeStep, onBack)}
346 className={activeStep.name === 'Get started' ? 'pf-m-disabled' : ''}
347 >
348 Back
349 </Button>
350 <Button variant="primary" type="submit" onClick={() => this.getNextStep(activeStep, onNext)}>
351 {activeStep.name === 'Review' ? 'Finish' : 'Next'}
352 </Button>
353 <Button variant="link" onClick={onClose}>
354 Cancel
355 </Button>
356 </>
357 );
358 }}
359 </WizardContextConsumerDeprecated>
360 </WizardFooterDeprecated>
361 );
362 const title = 'Progressive wizard';
363 return (
364 <WizardDeprecated
365 navAriaLabel={`${title} steps`}
366 mainAriaLabel={`${title} content`}
367 onClose={this.closeWizard}
368 footer={CustomFooter}
369 onGoToStep={this.onGoToStep}
370 steps={steps}
371 height={400}
372 />
373 );
374 }
375 }
376 ```
377
378 ### Get current step
379
380 ```js file="./WizardGetCurrentStep.tsx"
381
382 ```
383
384 ### Wizard in modal
385
386 ```js file="./WizardInModal.tsx"
387
388 ```
389
390 ### Wizard with drawer
391
392 ```js file="./WizardWithDrawer.tsx"
393
394 ```