import { ComponentType, ElementType, FormEvent, HTMLAttributes, ReactNode, Ref } from 'react'; import { ErrorSchema, FormContextType, GenericObjectType, IdSchema, Registry, RJSFSchema, StrictRJSFSchema, UiSchema, ValidatorType, TemplatesType, RegistryWidgetsType, RJSFValidationError, CustomValidator, Experimental_DefaultFormStateBehavior, ErrorTransformer } from '@rjsf/utils'; import Form, { IChangeEvent } from '@rjsf/core'; /** * The props for the `Field` components * @public */ export interface ScaffolderRJSFFieldProps extends GenericObjectType, Pick, Exclude, 'onBlur' | 'onFocus' | 'onChange'>> { /** The JSON subschema object for this field */ schema: S; /** The uiSchema for this field */ uiSchema: UiSchema; /** The tree of unique ids for every child field */ idSchema: IdSchema; /** The data for this field */ formData?: T; /** The tree of errors for this field and its children */ errorSchema?: ErrorSchema; /** The field change event handler; called with the updated form data and an optional `ErrorSchema` */ onChange: (newFormData: T | undefined, es?: ErrorSchema, id?: string) => any; /** The input blur event handler; call it with the field id and value */ onBlur: (id: string, value: any) => void; /** The input focus event handler; call it with the field id and value */ onFocus: (id: string, value: any) => void; /** The `formContext` object that you passed to `Form` */ formContext?: F; /** A boolean value stating if the field should autofocus */ autofocus?: boolean; /** A boolean value stating if the field is disabled */ disabled: boolean; /** A boolean value stating if the field is hiding its errors */ hideError?: boolean; /** A boolean value stating if the field is read-only */ readonly: boolean; /** The required status of this field */ required?: boolean; /** The unique name of the field, usually derived from the name of the property in the JSONSchema */ name: string; /** To avoid collisions with existing ids in the DOM, it is possible to change the prefix used for ids; * Default is `root` */ idPrefix?: string; /** To avoid using a path separator that is present in field names, it is possible to change the separator used for * ids (Default is `_`) */ idSeparator?: string; /** An array of strings listing all generated error messages from encountered errors for this field */ rawErrors: string[]; /** The `registry` object */ registry: Registry; } /** * The properties that are passed to the `Form` * @public */ export interface ScaffolderRJSFFormProps { /** The JSON schema object for the form */ schema: S; /** An implementation of the `ValidatorType` interface that is needed for form validation to work */ validator: ValidatorType; /** The optional children for the form, if provided, it will replace the default `SubmitButton` */ children?: ReactNode; /** The uiSchema for the form */ uiSchema?: UiSchema; /** The data for the form, used to prefill a form with existing data */ formData?: T; /** You can provide a `formContext` object to the form, which is passed down to all fields and widgets. Useful for * implementing context aware fields and widgets. * * NOTE: Setting `{readonlyAsDisabled: false}` on the formContext will make the antd theme treat readOnly fields as * disabled. */ formContext?: F; /** To avoid collisions with existing ids in the DOM, it is possible to change the prefix used for ids; * Default is `root` */ idPrefix?: string; /** To avoid using a path separator that is present in field names, it is possible to change the separator used for * ids (Default is `_`) */ idSeparator?: string; /** It's possible to disable the whole form by setting the `disabled` prop. The `disabled` prop is then forwarded down * to each field of the form. If you just want to disable some fields, see the `ui:disabled` parameter in `uiSchema` */ disabled?: boolean; /** It's possible to make the whole form read-only by setting the `readonly` prop. The `readonly` prop is then * forwarded down to each field of the form. If you just want to make some fields read-only, see the `ui:readonly` * parameter in `uiSchema` */ readonly?: boolean; /** The dictionary of registered fields in the form */ fields?: ScaffolderRJSFRegistryFieldsType; /** The dictionary of registered templates in the form; Partial allows a subset to be provided beyond the defaults */ templates?: Partial, 'ButtonTemplates'>> & { ButtonTemplates?: Partial['ButtonTemplates']>; }; /** The dictionary of registered widgets in the form */ widgets?: RegistryWidgetsType; /** If you plan on being notified every time the form data are updated, you can pass an `onChange` handler, which will * receive the same args as `onSubmit` any time a value is updated in the form. Can also return the `id` of the field * that caused the change */ onChange?: (data: IChangeEvent, id?: string) => void; /** To react when submitted form data are invalid, pass an `onError` handler. It will be passed the list of * encountered errors */ onError?: (errors: RJSFValidationError[]) => void; /** You can pass a function as the `onSubmit` prop of your `Form` component to listen to when the form is submitted * and its data are valid. It will be passed a result object having a `formData` attribute, which is the valid form * data you're usually after. The original event will also be passed as a second parameter */ onSubmit?: (data: IChangeEvent, event: FormEvent) => void; /** Sometimes you may want to trigger events or modify external state when a field has been touched, so you can pass * an `onBlur` handler, which will receive the id of the input that was blurred and the field value */ onBlur?: (id: string, data: any) => void; /** Sometimes you may want to trigger events or modify external state when a field has been focused, so you can pass * an `onFocus` handler, which will receive the id of the input that is focused and the field value */ onFocus?: (id: string, data: any) => void; /** The value of this prop will be passed to the `accept-charset` HTML attribute on the form */ acceptcharset?: string; /** The value of this prop will be passed to the `action` HTML attribute on the form * * NOTE: this just renders the `action` attribute in the HTML markup. There is no real network request being sent to * this `action` on submit. Instead, react-jsonschema-form catches the submit event with `event.preventDefault()` * and then calls the `onSubmit` function, where you could send a request programmatically with `fetch` or similar. */ action?: string; /** The value of this prop will be passed to the `autocomplete` HTML attribute on the form */ autoComplete?: string; /** The value of this prop will be passed to the `class` HTML attribute on the form */ className?: string; /** The value of this prop will be passed to the `enctype` HTML attribute on the form */ enctype?: string; /** The value of this prop will be passed to the `id` HTML attribute on the form */ id?: string; /** The value of this prop will be passed to the `name` HTML attribute on the form */ name?: string; /** The value of this prop will be passed to the `method` HTML attribute on the form */ method?: string; /** It's possible to change the default `form` tag name to a different HTML tag, which can be helpful if you are * nesting forms. However, native browser form behaviour, such as submitting when the `Enter` key is pressed, may no * longer work */ tagName?: ElementType; /** The value of this prop will be passed to the `target` HTML attribute on the form */ target?: string; /** Formerly the `validate` prop; Takes a function that specifies custom validation rules for the form */ customValidate?: CustomValidator; /** This prop allows passing in custom errors that are augmented with the existing JSON Schema errors on the form; it * can be used to implement asynchronous validation */ extraErrors?: ErrorSchema; /** If set to true, turns off HTML5 validation on the form; Set to `false` by default */ noHtml5Validate?: boolean; /** If set to true, turns off all validation. Set to `false` by default * * @deprecated - In a future release, this switch may be replaced by making `validator` prop optional */ noValidate?: boolean; /** If set to true, the form will perform validation and show any validation errors whenever the form data is changed, * rather than just on submit */ liveValidate?: boolean; /** If `omitExtraData` and `liveOmit` are both set to true, then extra form data values that are not in any form field * will be removed whenever `onChange` is called. Set to `false` by default */ liveOmit?: boolean; /** If set to true, then extra form data values that are not in any form field will be removed whenever `onSubmit` is * called. Set to `false` by default. */ omitExtraData?: boolean; /** When this prop is set to `top` or 'bottom', a list of errors (or the custom error list defined in the `ErrorList`) will also * show. When set to false, only inline input validation errors will be shown. Set to `top` by default */ showErrorList?: false | 'top' | 'bottom'; /** A function can be passed to this prop in order to make modifications to the default errors resulting from JSON * Schema validation */ transformErrors?: ErrorTransformer; /** If set to true, then the first field with an error will receive the focus when the form is submitted with errors */ focusOnFirstError?: boolean | ((error: RJSFValidationError) => void); /** Optional string translation function, if provided, allows users to change the translation of the RJSF internal * strings. Some strings contain replaceable parameter values as indicated by `%1`, `%2`, etc. The number after the * `%` indicates the order of the parameter. The ordering of parameters is important because some languages may choose * to put the second parameter before the first in its translation. */ translateString?: Registry['translateString']; /** Optional configuration object with flags, if provided, allows users to override default form state behavior * Currently only affecting minItems on array fields and handling of setting defaults based on the value of * `emptyObjectFields` */ experimental_defaultFormStateBehavior?: Experimental_DefaultFormStateBehavior; /** * _internalFormWrapper is currently used by the semantic-ui theme to provide a custom wrapper around `
` * that supports the proper rendering of those themes. To use this prop, one must pass a component that takes two * props: `children` and `as`. That component, at minimum, should render the `children` inside of a tag * unless `as` is provided, in which case, use the `as` prop in place of ``. * i.e.: * ``` * export default function InternalForm({ children, as }) { * const FormTag = as || 'form'; * return {children}; * } * ``` * * Use at your own risk as this prop is private and may change at any time without notice. */ _internalFormWrapper?: ElementType; /** Support receiving a React ref to the Form */ ref?: Ref>; } /** * The set of `Fields` stored in the `Registry` * @public */ export type ScaffolderRJSFRegistryFieldsType = { /** A `Field` indexed by `name` */ [name: string]: ScaffolderRJSFField; }; /** * The `Field` type for Field Extensions * @public */ export type ScaffolderRJSFField = ComponentType>;