| 1 |
import { useState } from 'react'; |
| 2 |
import { |
| 3 |
Form, |
| 4 |
FormGroup, |
| 5 |
FormHelperText, |
| 6 |
HelperText, |
| 7 |
HelperTextItem, |
| 8 |
TextInput, |
| 9 |
Wizard, |
| 10 |
WizardStep |
| 11 |
} from '@patternfly/react-core'; |
| 12 |
|
| 13 |
interface SampleFormProps { |
| 14 |
value: string; |
| 15 |
isValid: boolean; |
| 16 |
setValue: (value: string) => void; |
| 17 |
setIsValid: (isValid: boolean) => void; |
| 18 |
} |
| 19 |
|
| 20 |
const SampleForm: React.FunctionComponent<SampleFormProps> = ({ value, isValid, setValue, setIsValid }) => { |
| 21 |
const validated = isValid ? 'default' : 'error'; |
| 22 |
|
| 23 |
const handleTextInputChange = (_event, value: string) => { |
| 24 |
const isValid = /^\d+$/.test(value); |
| 25 |
|
| 26 |
setValue(value); |
| 27 |
setIsValid(isValid); |
| 28 |
}; |
| 29 |
|
| 30 |
return ( |
| 31 |
<Form> |
| 32 |
<FormGroup label="Age:" type="number" fieldId="age"> |
| 33 |
<TextInput |
| 34 |
validated={validated} |
| 35 |
value={value} |
| 36 |
id="age" |
| 37 |
aria-describedby="age-helper" |
| 38 |
onChange={handleTextInputChange} |
| 39 |
/> |
| 40 |
<FormHelperText> |
| 41 |
<HelperText> |
| 42 |
<HelperTextItem variant={validated}> |
| 43 |
{validated === 'error' ? 'Age has to be a number' : 'Write your age in numbers.'} |
| 44 |
</HelperTextItem> |
| 45 |
</HelperText> |
| 46 |
</FormHelperText> |
| 47 |
</FormGroup> |
| 48 |
</Form> |
| 49 |
); |
| 50 |
}; |
| 51 |
|
| 52 |
export const WizardEnabledOnFormValidation: React.FunctionComponent = () => { |
| 53 |
const [ageValue, setAgeValue] = useState('Thirty'); |
| 54 |
const [isSubAFormValid, setIsSubAFormValid] = useState(false); |
| 55 |
|
| 56 |
const onSave = () => alert(`Wow, you look a lot younger than ${ageValue}.`); |
| 57 |
|
| 58 |
return ( |
| 59 |
<Wizard isVisitRequired height={400} title="Enabled on form validation wizard" onSave={onSave}> |
| 60 |
<WizardStep name="Information" id="form-valid-info"> |
| 61 |
Information content |
| 62 |
</WizardStep> |
| 63 |
<WizardStep |
| 64 |
name="Configuration" |
| 65 |
id="form-valid-config" |
| 66 |
steps={[ |
| 67 |
<WizardStep |
| 68 |
name="Substep A with validation" |
| 69 |
id="form-valid-sub-a" |
| 70 |
key="form-valid-sub-a" |
| 71 |
footer={{ isNextDisabled: !isSubAFormValid }} |
| 72 |
> |
| 73 |
<SampleForm |
| 74 |
value={ageValue} |
| 75 |
setValue={setAgeValue} |
| 76 |
isValid={isSubAFormValid} |
| 77 |
setIsValid={setIsSubAFormValid} |
| 78 |
/> |
| 79 |
</WizardStep>, |
| 80 |
<WizardStep name="Substep B" id="form-valid-sub-b" key="form-valid-sub-b"> |
| 81 |
Substep B content |
| 82 |
</WizardStep> |
| 83 |
]} |
| 84 |
/> |
| 85 |
<WizardStep name="Additional" id="form-valid-additional"> |
| 86 |
Additional step content |
| 87 |
</WizardStep> |
| 88 |
<WizardStep name="Review" id="form-valid-review" footer={{ nextButtonText: 'Submit form' }}></WizardStep> |
| 89 |
</Wizard> |
| 90 |
); |
| 91 |
}; |