react-jsonschema-form-validation
Version:
Simple form validation using JSON Schema and AJV
56 lines (49 loc) • 1.77 kB
JavaScript
import React, { useContext } from 'react';
/**
* @import { ReactElement } from 'react'
* @import { FormContextValue } from './Context.types'
*/
/**
* React context that carries the `<Form>` state down to descendant
* `<Field>` and `<FieldError>` components. Not intended for direct use —
* consume it through `useFormContext()` (or the legacy `withFormContext()`
* render-prop helper), which handles the "outside a Form" case with a
* descriptive error instead of returning `undefined`.
*/
var FormContext = React.createContext(
/** @type {FormContextValue | undefined} */
undefined);
export default FormContext;
var OUTSIDE_FORM_ERROR = 'react-jsonschema-form-validation: ' + 'useFormContext / withFormContext must be used inside a <Form> component.';
/**
* Reads the form context. Throws if the calling component is not a
* descendant of a `<Form>` — failing loudly is better than returning
* `undefined` and crashing later with a less obvious error.
*
* @returns {FormContextValue}
*/
export var useFormContext = function useFormContext() {
var ctx = useContext(FormContext);
if (ctx === undefined) {
throw new Error(OUTSIDE_FORM_ERROR);
}
return ctx;
};
/**
* Legacy render-prop helper kept for backward compatibility — prefer
* `useFormContext()` in new code. Like `useFormContext`, it throws when
* rendered outside a `<Form>` so the callback always receives a defined
* context value.
*
* @template T
* @param {(ctx: FormContextValue) => T} cb
* @returns {ReactElement}
*/
export var withFormContext = function withFormContext(cb) {
return React.createElement(FormContext.Consumer, null, function (ctx) {
if (ctx === undefined) {
throw new Error(OUTSIDE_FORM_ERROR);
}
return cb(ctx);
});
};