Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ governing permissions and limitations under the License.
}

.spectrum-InLineAlert {
composes: spectrum-FocusRing;
--spectrum-focus-ring-gap: var(--spectrum-alias-focus-ring-gap);
--spectrum-focus-ring-border-size: var(--spectrum-inlinealert-border-width);
--spectrum-focus-ring-border-radius: var(--spectrum-inlinealert-border-radius);
--spectrum-focus-ring-size: var(--spectrum-button-primary-focus-ring-size-key-focus);

position: relative;

display: inline-block;
Expand All @@ -61,6 +67,8 @@ governing permissions and limitations under the License.
border-inline-width: var(--spectrum-inlinealert-border-width);
border-style: solid;
border-radius: var(--spectrum-inlinealert-border-radius);

outline: none;
}

.spectrum-InLineAlert .spectrum-InLineAlert-grid {
Expand Down
7 changes: 6 additions & 1 deletion packages/@react-aria/datepicker/src/useDateField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,12 @@ export function useDateField<T extends DateValue>(props: AriaDateFieldOptions<T>
}, [focusManager]);

useFormReset(props.inputRef, state.value, state.setValue);
useFormValidation(props, state, props.inputRef);
useFormValidation({
...props,
focus() {
focusManager.focusFirst();
}
}, state, props.inputRef);

