Skip to content

Commit 89f20b0

Browse files
authored
RAC: export FormContext and document validationBehavior on RAC Form (#6292)
* export FormContext and document validationBehavior * fix lint * one more lint issue
1 parent 4e3a3dd commit 89f20b0

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

packages/react-aria-components/docs/Form.mdx

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,63 @@ To provide validation errors, the `validationErrors` prop should be set to an ob
160160

161161
See the [Forms](forms.html) guide to learn more about form validation in React Aria, including client-side validation, and integration with other frameworks and libraries.
162162

163+
### Validation behavior
164+
165+
By default, native HTML form validation is used to display errors and block form submission.
166+
167+
```tsx example
168+
<Form>
169+
<TextField name="email" type="email" isRequired>
170+
<Label>Email</Label>
171+
<Input />
172+
<FieldError />
173+
</TextField>
174+
<div style={{display: 'flex', gap: 8}}>
175+
<Button type="submit">Submit</Button>
176+
<Button type="reset">Reset</Button>
177+
</div>
178+
</Form>
179+
```
180+
181+
To instead use ARIA attributes for form validation, set the validationBehavior prop to "aria". This will not block form submission, and will ensure validation errors are displayed to the user in realtime as the value is edited.
182+
183+
This can be set at the form level to apply to all fields, or at the field level to override the form's behavior for a specific field.
184+
185+
```tsx example
186+
const EMAIL_REGEX = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
187+
188+
function Example() {
189+
let [value, setValue] = React.useState('');
190+
return (
191+
<Form validationBehavior="aria" onSubmit={(e) => {e.preventDefault()}}>
192+
<TextField
193+
name="email"
194+
type="email"
195+
value={value}
196+
onChange={setValue}
197+
isRequired
198+
isInvalid={value.length > 0 && !EMAIL_REGEX.test(value)}>
199+
<Label>Email</Label>
200+
<Input />
201+
<FieldError />
202+
</TextField>
203+
<div style={{display: 'flex', gap: 8}}>
204+
<Button type="submit">Submit</Button>
205+
<Button onPress={() => setValue('')}>Reset</Button>
206+
</div>
207+
</Form>
208+
);
209+
}
210+
```
211+
212+
The `validationBehavior` for a Form can be accessed from within any component inside a given From by using the `FormContext`.
213+
214+
```tsx
215+
import {FormContext} from 'react-aria-components';
216+
217+
let {validationBehavior} = useContext(FormContext);
218+
```
219+
163220
### Focus management
164221

165222
By default, after a user submits a form with validation errors, the first invalid field will be focused. You can prevent this by calling `preventDefault` during the `onInvalid` event, and move focus yourself. This example shows how to move focus to an [alert](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/alert_role) element at the top of a form.

packages/react-aria-components/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export {DialogTrigger, Dialog, DialogContext, OverlayTriggerStateContext} from '
3434
export {DropZone, DropZoneContext} from './DropZone';
3535
export {FieldError, FieldErrorContext} from './FieldError';
3636
export {FileTrigger} from './FileTrigger';
37-
export {Form} from './Form';
37+
export {Form, FormContext} from './Form';
3838
export {GridList, GridListItem, GridListContext} from './GridList';
3939
export {Group, GroupContext} from './Group';
4040
export {Header, HeaderContext} from './Header';

0 commit comments

Comments
 (0)