@hypothesis/frontend-shared
Version:
Shared components, styles and utilities for Hypothesis projects
18 lines • 769 B
JavaScript
import { useLayoutEffect } from 'preact/hooks';
/**
* Sync custom validation error messages to the browser's native validation
* state.
*
* @param ref - An `HTMLInputElement` or other element that supports the
* Constraint Validation API
* @param error - The current error or undefined if the field input is valid
*/
export function useValidationError(ref, error) {
// Sync errors to native form validation API. This will prevent submission
// of the form until the error is resolved.
useLayoutEffect(() => {
var _ref$current;
(_ref$current = ref.current) === null || _ref$current === void 0 || _ref$current.setCustomValidity(error !== null && error !== void 0 ? error : '');
}, [error, ref]);
}
//# sourceMappingURL=use-validation-error.js.map