let inputProps: InputHTMLAttributes<HTMLInputElement> = {
type: 'hidden',
Expand Down
1 change: 1 addition & 0 deletions packages/@react-aria/form/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"url": "https://github.com/adobe/react-spectrum"
},
"dependencies": {
"@react-aria/interactions": "^3.19.1",
"@react-aria/utils": "^3.21.0",
"@react-stately/form": "3.0.0-alpha.1",
"@react-types/shared": "^3.21.0",
Expand Down
39 changes: 34 additions & 5 deletions packages/@react-aria/form/src/useFormValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@

import {FormValidationState} from '@react-stately/form';
import {RefObject, useEffect} from 'react';
import {setInteractionModality} from '@react-aria/interactions';
import {useEffectEvent, useLayoutEffect} from '@react-aria/utils';
import {Validation, ValidationResult} from '@react-types/shared';

type ValidatableElement = HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement;

export function useFormValidation<T>(props: Validation<T>, state: FormValidationState, ref: RefObject<ValidatableElement>) {
let {validationBehavior} = props;
interface FormValidationProps<T> extends Validation<T> {
focus?: () => void
}

export function useFormValidation<T>(props: FormValidationProps<T>, state: FormValidationState, ref: RefObject<ValidatableElement>) {
let {validationBehavior, focus} = props;

// This is a useLayoutEffect so that it runs before the useEffect in useFormValidationState, which commits the validation change.
useLayoutEffect(() => {
Expand All @@ -43,14 +48,27 @@ export function useFormValidation<T>(props: Validation<T>, state: FormValidation
});

let onInvalid = useEffectEvent((e: Event) => {
// Prevent default browser error UI from appearing.
e.preventDefault();

// Only commit validation if we are not already displaying one.
// This avoids clearing server errors that the user didn't actually fix.
if (!state.displayValidation.isInvalid) {
state.commitValidation();
}

// Auto focus the first invalid input in a form, unless the error already had its default prevented.
let form = ref.current?.form;
if (!e.defaultPrevented && form && getFirstInvalidInput(form) === ref.current) {
if (focus) {
focus();
} else {
ref.current?.focus();
}

// Always show focus ring.
setInteractionModality('keyboard');
}

// Prevent default browser error UI from appearing.
e.preventDefault();
});

let onChange = useEffectEvent(() => {
Expand Down Expand Up @@ -101,3 +119,14 @@ function getNativeValidity(input: ValidatableElement): ValidationResult {
validationErrors: input.validationMessage ? [input.validationMessage] : []
};
}

function getFirstInvalidInput(form: HTMLFormElement): ValidatableElement | null {
for (let i = 0; i < form.elements.length; i++) {
let element = form.elements[i] as ValidatableElement;
if (!element.validity.valid) {
return element;
}
}

return null;
}
5 changes: 4 additions & 1 deletion packages/@react-aria/select/src/HiddenSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ export function useHiddenSelect<T>(props: AriaHiddenSelectOptions, state: Select
let {visuallyHiddenProps} = useVisuallyHidden();

useFormReset(props.selectRef, state.selectedKey, state.setSelectedKey);
useFormValidation({validationBehavior}, state, props.selectRef);
useFormValidation({
validationBehavior,
focus: () => triggerRef.current.focus()
}, state, props.selectRef);

// In Safari, the <select> cannot have `display: none` or `hidden` for autofill to work.
// In Firefox, there must be a <label> to identify the <select> whereas other browsers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ function _MobileSearchAutocomplete<T extends object>(props: SpectrumSearchAutoco
let {triggerProps, overlayProps} = useOverlayTrigger({type: 'listbox'}, state, buttonRef);

let inputRef = useRef<HTMLInputElement>(null);
useFormValidation(props, state, inputRef);
useFormValidation({
...props,
focus: () => buttonRef.current?.focus()
}, state, inputRef);
let {isInvalid, validationErrors, validationDetails} = state.displayValidation;
let validationState = props.validationState || (isInvalid ? 'invalid' : undefined);
let errorMessage = props.errorMessage ?? validationErrors.join(' ');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2504,10 +2504,10 @@ describe('SearchAutocomplete', function () {

act(() => {getByTestId('form').checkValidity();});

expect(document.activeElement).toBe(input);
expect(input).toHaveAttribute('aria-describedby');
expect(document.getElementById(input.getAttribute('aria-describedby'))).toHaveTextContent('Constraints not satisfied');

await user.tab();
await user.keyboard('Tw');
act(() => {
jest.runAllTimers();
Expand Down Expand Up @@ -2541,10 +2541,11 @@ describe('SearchAutocomplete', function () {

act(() => {getByTestId('form').checkValidity();});

expect(document.activeElement).toBe(input);
expect(input).toHaveAttribute('aria-describedby');
expect(document.getElementById(input.getAttribute('aria-describedby'))).toHaveTextContent('Invalid value');

await user.tab();
await user.clear(input);
await user.keyboard('On');
act(() => {
jest.runAllTimers();
Expand Down
3 changes: 3 additions & 0 deletions packages/@react-spectrum/checkbox/test/CheckboxGroup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ describe('CheckboxGroup', () => {

expect(group).toHaveAttribute('aria-describedby');
expect(document.getElementById(group.getAttribute('aria-describedby'))).toHaveTextContent('Constraints not satisfied');
expect(document.activeElement).toBe(checkboxes[0]);

await user.click(checkboxes[0]);
expect(checkboxes[0].validity.valid).toBe(true);
Expand Down Expand Up @@ -523,6 +524,7 @@ describe('CheckboxGroup', () => {

expect(group).toHaveAttribute('aria-describedby');
expect(document.getElementById(group.getAttribute('aria-describedby'))).toHaveTextContent(['You must accept all terms']);
expect(document.activeElement).toBe(checkboxes[0]);

await user.click(checkboxes[0]);
expect(group).toHaveAttribute('aria-describedby');
Expand Down Expand Up @@ -568,6 +570,7 @@ describe('CheckboxGroup', () => {

expect(group).toHaveAttribute('aria-describedby');
expect(document.getElementById(group.getAttribute('aria-describedby'))).toHaveTextContent('You must accept the terms. You must accept the cookies.');
expect(document.activeElement).toBe(checkboxes[0]);

await user.click(checkboxes[0]);
expect(checkboxes[0].validity.valid).toBe(true);
Expand Down
7 changes: 4 additions & 3 deletions packages/@react-spectrum/color/test/ColorField.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,8 @@ describe('ColorField', function () {

expect(input).toHaveAttribute('aria-describedby');
expect(document.getElementById(input.getAttribute('aria-describedby'))).toHaveTextContent('Constraints not satisfied');
expect(document.activeElement).toBe(input);

await user.tab();
await user.keyboard('#000');

expect(input).toHaveAttribute('aria-describedby');
Expand Down Expand Up @@ -424,8 +424,9 @@ describe('ColorField', function () {

expect(input).toHaveAttribute('aria-describedby');
expect(document.getElementById(input.getAttribute('aria-describedby'))).toHaveTextContent('Invalid value');
expect(document.activeElement).toBe(input);

await user.tab();
await user.clear(input);
await user.keyboard('#111');

expect(input).toHaveAttribute('aria-describedby');
Expand Down Expand Up @@ -515,8 +516,8 @@ describe('ColorField', function () {
act(() => {getByTestId('form').checkValidity();});

expect(input).toHaveAttribute('aria-describedby');
expect(document.activeElement).toBe(input);

await user.tab();
await user.keyboard('333');

expect(input).toHaveAttribute('aria-describedby');
Expand Down
5 changes: 4 additions & 1 deletion packages/@react-spectrum/combobox/src/MobileComboBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ export const MobileComboBox = React.forwardRef(function MobileComboBox<T extends
let {triggerProps, overlayProps} = useOverlayTrigger({type: 'listbox'}, state, buttonRef);

let inputRef = useRef<HTMLInputElement>(null);
useFormValidation(props, state, inputRef);
useFormValidation({
...props,
focus: () => buttonRef.current.focus()
}, state, inputRef);
let {isInvalid, validationErrors, validationDetails} = state.displayValidation;
let validationState = props.validationState || (isInvalid ? 'invalid' : null);
let errorMessage = props.errorMessage ?? validationErrors.join(' ');
Expand Down
3 changes: 2 additions & 1 deletion packages/@react-spectrum/combobox/test/ComboBox.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5383,10 +5383,10 @@ describe('ComboBox', function () {

act(() => {getByTestId('form').checkValidity();});

expect(document.activeElement).toBe(input);
expect(input).toHaveAttribute('aria-describedby');
expect(document.getElementById(input.getAttribute('aria-describedby'))).toHaveTextContent('Constraints not satisfied');

await user.tab();
await user.keyboard('[ArrowRight]Tw');

act(() => {
Expand Down Expand Up @@ -5421,6 +5421,7 @@ describe('ComboBox', function () {

act(() => {getByTestId('form').checkValidity();});

expect(document.activeElement).toBe(input);
expect(input).toHaveAttribute('aria-describedby');
expect(document.getElementById(input.getAttribute('aria-describedby'))).toHaveTextContent('Invalid value');

Expand Down
16 changes: 10 additions & 6 deletions packages/@react-spectrum/datepicker/test/DateField.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,9 @@ describe('DateField', function () {
expect(group).toHaveAttribute('aria-describedby');
let getDescription = () => group.getAttribute('aria-describedby').split(' ').map(d => document.getElementById(d).textContent).join(' ');
expect(getDescription()).toContain('Constraints not satisfied');
expect(document.activeElement).toBe(within(group).getAllByRole('spinbutton')[0]);

await user.keyboard('[Tab][ArrowUp][Tab][ArrowUp][Tab][ArrowUp]');
await user.keyboard('[ArrowUp][Tab][ArrowUp][Tab][ArrowUp]');

expect(getDescription()).toContain('Constraints not satisfied');
expect(input.validity.valid).toBe(true);
Expand Down Expand Up @@ -422,8 +423,9 @@ describe('DateField', function () {

expect(group).toHaveAttribute('aria-describedby');
expect(getDescription()).toContain('Value must be 2/3/2020 or later.');
expect(document.activeElement).toBe(within(group).getAllByRole('spinbutton')[0]);

await user.keyboard('[Tab][Tab][Tab][ArrowUp]');
await user.keyboard('[Tab][Tab][ArrowUp]');

expect(getDescription()).toContain('Value must be 2/3/2020 or later.');
expect(input.validity.valid).toBe(true);
Expand All @@ -440,9 +442,9 @@ describe('DateField', function () {

act(() => {getByTestId('form').checkValidity();});
expect(getDescription()).toContain('Value must be 2/3/2024 or earlier.');
expect(document.activeElement).toBe(within(group).getAllByRole('spinbutton')[0]);

await user.tab({shift: true});
await user.keyboard('[ArrowDown]');
await user.keyboard('[Tab][Tab][ArrowDown]');
expect(getDescription()).toContain('Value must be 2/3/2024 or earlier.');
expect(input.validity.valid).toBe(true);
await user.tab();
Expand All @@ -469,8 +471,9 @@ describe('DateField', function () {

expect(group).toHaveAttribute('aria-describedby');
expect(getDescription()).toContain('Invalid value');
expect(document.activeElement).toBe(within(group).getAllByRole('spinbutton')[0]);

await user.keyboard('[Tab][ArrowRight][ArrowRight]2024');
await user.keyboard('[ArrowRight][ArrowRight]2024');

expect(getDescription()).toContain('Invalid value');
expect(input.validity.valid).toBe(true);
Expand Down Expand Up @@ -589,8 +592,9 @@ describe('DateField', function () {
expect(group).toHaveAttribute('aria-describedby');
let getDescription = () => group.getAttribute('aria-describedby').split(' ').map(d => document.getElementById(d).textContent).join(' ');
expect(getDescription()).toContain('Constraints not satisfied');
expect(document.activeElement).toBe(within(group).getAllByRole('spinbutton')[0]);

await user.keyboard('[Tab]232023');
await user.keyboard('232023');

expect(group).toHaveAttribute('aria-describedby');
expect(input.validity.valid).toBe(true);
Expand Down
13 changes: 8 additions & 5 deletions packages/@react-spectrum/datepicker/test/DatePicker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1956,8 +1956,9 @@ describe('DatePicker', function () {
expect(group).toHaveAttribute('aria-describedby');
let getDescription = () => group.getAttribute('aria-describedby').split(' ').map(d => document.getElementById(d).textContent).join(' ');
expect(getDescription()).toContain('Constraints not satisfied');
expect(document.activeElement).toBe(within(group).getAllByRole('spinbutton')[0]);

await user.keyboard('[Tab][ArrowUp][Tab][ArrowUp][Tab][ArrowUp]');
await user.keyboard('[ArrowUp][Tab][ArrowUp][Tab][ArrowUp]');

expect(getDescription()).toContain('Constraints not satisfied');
expect(input.validity.valid).toBe(true);
Expand Down Expand Up @@ -1986,8 +1987,9 @@ describe('DatePicker', function () {

expect(group).toHaveAttribute('aria-describedby');
expect(getDescription()).toContain('Value must be 2/3/2020 or later.');
expect(document.activeElement).toBe(within(group).getAllByRole('spinbutton')[0]);

await user.keyboard('[Tab][Tab][Tab][ArrowUp]');
await user.keyboard('[Tab][Tab][ArrowUp]');

expect(getDescription()).toContain('Value must be 2/3/2020 or later.');
expect(input.validity.valid).toBe(true);
Expand All @@ -2004,9 +2006,9 @@ describe('DatePicker', function () {

act(() => {getByTestId('form').checkValidity();});
expect(getDescription()).toContain('Value must be 2/3/2024 or earlier.');
expect(document.activeElement).toBe(within(group).getAllByRole('spinbutton')[0]);

await user.tab({shift: true});
await user.keyboard('[ArrowDown]');
await user.keyboard('[Tab][Tab][ArrowDown]');
expect(getDescription()).toContain('Value must be 2/3/2024 or earlier.');
expect(input.validity.valid).toBe(true);
await user.tab();
Expand All @@ -2033,8 +2035,9 @@ describe('DatePicker', function () {

expect(group).toHaveAttribute('aria-describedby');
expect(getDescription()).toContain('Invalid value');
expect(document.activeElement).toBe(within(group).getAllByRole('spinbutton')[0]);

await user.keyboard('[Tab][ArrowRight][ArrowRight]2024');
await user.keyboard('[ArrowRight][ArrowRight]2024');

expect(getDescription()).toContain('Invalid value');
expect(input.validity.valid).toBe(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1548,8 +1548,9 @@ describe('DateRangePicker', function () {
expect(group).toHaveAttribute('aria-describedby');
let getDescription = () => group.getAttribute('aria-describedby').split(' ').map(d => document.getElementById(d).textContent).join(' ');
expect(getDescription()).toContain('Constraints not satisfied');
expect(document.activeElement).toBe(within(group).getAllByRole('spinbutton')[0]);

await user.keyboard('[Tab][ArrowUp][Tab][ArrowUp][Tab][ArrowUp]');
await user.keyboard('[ArrowUp][Tab][ArrowUp][Tab][ArrowUp]');
await user.keyboard('[Tab][ArrowUp][Tab][ArrowUp][Tab][ArrowUp]');

expect(getDescription()).toContain('Constraints not satisfied');
Expand Down Expand Up @@ -1582,8 +1583,9 @@ describe('DateRangePicker', function () {

expect(group).toHaveAttribute('aria-describedby');
expect(getDescription()).toContain('Value must be 2/3/2020 or later. Value must be 2/3/2024 or earlier.');
expect(document.activeElement).toBe(within(group).getAllByRole('spinbutton')[0]);

await user.keyboard('[Tab][Tab][Tab][ArrowUp]');
await user.keyboard('[Tab][Tab][ArrowUp]');

expect(getDescription()).toContain('Value must be 2/3/2020 or later. Value must be 2/3/2024 or earlier.');
expect(startInput.validity.valid).toBe(false);
Expand Down Expand Up @@ -1624,8 +1626,9 @@ describe('DateRangePicker', function () {

expect(group).toHaveAttribute('aria-describedby');
expect(getDescription()).toContain('Invalid value');
expect(document.activeElement).toBe(within(group).getAllByRole('spinbutton')[0]);

await user.keyboard('[Tab][ArrowRight][ArrowRight]2024');
await user.keyboard('[ArrowRight][ArrowRight]2024');
expect(getDescription()).toContain('Invalid value');
expect(startInput.validity.valid).toBe(false);
expect(endInput.validity.valid).toBe(false);
Expand Down
Loading