Debian Codegraph @

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

raw
1 import { useEffect, useState } from 'react';
2 import {
3 EmptyState,
4 EmptyStateFooter,
5 EmptyStateBody,
6 EmptyStateActions,
7 Form,
8 FormGroup,
9 TextInput,
10 Progress,
11 Button,
12 Alert
13 } from '@patternfly/react-core';
14 import {
15 Wizard as WizardDeprecated,
16 WizardFooter as WizardFooterDeprecated,
17 WizardContextConsumer as WizardContextConsumerDeprecated
18 } from '@patternfly/react-core/deprecated';
19 import layout from '@patternfly/react-styles/css/layouts/Bullseye/bullseye';
20
21 // eslint-disable-next-line patternfly-react/import-tokens-icons
22 import { CogsIcon } from '@patternfly/react-icons';
23
24 interface finishedProps {
25 onClose: () => void;
26 }
27
28 const FinishedStep: React.FunctionComponent<finishedProps> = (props: finishedProps) => {
29 const [percent, setPercent] = useState(0);
30
31 const tick = () => {
32 setPercent((prevPercent) => {
33 if (prevPercent < 100) {
34 return prevPercent + 20;
35 } else {
36 return prevPercent;
37 }
38 });
39 };
40
41 useEffect(() => {
42 const interval = setInterval(() => tick(), 1000);
43
44 if (percent >= 100) {
45 clearInterval(interval);
46 }
47
48 return () => clearInterval(interval);
49 }, [percent]);
50
51 return (
52 <div className={layout.bullseye}>
53 <EmptyState
54 headingLevel="h4"
55 titleText={percent === 100 ? 'Validation complete' : 'Validating credentials'}
56 icon={CogsIcon}
57 variant="lg"
58 >
59 <EmptyStateBody>
60 <Progress value={percent} measureLocation="outside" aria-label="validation-progress" />
61 </EmptyStateBody>
62 <EmptyStateBody>
63 Description can be used to further elaborate on the validation step, or give the user a better idea of how
64 long the process will take.
65 </EmptyStateBody>
66 <EmptyStateFooter>
67 <EmptyStateActions>
68 <Button isDisabled={percent !== 100} onClick={props.onClose}>
69 Log to console
70 </Button>
71 </EmptyStateActions>
72 </EmptyStateFooter>
73 </EmptyState>
74 </div>
75 );
76 };
77
78 interface sampleFormProps {
79 formValue: string;
80 isFormValid: boolean;
81 onChange?: (isValid: boolean, value: string) => void;
82 }
83
84 const SampleForm: React.FunctionComponent<sampleFormProps> = (props: sampleFormProps) => {
85 const [value, setValue] = useState(props.formValue);
86 const [isValid, setIsValid] = useState(props.isFormValid);
87
88 const handleTextInputChange = (_event, value: string) => {
89 const valid = /^\d+$/.test(value);
90 setValue(value);
91 setIsValid(valid);
92 props.onChange && props.onChange(valid, value);
93 };
94
95 const validated = isValid ? 'default' : 'error';
96
97 return (
98 <Form>
99 <FormGroup
100 label="Age:"
101 type="number"
102 helperText="Write your age in numbers."
103 helperTextInvalid="Age has to be a number"
104 fieldId="age"
105 validated={validated}
106 >
107 <TextInput
108 validated={validated}
109 value={value}
110 id="age"
111 aria-describedby="age-helper"
112 onChange={handleTextInputChange}
113 />
114 </FormGroup>
115 </Form>
116 );
117 };
118
119 export const WizardValidateButtonPress: React.FunctionComponent = () => {
120 const [isFormValid, setIsFormValid] = useState(false);
121 const [formValue, setFormValue] = useState('Validating on button press');
122 const [stepsValid, setStepsValid] = useState(0);
123 const [errorText, setErrorText] = useState(false);
124
125 const closeWizard = () => {
126 // eslint-disable-next-line no-console
127 console.log('close wizard');
128 };
129
130 const onFormChange = (isValid: boolean, value: string) => {
131 setIsFormValid(isValid);
132 setFormValue(value);
133 };
134
135 const validateLastStep: (onNext: () => void) => void = (onNext) => {
136 if (stepsValid !== 1 && !isFormValid) {
137 setErrorText(true);
138 } else {
139 setStepsValid(1);
140 setErrorText(false);
141 onNext();
142 }
143 };
144
145 const steps = [
146 { name: 'First step', component: <p>Step 1 content</p> },
147 { name: 'Second step', component: <p>Step 2 content</p> },
148 {
149 name: 'Final Step',
150 component: (
151 <>
152 {errorText && (
153 <div style={{ padding: '15px 0' }}>
154 <Alert variant="warning" title="Validation failed, please try again" />
155 </div>
156 )}
157 <SampleForm formValue={formValue} isFormValid={stepsValid !== 1} onChange={onFormChange} />
158 </>
159 )
160 },
161 { name: 'Finish', component: <FinishedStep onClose={closeWizard} />, isFinishedStep: true }
162 ];
163
164 const CustomFooter = (
165 <WizardFooterDeprecated>
166 <WizardContextConsumerDeprecated>
167 {({ activeStep, goToStepByName, onNext, onBack, onClose }) => {
168 if (activeStep.name !== 'Final Step') {
169 return (
170 <>
171 <Button variant="primary" type="submit" onClick={onNext}>
172 Forward
173 </Button>
174 <Button
175 variant="secondary"
176 onClick={onBack}
177 className={activeStep.name === 'First step' ? 'pf-m-disabled' : ''}
178 >
179 Backward
180 </Button>
181 <Button variant="link" onClick={onClose}>
182 Cancel
183 </Button>
184 </>
185 );
186 }
187 // Final step buttons
188 return (
189 <>
190 <Button onClick={() => validateLastStep(onNext)}>Validate</Button>
191 <Button onClick={() => goToStepByName('First step')}>Go to Beginning</Button>
192 </>
193 );
194 }}
195 </WizardContextConsumerDeprecated>
196 </WizardFooterDeprecated>
197 );
198
199 const title = 'Validate on button press wizard example';
200 return (
201 <WizardDeprecated
202 navAriaLabel={`${title} steps`}
203 mainAriaLabel={`${title} content`}
204 onClose={closeWizard}
205 footer={CustomFooter}
206 steps={steps}
207 height={400}
208 />
209 );
210 };