Skip to content

Commit 5065f25

Browse files
authored
Update docs form validation example to demonstrate best-practise when submitting a form in an invalid state (#834)
* update docs validation example to demonstrate best-practise when submitting a form in an invalid state * tidy up example * update copy
1 parent 7473042 commit 5065f25

File tree

1 file changed

+41
-39
lines changed

1 file changed

+41
-39
lines changed

apps/docs/content/components/FormControl.mdx

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ description: Use the form control component to display form inputs alongside lab
99
import InlineCode from '@primer/gatsby-theme-doctocat/src/components/inline-code'
1010
import {PropTableValues} from '../../src/components'
1111
import {Label} from '@primer/react'
12-
import {Heading, Text} from '@primer/react-brand'
12+
import {Button, Heading, Text} from '@primer/react-brand'
1313
import {SearchIcon} from '@primer/octicons-react'
1414
import {Link} from 'gatsby'
1515

@@ -228,52 +228,54 @@ Labels should only be visually hidden when the context is clear from the input i
228228

229229
The following example demonstrates declarative form validation in [controlled mode](https://reactjs.org/docs/forms.html#controlled-components).
230230

231-
Try changing the input value to to `monalisa` to show the `success` state.
231+
When the form is submitted with an invalid value, the invalid input receives focus to help the user correct the error. This is especially important for users navigating the form using a screen reader.
232+
233+
More information on form validation best practices can be found in the [Primer UI Patterns documentation](https://primer.style/ui-patterns/forms/overview#validation).
234+
235+
Try changing the input value to `monalisa` and submitting the form to show the `success` state.
232236

233237
```javascript live noinline
234238
const App = () => {
235-
const [value, setValue] = React.useState()
236-
const [validationState, setValidationState] = React.useState()
237-
238-
React.useEffect(() => {
239-
const defaultHandle = 'mona lisa'
240-
setValue(defaultHandle)
241-
validate(defaultHandle)
242-
}, [])
243-
244-
const validate = (inputValue) => {
245-
if (/\s/g.test(inputValue)) {
246-
setValidationState('error')
247-
} else {
248-
setValidationState('success')
249-
}
250-
}
239+
const inputRef = React.useRef(null)
240+
const [value, setValue] = React.useState('mona lisa')
241+
const [isValid, setIsValid] = React.useState(false)
242+
243+
const onChange = (e) => setValue(e.target.value)
244+
245+
const onSubmit = (e) => {
246+
e.preventDefault()
247+
const valid = !value.includes(' ')
248+
setIsValid(valid)
251249

252-
const handleChange = (event) => {
253-
event.preventDefault()
254-
if (!event.target.value) {
255-
setValue(undefined)
256-
setValidationState(undefined)
257-
return
250+
if (!valid) {
251+
inputRef.current.focus()
258252
}
259-
setValue(event.target.value)
260-
validate(event.target.value)
261253
}
262254

263255
return (
264-
<FormControl validationStatus={validationState} fullWidth>
265-
<FormControl.Label>GitHub handle</FormControl.Label>
266-
<TextInput onChange={handleChange} value={value} fullWidth />
267-
{validationState && validationState === 'error' && (
268-
<FormControl.Validation>
269-
GitHub handles cannot contain spaces.{' '}
270-
{value && `Did you mean "${value.replaceAll(' ', '')}"`}
271-
</FormControl.Validation>
272-
)}
273-
{validationState && validationState === 'success' && (
274-
<FormControl.Validation>Valid name</FormControl.Validation>
275-
)}
276-
</FormControl>
256+
<form onSubmit={onSubmit}>
257+
<Stack gap="normal">
258+
<FormControl validationStatus={isValid ? 'success' : 'error'} fullWidth>
259+
<FormControl.Label>GitHub handle</FormControl.Label>
260+
<TextInput
261+
ref={inputRef}
262+
fullWidth
263+
value={value}
264+
onChange={onChange}
265+
/>
266+
{isValid && (
267+
<FormControl.Validation>Valid name</FormControl.Validation>
268+
)}
269+
{!isValid && (
270+
<FormControl.Validation>
271+
GitHub handles cannot contain spaces.{' '}
272+
{value && `Did you mean "${value.replaceAll(' ', '')}"`}
273+
</FormControl.Validation>
274+
)}
275+
</FormControl>
276+
<Button type="submit">Submit</Button>
277+
</Stack>
278+
</form>
277279
)
278280
}
279281

0 commit comments

Comments
 (0)