Skip to content

KForm

With form, you can collect, verify and submit data.

Install

bash
pnpm add @ikun-ui/form
bash
yarn add @ikun-ui/form
bash
npm install @ikun-ui/form

Basic usage

Form contains three components, they are KForm, KFormItem

Alignment

Label has three alignment methods and two position settings.

Validation

The following is a best practice for form validation

Custom Validation Rules

Support custom form verification functions by setting rules

Size Control

Form supports three sizes

Form Props

NameTypeDefaultDescription
rulesKFormRules-Form validation rules
initValueany-Form initial value.
sizesm | md | lgmdSize of form
disabledbooleanfalseDisabled the form
manualValidatebooleanfalseWhether to verify when inputting.
labelAlignleft | right | centerrightLabel aligned horizontally.
labelPositionhorizontal | vertical horizontalLabel display position.
labelWidthstring-Label width
clsstring''Additional class for component
attrsany{}Additional attributes
typescript
export interface KFormRule {
	// maximum value
	max?: number;
	// minimum value
	min?: number;
	required?: boolean;
	// Custom verification function
	validator?: (value: any, callback: any) => void;
	// error message
	msg?: string;
}

export type KFormRules = Record<string, KFormRule[]>;

Form Events

NameDescriptionType
validateTriggers when the form is validated, but it doesn't fire when the validateForm api is called.(event: ValidateData)=> void
typescript
export interface ValidateData {
	// current form value
	data: any;
	// Whether the verification passed
	isValid: boolean;
	// Fields and information that fail verification
	invalidFields?: ValidateError[];
}

export type Value = any;
export type Values = Record<string, Value>;
export interface ValidateError {
	// error message
	message?: string;
	fieldValue?: Value;
	field?: string;
}

Form Slots

NameDescription
defaultCustomize default content, Please pass in the KFormItem.

Form API

NameDescription
validateFormValidate the entire form object.
resetFormReset the entire form to default values and clear validation.
setFormSet the entire form object value.
getFormGet the entire form object value.
validateFieldValidate the specified fields.
clearValidateFieldClear verification information of specific field.
setFieldSet the specified field value.
typescript
export interface IKunFormInstance {
	/**
	 * Validate the entire form object
	 * @public
	 * @param callback
	 */
	validateForm(callback: FormValidateCallback): void;
	/**
	 * Reset the entire form to default values and clear validation
	 * @public
	 */
	resetForm(): void;
	/**
	 * Set the entire form object value
	 * @public
	 * @param values
	 * @param isValidate Whether to Validate form fields
	 */
	setForm(values: any, isValidate: boolean): void;
	/**
	 * get the entire form object value
	 * @public
	 */
	getForm<T>(): T;
	/**
	 * Validate the specific fields
	 * @public
	 * @param path field path
	 */
	validateField(path: string): void;
	/**
	 * Clear verification information of specific field
	 * @public
	 * @param path field path
	 */
	clearValidateField(path: string): void;
	/**
	 * Set the specified field value
	 * @public
	 * @param path field path
	 * @param value
	 * @param isValidate Whether to Validate form fields
	 */
	setField(path: string, values: any, isValidate: boolean): void;
}

Form Props

NameTypeDefaultDescription
fieldstring-Specify the field. If you want to specify a subfield of the object, please use foo.xxx.
labelstringmdLabel content.
showMsgbooleanfalseWhether to display an error message when the verification is triggered
clsstring''Additional class for component
attrsany{}Additional attributes

Form Item Slots

NameDescription
defaultCustomize default content

MIT Licensed