2024-10-18 14:48:40 +00:00
|
|
|
/**
|
|
|
|
*
|
2024-10-21 09:30:11 +00:00
|
|
|
* Form provides validation functionality and manages form state.
|
2024-10-18 14:48:40 +00:00
|
|
|
*
|
2024-11-01 07:08:02 +00:00
|
|
|
* [Live Demo](https://www.primevue.org/forms/)
|
2024-10-18 14:48:40 +00:00
|
|
|
*
|
2024-10-21 09:30:11 +00:00
|
|
|
* @module form
|
2024-10-18 14:48:40 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
import type { DefineComponent, DesignToken, EmitFn, PassThrough } from '@primevue/core';
|
|
|
|
import type { ComponentHooks } from '@primevue/core/basecomponent';
|
2024-10-21 09:30:11 +00:00
|
|
|
import { VNode } from 'vue';
|
2024-11-01 21:53:41 +00:00
|
|
|
import type { PassThroughOptions } from '../types';
|
2024-10-18 14:48:40 +00:00
|
|
|
|
2024-10-21 09:30:11 +00:00
|
|
|
export declare type FormPassThroughOptionType = FormPassThroughAttributes | ((options: FormPassThroughMethodOptions) => FormPassThroughAttributes | string) | string | null | undefined;
|
2024-10-18 14:48:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Custom passthrough(pt) option method.
|
|
|
|
*/
|
2024-10-21 09:30:11 +00:00
|
|
|
export interface FormPassThroughMethodOptions {
|
2024-10-18 14:48:40 +00:00
|
|
|
/**
|
|
|
|
* Defines instance.
|
|
|
|
*/
|
|
|
|
instance: any;
|
|
|
|
/**
|
|
|
|
* Defines valid properties.
|
|
|
|
*/
|
2024-10-21 09:30:11 +00:00
|
|
|
props: FormProps;
|
2024-10-18 14:48:40 +00:00
|
|
|
/**
|
|
|
|
* Defines valid attributes.
|
|
|
|
*/
|
|
|
|
attrs: any;
|
|
|
|
/**
|
|
|
|
* Defines parent options.
|
|
|
|
*/
|
|
|
|
parent: any;
|
|
|
|
/**
|
|
|
|
* Defines passthrough(pt) options in global config.
|
|
|
|
*/
|
|
|
|
global: object | undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Custom passthrough(pt) options.
|
2024-10-21 09:30:11 +00:00
|
|
|
* @see {@link FormProps.pt}
|
2024-10-18 14:48:40 +00:00
|
|
|
*/
|
2024-10-21 09:30:11 +00:00
|
|
|
export interface FormPassThroughOptions {
|
2024-10-18 14:48:40 +00:00
|
|
|
/**
|
|
|
|
* Used to pass attributes to the root's DOM element.
|
|
|
|
*/
|
2024-10-21 09:30:11 +00:00
|
|
|
root?: FormPassThroughOptionType;
|
2024-10-18 14:48:40 +00:00
|
|
|
/**
|
|
|
|
* Used to manage all lifecycle hooks.
|
|
|
|
* @see {@link BaseComponent.ComponentHooks}
|
|
|
|
*/
|
|
|
|
hooks?: ComponentHooks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Custom passthrough attributes for each DOM elements
|
|
|
|
*/
|
2024-10-21 09:30:11 +00:00
|
|
|
export interface FormPassThroughAttributes {
|
2024-10-18 14:48:40 +00:00
|
|
|
[key: string]: any;
|
|
|
|
}
|
|
|
|
|
2024-10-23 09:27:41 +00:00
|
|
|
/**
|
|
|
|
* Resolver options for Form component.
|
|
|
|
*/
|
|
|
|
export interface FormResolverOptions {
|
|
|
|
/**
|
|
|
|
* The values of the form fields.
|
|
|
|
*/
|
|
|
|
values: Record<string, any>;
|
|
|
|
/**
|
|
|
|
* The names of the form fields.
|
|
|
|
*/
|
|
|
|
names: string[] | undefined;
|
|
|
|
}
|
|
|
|
|
2024-10-23 10:38:23 +00:00
|
|
|
/**
|
|
|
|
* Submit events
|
|
|
|
*/
|
|
|
|
export interface FormSubmitEvent {
|
|
|
|
/**
|
|
|
|
* The original DOM event.
|
|
|
|
*/
|
|
|
|
originalEvent: Event;
|
|
|
|
/**
|
|
|
|
* The form values.
|
|
|
|
*/
|
|
|
|
values: Record<string, any>;
|
|
|
|
/**
|
|
|
|
* The form state.
|
|
|
|
*/
|
|
|
|
states: Record<string, FormFieldState>;
|
|
|
|
/**
|
|
|
|
* Whether the form is valid.
|
|
|
|
*/
|
|
|
|
valid: boolean;
|
|
|
|
/**
|
|
|
|
* The form errors.
|
|
|
|
*/
|
|
|
|
errors: any[];
|
|
|
|
/**
|
|
|
|
* Resets the form.
|
|
|
|
*/
|
|
|
|
reset: () => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The state of a form field.
|
|
|
|
*/
|
|
|
|
export interface FormFieldState {
|
|
|
|
/**
|
|
|
|
* The value of the form field.
|
|
|
|
*/
|
|
|
|
value: any;
|
|
|
|
/**
|
|
|
|
* Whether the form field has been touched.
|
2024-10-23 20:37:45 +00:00
|
|
|
* @defaultValue false
|
2024-10-23 10:38:23 +00:00
|
|
|
*/
|
|
|
|
touched: boolean;
|
|
|
|
/**
|
|
|
|
* Whether the form field has been modified.
|
2024-10-23 20:37:45 +00:00
|
|
|
* @defaultValue false
|
2024-10-23 10:38:23 +00:00
|
|
|
*/
|
|
|
|
dirty: boolean;
|
|
|
|
/**
|
|
|
|
* Whether the form field has not been modified.
|
2024-10-23 20:37:45 +00:00
|
|
|
* @defaultValue true
|
2024-10-23 10:38:23 +00:00
|
|
|
*/
|
|
|
|
pristine: boolean;
|
|
|
|
/**
|
|
|
|
* Whether the form field is valid.
|
2024-10-23 20:37:45 +00:00
|
|
|
* @defaultValue true
|
2024-10-23 10:38:23 +00:00
|
|
|
*/
|
|
|
|
valid: boolean;
|
|
|
|
/**
|
|
|
|
* Whether the form field is invalid.
|
2024-10-23 20:37:45 +00:00
|
|
|
* @defaultValue false
|
2024-10-23 10:38:23 +00:00
|
|
|
*/
|
|
|
|
invalid: boolean;
|
|
|
|
/**
|
|
|
|
* The first error message of the form field.
|
|
|
|
*/
|
|
|
|
error: any;
|
|
|
|
/**
|
|
|
|
* All error messages of the form field.
|
2024-10-23 20:37:45 +00:00
|
|
|
* @defaultValue []
|
2024-10-23 10:38:23 +00:00
|
|
|
*/
|
|
|
|
errors: any[];
|
|
|
|
}
|
|
|
|
|
2024-10-18 14:48:40 +00:00
|
|
|
/**
|
2024-10-21 09:30:11 +00:00
|
|
|
* Defines valid properties in Form component.
|
2024-10-18 14:48:40 +00:00
|
|
|
*/
|
2024-10-21 09:30:11 +00:00
|
|
|
export interface FormProps {
|
|
|
|
/**
|
|
|
|
* A function that resolves validation logic.
|
2024-10-23 09:27:41 +00:00
|
|
|
* @param {FormResolverOptions} e - Resolver options
|
2024-10-21 09:30:11 +00:00
|
|
|
*/
|
2024-10-23 09:27:41 +00:00
|
|
|
resolver?: (e: FormResolverOptions) => Promise<Record<string, any>> | Record<string, any> | undefined;
|
2024-10-21 09:30:11 +00:00
|
|
|
/**
|
2024-10-22 09:14:17 +00:00
|
|
|
* The initial values for the form fields.
|
2024-10-21 09:30:11 +00:00
|
|
|
*/
|
2024-10-22 09:14:17 +00:00
|
|
|
initialValues?: Record<string, any> | undefined;
|
2024-10-21 09:30:11 +00:00
|
|
|
/**
|
|
|
|
* Whether to validate the form fields when the values change.
|
|
|
|
* @defaultValue true
|
|
|
|
*/
|
|
|
|
validateOnValueUpdate?: boolean | string[] | undefined;
|
|
|
|
/**
|
|
|
|
* Whether to validate the form fields when they lose focus (on blur).
|
|
|
|
* @defaultValue false
|
|
|
|
*/
|
|
|
|
validateOnBlur?: boolean | string[] | undefined;
|
|
|
|
/**
|
|
|
|
* Whether to validate the form fields immediately after the form is mounted.
|
|
|
|
* @defaultValue false
|
|
|
|
*/
|
|
|
|
validateOnMount?: boolean | string[] | undefined;
|
|
|
|
/**
|
|
|
|
* Whether to validate the form fields when the form is submitted.
|
|
|
|
* @defaultValue true
|
|
|
|
*/
|
|
|
|
validateOnSubmit?: boolean | string[] | undefined;
|
2024-10-18 14:48:40 +00:00
|
|
|
/**
|
|
|
|
* It generates scoped CSS variables using design tokens for the component.
|
|
|
|
*/
|
|
|
|
dt?: DesignToken<any>;
|
|
|
|
/**
|
|
|
|
* Used to pass attributes to DOM elements inside the component.
|
2024-10-21 09:30:11 +00:00
|
|
|
* @type {FormPassThroughOptions}
|
2024-10-18 14:48:40 +00:00
|
|
|
*/
|
2024-10-21 09:30:11 +00:00
|
|
|
pt?: PassThrough<FormPassThroughOptions>;
|
2024-10-18 14:48:40 +00:00
|
|
|
/**
|
|
|
|
* Used to configure passthrough(pt) options of the component.
|
|
|
|
* @type {PassThroughOptions}
|
|
|
|
*/
|
|
|
|
ptOptions?: PassThroughOptions;
|
|
|
|
/**
|
|
|
|
* When enabled, it removes component related styles in the core.
|
|
|
|
* @defaultValue false
|
|
|
|
*/
|
|
|
|
unstyled?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-10-21 09:30:11 +00:00
|
|
|
* Defines valid slots in Form component.
|
2024-10-18 14:48:40 +00:00
|
|
|
*/
|
2024-10-21 09:30:11 +00:00
|
|
|
export interface FormSlots {
|
2024-10-18 14:48:40 +00:00
|
|
|
/**
|
|
|
|
* Default content slot.
|
2024-10-23 10:38:23 +00:00
|
|
|
* @param {Object} scope - default slot's params.
|
2024-10-18 14:48:40 +00:00
|
|
|
*/
|
2024-12-09 09:53:39 +00:00
|
|
|
default: (
|
|
|
|
scope: {
|
|
|
|
/**
|
|
|
|
* Registers a form field for validation and tracking.
|
|
|
|
* @param field - The name of the form field to register.
|
|
|
|
* @param options - Configuration options for the field, such as validation rules.
|
|
|
|
* @returns - Returns an object or value representing the registered field.
|
|
|
|
*/
|
|
|
|
register: (field: string, options: any) => any;
|
|
|
|
/**
|
|
|
|
* Resets the entire form state, clearing values and validation statuses.
|
|
|
|
*/
|
|
|
|
reset: () => void;
|
|
|
|
/**
|
|
|
|
* Indicates whether the form is valid, returning `true` if all fields pass validation.
|
|
|
|
*/
|
|
|
|
valid: boolean;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Index signature for dynamically added form fields.
|
|
|
|
*/
|
|
|
|
} & Record<string, FormFieldState>
|
|
|
|
) => VNode[];
|
2024-10-18 14:48:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-10-21 09:30:11 +00:00
|
|
|
* Defines valid emits in Form component.
|
2024-10-18 14:48:40 +00:00
|
|
|
*/
|
2024-10-21 09:30:11 +00:00
|
|
|
export interface FormEmitsOptions {
|
|
|
|
/**
|
|
|
|
* Emitted when the form is submitted.
|
2024-11-01 06:02:14 +00:00
|
|
|
* @param {FormSubmitEvent} event - Custom submit event.
|
2024-10-21 09:30:11 +00:00
|
|
|
*/
|
2024-11-01 06:02:14 +00:00
|
|
|
submit: (event: FormSubmitEvent) => void;
|
2024-10-21 09:30:11 +00:00
|
|
|
}
|
2024-10-18 14:48:40 +00:00
|
|
|
|
2024-10-21 09:30:11 +00:00
|
|
|
export declare type FormEmits = EmitFn<FormEmitsOptions>;
|
2024-10-18 14:48:40 +00:00
|
|
|
|
|
|
|
/**
|
2024-10-21 09:30:11 +00:00
|
|
|
* **PrimeVue - Form**
|
2024-10-18 14:48:40 +00:00
|
|
|
*
|
2024-10-21 09:30:11 +00:00
|
|
|
* _Form provides validation functionality and manages form state._
|
2024-10-18 14:48:40 +00:00
|
|
|
*
|
2024-11-01 07:08:02 +00:00
|
|
|
* [Live Demo](https://www.primevue.org/forms/)
|
2024-10-18 14:48:40 +00:00
|
|
|
* --- ---
|
|
|
|
* 
|
|
|
|
*
|
|
|
|
* @group Component
|
|
|
|
*
|
|
|
|
*/
|
2024-10-21 09:30:11 +00:00
|
|
|
declare const Form: DefineComponent<FormProps, FormSlots, FormEmits>;
|
2024-10-18 14:48:40 +00:00
|
|
|
|
|
|
|
declare module 'vue' {
|
|
|
|
export interface GlobalComponents {
|
2024-10-21 09:30:11 +00:00
|
|
|
Form: DefineComponent<FormProps, FormSlots, FormEmits>;
|
2024-10-18 14:48:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-21 09:30:11 +00:00
|
|
|
export default Form;
|