| 1 |
import { useCallback, useEffect, useState } from 'react'; |
| 2 |
import { |
| 3 |
Button, |
| 4 |
ActionList, |
| 5 |
ActionListGroup, |
| 6 |
ActionListItem, |
| 7 |
Alert, |
| 8 |
EmptyState, |
| 9 |
EmptyStateFooter, |
| 10 |
EmptyStateBody, |
| 11 |
EmptyStateActions, |
| 12 |
Progress, |
| 13 |
Form, |
| 14 |
FormGroup, |
| 15 |
TextInput, |
| 16 |
HelperText, |
| 17 |
HelperTextItem, |
| 18 |
FormHelperText, |
| 19 |
Wizard, |
| 20 |
WizardStep, |
| 21 |
WizardFooterWrapper, |
| 22 |
useWizardContext |
| 23 |
} from '@patternfly/react-core'; |
| 24 |
import CogsIcon from '@patternfly/react-icons/dist/esm/icons/cogs-icon'; |
| 25 |
import layout from '@patternfly/react-styles/css/layouts/Bullseye/bullseye'; |
| 26 |
|
| 27 |
interface ValidationProgressProps { |
| 28 |
onClose(): void; |
| 29 |
} |
| 30 |
|
| 31 |
const ValidationProgress: React.FunctionComponent<ValidationProgressProps> = ({ onClose }) => { |
| 32 |
const [percentValidated, setPercentValidated] = useState(0); |
| 33 |
|
| 34 |
const tick = useCallback(() => { |
| 35 |
if (percentValidated < 100) { |
| 36 |
setPercentValidated((prevValue) => prevValue + 20); |
| 37 |
} |
| 38 |
}, [percentValidated]); |
| 39 |
|
| 40 |
useEffect(() => { |
| 41 |
const interval = setInterval(() => tick(), 1000); |
| 42 |
|
| 43 |
return () => { |
| 44 |
clearInterval(interval); |
| 45 |
}; |
| 46 |
}, [tick]); |
| 47 |
|
| 48 |
return ( |
| 49 |
<div className={layout.bullseye}> |
| 50 |
<EmptyState |
| 51 |
headingLevel="h4" |
| 52 |
titleText={percentValidated === 100 ? 'Validation complete' : 'Validating credentials'} |
| 53 |
icon={CogsIcon} |
| 54 |
variant="lg" |
| 55 |
> |
| 56 |
<EmptyStateBody> |
| 57 |
<Progress value={percentValidated} measureLocation="outside" aria-label="Wizard validation progress" /> |
| 58 |
</EmptyStateBody> |
| 59 |
<EmptyStateBody> |
| 60 |
Description can be used to further elaborate on the validation step, or give the user a better idea of how |
| 61 |
long the process will take. |
| 62 |
</EmptyStateBody> |
| 63 |
<EmptyStateFooter> |
| 64 |
<EmptyStateActions> |
| 65 |
<Button isDisabled={percentValidated !== 100} onClick={onClose}> |
| 66 |
Log to console |
| 67 |
</Button> |
| 68 |
</EmptyStateActions> |
| 69 |
</EmptyStateFooter> |
| 70 |
</EmptyState> |
| 71 |
</div> |
| 72 |
); |
| 73 |
}; |
| 74 |
|
| 75 |
interface LastStepFooterProps { |
| 76 |
isValid: boolean; |
| 77 |
setIsSubmitted(isSubmitted: boolean): void; |
| 78 |
setHasErrorOnSubmit(isSubmitted: boolean): void; |
| 79 |
} |
| 80 |
|
| 81 |
const LastStepFooter: React.FunctionComponent<LastStepFooterProps> = ({ |
| 82 |
isValid, |
| 83 |
setIsSubmitted, |
| 84 |
setHasErrorOnSubmit |
| 85 |
}) => { |
| 86 |
const { goToNextStep, goToPrevStep } = useWizardContext(); |
| 87 |
|
| 88 |
const onValidate = () => { |
| 89 |
setIsSubmitted(true); |
| 90 |
|
| 91 |
if (!isValid) { |
| 92 |
setIsSubmitted(false); |
| 93 |
setHasErrorOnSubmit(true); |
| 94 |
} else { |
| 95 |
goToNextStep(); |
| 96 |
} |
| 97 |
}; |
| 98 |
|
| 99 |
return ( |
| 100 |
<WizardFooterWrapper> |
| 101 |
<ActionList> |
| 102 |
<ActionListGroup> |
| 103 |
<ActionListItem> |
| 104 |
<Button variant="secondary" onClick={goToPrevStep}> |
| 105 |
Back |
| 106 |
</Button> |
| 107 |
</ActionListItem> |
| 108 |
<ActionListItem> |
| 109 |
<Button onClick={onValidate}>Validate</Button> |
| 110 |
</ActionListItem> |
| 111 |
</ActionListGroup> |
| 112 |
</ActionList> |
| 113 |
</WizardFooterWrapper> |
| 114 |
); |
| 115 |
}; |
| 116 |
|
| 117 |
interface SampleFormProps { |
| 118 |
value: string; |
| 119 |
isValid: boolean; |
| 120 |
setValue: (value: string) => void; |
| 121 |
setIsValid: (isValid: boolean) => void; |
| 122 |
} |
| 123 |
|
| 124 |
const SampleForm: React.FunctionComponent<SampleFormProps> = ({ value, isValid, setValue, setIsValid }) => { |
| 125 |
const validated = isValid ? 'default' : 'error'; |
| 126 |
|
| 127 |
const handleTextInputChange = (_event, value: string) => { |
| 128 |
const isValid = /^\d+$/.test(value); |
| 129 |
|
| 130 |
setValue(value); |
| 131 |
setIsValid(isValid); |
| 132 |
}; |
| 133 |
|
| 134 |
return ( |
| 135 |
<Form> |
| 136 |
<FormGroup label="Age:" type="number" fieldId="age"> |
| 137 |
<TextInput |
| 138 |
validated={validated} |
| 139 |
value={value} |
| 140 |
id="age" |
| 141 |
aria-describedby="age-helper" |
| 142 |
onChange={handleTextInputChange} |
| 143 |
/> |
| 144 |
<FormHelperText> |
| 145 |
<HelperText> |
| 146 |
<HelperTextItem variant={validated}> |
| 147 |
{validated === 'error' ? 'Age has to be a number' : 'Write your age in numbers.'} |
| 148 |
</HelperTextItem> |
| 149 |
</HelperText> |
| 150 |
</FormHelperText> |
| 151 |
</FormGroup> |
| 152 |
</Form> |
| 153 |
); |
| 154 |
}; |
| 155 |
|
| 156 |
export const WizardValidateOnButtonPress: React.FunctionComponent = () => { |
| 157 |
const [ageValue, setAgeValue] = useState('Thirty'); |
| 158 |
const [isSubmitted, setIsSubmitted] = useState(false); |
| 159 |
const [isFirstStepValid, setIsFirstStepValid] = useState(false); |
| 160 |
const [hasErrorOnSubmit, setHasErrorOnSubmit] = useState(false); |
| 161 |
|
| 162 |
// eslint-disable-next-line no-console |
| 163 |
const onClose = () => console.log('Some close action occurs here.'); |
| 164 |
|
| 165 |
if (isSubmitted && isFirstStepValid) { |
| 166 |
return <ValidationProgress onClose={onClose} />; |
| 167 |
} |
| 168 |
|
| 169 |
return ( |
| 170 |
<Wizard title="Validate on button press wizard" onClose={onClose} height={400}> |
| 171 |
<WizardStep name="Step 1" id="validate-btn-step-1"> |
| 172 |
Step 1 content |
| 173 |
</WizardStep> |
| 174 |
<WizardStep name="Step 2" id="validate-btn-step-2"> |
| 175 |
Step 2 content |
| 176 |
</WizardStep> |
| 177 |
<WizardStep |
| 178 |
name="Final Step" |
| 179 |
id="validate-btn-finish-step" |
| 180 |
footer={ |
| 181 |
<LastStepFooter |
| 182 |
isValid={isFirstStepValid} |
| 183 |
setIsSubmitted={setIsSubmitted} |
| 184 |
setHasErrorOnSubmit={setHasErrorOnSubmit} |
| 185 |
/> |
| 186 |
} |
| 187 |
> |
| 188 |
{hasErrorOnSubmit && ( |
| 189 |
<div style={{ padding: '15px 0' }}> |
| 190 |
<Alert isInline variant="danger" title="Validation failed, please try again." /> |
| 191 |
</div> |
| 192 |
)} |
| 193 |
<SampleForm |
| 194 |
value={ageValue} |
| 195 |
setValue={(value) => setAgeValue(value)} |
| 196 |
isValid={!hasErrorOnSubmit || isFirstStepValid} |
| 197 |
setIsValid={setIsFirstStepValid} |
| 198 |
/> |
| 199 |
</WizardStep> |
| 200 |
</Wizard> |
| 201 |
); |
| 202 |
}; |