Skip to Content

Form

A comprehensive form component built on react-hook-form with validation, error handling, and layout support.

Import

import { Form, FormTextInput, FormSelect, FormActions } from '@tidbcloud/uikit/biz'

Basic Usage

import { Form, FormTextInput, FormSelect } from '@tidbcloud/uikit/biz' function Demo() { const handleSubmit = (data) => { console.log(data) } return ( <Form onSubmit={handleSubmit}> <FormTextInput name="username" label="Username" required /> <FormTextInput name="email" label="Email" type="email" required /> <FormSelect name="role" label="Role" data={['Admin', 'User', 'Guest']} /> </Form> ) }

With Validation

import { Form, FormTextInput } from '@tidbcloud/uikit/biz' function Demo() { return ( <Form onSubmit={(data) => console.log(data)} defaultValues={{ email: '' }}> <FormTextInput name="email" label="Email" rules={{ required: 'Email is required', pattern: { value: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i, message: 'Invalid email address' } }} /> </Form> ) }

Date and Time Fields

FormDateTimePicker binds a date and time picker to a form field. Set the initial value through the form’s defaultValues (or the external useForm instance). Field values are ISO strings; onChange receives a Date. utcOffset controls the displayed timezone while the stored value remains a UTC ISO string.

import { Form, FormDateTimePicker } from '@tidbcloud/uikit/biz' function Demo() { return ( <Form defaultValues={{ startTime: '2026-10-25T08:15:30.000Z' }} onSubmit={(data) => console.log(data)}> <FormDateTimePicker name="startTime" label="Start time" utcOffset="+08:00" format="YYYY-MM-DD HH:mm:ss Z" rules={{ required: 'Start time is required' }} /> </Form> ) }

The example displays 2026-10-25 16:15:30 +08:00 and submits 2026-10-25T08:15:30.000Z until changed. Pass startDate and endDate as absolute Date values to limit the selectable range. Input props such as label, required, disabled, size, and onBlur are forwarded to the picker. Form validation errors appear below the input, and invalid submissions focus it automatically.

Missing or invalid values use the picker’s visual fallback without writing a value to the form or firing onChange. Use rules.required to require a selection and rules.validate if existing values also need date or range validation.

Horizontal Layout

import { Form, FormTextInput } from '@tidbcloud/uikit/biz' function Demo() { return ( <Form layout="horizontal" onSubmit={(data) => console.log(data)}> <FormTextInput name="firstName" label="First Name" /> <FormTextInput name="lastName" label="Last Name" /> </Form> ) }

Without Actions

import { Form, FormTextInput } from '@tidbcloud/uikit/biz' import { Button } from '@tidbcloud/uikit' function Demo() { return ( <Form withActions={false} onSubmit={(data) => console.log(data)}> <FormTextInput name="search" label="Search" /> <Button type="submit">Search</Button> </Form> ) }

Custom Actions

import { Form, FormTextInput, FormActions } from '@tidbcloud/uikit/biz' function Demo() { return ( <Form onSubmit={(data) => console.log(data)} onCancel={() => console.log('cancelled')} actionsProps={{ cancelText: 'Reset', confirmText: 'Save Changes' }} > <FormTextInput name="name" label="Name" /> </Form> ) }

With Error Message

import { Form, FormTextInput } from '@tidbcloud/uikit/biz' function Demo() { return ( <Form errorMessage="Failed to submit form. Please try again." onSubmit={(data) => console.log(data)}> <FormTextInput name="name" label="Name" /> </Form> ) }

Form error messages support basic text formatting (a, b, br, code, em, i, li, ol, p, span, strong, and ul). Only href and title attributes are retained. Links can use HTTP, HTTPS, mailto, or relative URLs; images, SVG, styles, event handlers, and other attributes are removed.

Form Props

PropTypeDefaultDescription
errorMessagestring-Error message to display
mode'onChange' | 'onBlur' | 'onSubmit'-react-hook-form mode
reValidateMode'onChange' | 'onBlur' | 'onSubmit''onChange'Re-validation mode
defaultValuesobject-Default form values
formUseFormReturn-External form instance
withActionsbooleantrueShow form actions
actionsPropsFormActionsProps-Props for actions
errorMessagePropsFormErrorMessageProps-Props for error message
layout'horizontal' | 'vertical' | 'none''vertical'Form layout
layoutPropsobject-Props for layout
stopPropagationboolean-Stop submit event propagation
preventDefaultboolean-Prevent default submit
onSubmit(data) => void | Promise-Form submit handler
onRefresh() => void-Refresh callback
onError() => any-Error handler
onCancel() => void-Cancel callback
onUnmount() => void-Unmount callback

FormActions Props

PropTypeDefaultDescription
loadingboolean-Loading state
disabledboolean-Disabled state
onCancel() => void-Cancel handler
onConfirm() => void-Confirm handler
cancelTextReactNode'Cancel'Cancel button text
confirmTextReactNode'Confirm'Confirm button text
cancelPropsButtonProps-Cancel button props
confirmPropsButtonProps-Confirm button props

Form Field Components

ComponentDescription
FormTextInputText input field
FormPasswordInputPassword input field
FormNumberInputNumber input field
FormTextAreaTextarea field
FormSelectSelect dropdown
FormMultiSelectMulti-select dropdown
FormSwitchSwitch toggle
FormCheckboxCheckbox field
FormRadioGroupRadio button group
FormRatingRating input
FormPhoneInputPhone number input
FormProMultiSelectEnhanced multi-select
FormDateTimePickerDate and time picker
FormCopyTextCopyable text field
FormSegmentControlSegmented control
Last updated on