生态产品多个设计器现已发布候选版本

查看生态总览
Skip to content

Form ​

Data entry component. This page is the API reference, generated from the source code. Usage guidance and live examples are on the Chinese page.

Anatomy ​

Parts in bold are required.

data-scope="form": root · field-group · error-summary · error-summary-item · submit-trigger · reset-trigger

API reference ​

Descriptions of individual props, events and slots are written in Chinese and appear on the Chinese page.

Artifacts ​

LayerValue
Custom element<xh-form>
Vue componentsXhFormErrorSummary XhFormErrorSummaryItem XhFormFieldGroup XhFormResetTrigger XhFormRoot XhFormSubmitTrigger
ComposableuseForm
State machineformMachine
Skin@xihan-ui/styles/form.css

Props ​

PropTypeRequired
valuesTValues
defaultValuesTValues
errorsFormErrorPatch
defaultErrorsFormErrorPatch
validate(values: TValues) => FormErrorPatchOf<TValues> | Promise<FormErrorPatchOf<TValues>>
schemaFormStandardSchema
rulesFormRules<TValues>
validateMessagesFormValidateMessages
translationsPartial<FormTranslations>
validateOnFormValidateOn
layoutFormLayout
columnsFormColumns
labelWidthnumber | string
labelAlign'start' | 'end'
disabledboolean
readOnlyboolean
onValuesChange(details: FormValuesChangeDetails<TValues>) => void
onErrorsChange(details: FormErrorsChangeDetails) => void
onSubmit(details: FormSubmitDetails<TValues>) => void | PromiseLike<unknown>
onSubmitError(details: FormSubmitErrorDetails<TValues>) => void
onInvalid(details: FormInvalidDetails<TValues>) => void
onValidationError(details: FormValidationErrorDetails<TValues>) => void

Events ​

Custom elements put the payload in detail; Vue emits an event of the same name.

EventPayload
values-changeFormValuesChangeDetails
errors-changeFormErrorsChangeDetails
submitFormSubmitDetails
submit-errorFormSubmitErrorDetails
invalidFormSubmitDetails
validation-errorFormValidationErrorDetails

Slots ​

Only slots with a payload are listed.

Vue componentSlotPayload
XhFormErrorSummarydefaultFormErrorSummarySlotProps
XhFormErrorSummaryItemdefaultFormErrorSummaryItemSlotProps
XhFormFieldGroupdefaultFormFieldGroupSlotProps
XhFormRootdefaultFormRootSlotProps

React adapter props ​

Only the props each component declares itself: DOM attributes inherited from ComponentPropsWithRef are left out, and root props already listed in the Props table are not repeated. The Vue counterpart is the slot table above.

React componentPropTypeRequired
XhFormErrorSummarychildrenSlotChildren<FormErrorSummarySlotProps>
XhFormErrorSummaryItemnamePYes
XhFormErrorSummaryItemchildrenSlotChildren<FormErrorSummaryItemSlotProps<P>>
XhFormFieldGroupnamePYes
XhFormFieldGroupspanFormFieldSpan
XhFormFieldGroupchildrenSlotChildren<FormFieldGroupSlotProps<TValues, P>>
XhFormRootchildrenSlotChildren<FormRootSlotProps<TValues>>

State ​

Public state is written to data-state.

PartValues
root'invalid' | 'idle'
error-summary'invalid' | 'idle'

The following names are internal to the state machine.

States: idle · invalid

Events: SUBMIT · RESET · FIELD.RESET · VALIDATE · SUBMIT.SETTLED · VALIDATION.PASS · VALIDATION.FAIL · FIELD.SET · FIELD.ARRAY.MUTATE · FIELD.BLUR · ERROR.SET · ERRORS.CLEAR · ERROR.FOCUS · PRESS.START · PRESS.END · SUMMARY.SNAPSHOT · SUMMARY.RENDERED

Guards: isEnabled · isEditable · isValidationSnapshotCurrent · canPress · isSubmitting

connect API ​

getXxxProps() returns the host attributes of the matching part.

MemberType
valuesTValues
errorsFormErrors
errorNamesFormPath[]
errorCountnumber
invalidboolean
submitFailedboolean
validatingboolean
submittingboolean
dirtyboolean
validationErrorFormValidationErrorDetails<TValues> | null
disabledboolean
readOnlyboolean
validateOnFormValidateOn
layoutFormLayout
getFieldId(name: FormFieldPath<TValues>) => string
getFieldValue<P extends FormFieldPath<TValues>>(name: P) => FormPathValue<TValues, P> | undefined
getFieldError(name: FormFieldPath<TValues>) => string | undefined
summaryErrorsFormErrors
summaryErrorNamesFormPath[]
summaryErrorCountnumber
getSummaryError(name: FormFieldPath<TValues>) => string | undefined
isFieldInvalid(name: FormFieldPath<TValues>) => boolean
isFieldDirty(name: FormFieldPath<TValues>) => boolean
isFieldTouched(name: FormFieldPath<TValues>) => boolean
isFieldRequired(name: FormFieldPath<TValues>) => boolean
setFieldValue<P extends FormFieldPath<TValues>>(name: P, value: FormPathValue<TValues, P>) => void
setFieldError(name: FormFieldPath<TValues>, message?: string) => void
clearErrors() => void
submit() => void
validateAll() => Promise<FormValidateResult>
validateField(name: FormFieldPath<TValues>) => Promise<FormValidateResult>
validateFields(names: readonly FormFieldPath<TValues>[]) => Promise<FormValidateResult>
resetField(name: FormFieldPath<TValues>) => void
reset() => void
getRootProps() => T['element']
getFieldGroupProps(props: FormFieldGroupProps<TValues>) => T['element']
getErrorSummaryProps() => T['element']
getErrorSummaryItemProps(props: FormErrorSummaryItemProps<TValues>) => T['element']
getSubmitTriggerProps() => T['button']
getResetTriggerProps() => T['button']

Accessibility ​

Keyboard ​

Specification: W3C APG. Each row is a stable behavior id that the conformance tests cover; the conditions and effects are described on the Chinese page.

KeysBehavior
Enter / Spaceform.kbd.press

ARIA ​

These attributes are produced by connect.

PartAttributeValue
rootaria-busy'true' | undefined
error-summaryaria-atomic'true'
error-summaryaria-live'assertive'
error-summaryrole'alert'
submit-triggeraria-busy'true' | undefined
submit-triggeraria-disabled'true' | undefined

Style reference ​

Skin ​

@xihan-ui/styles/form.css is written against [data-scope="form"][data-part="root"] part selectors; the published build uses the mount class .xh-scope-form in place of data-scope (same specificity) and sits in the xihan.components layer. Put overrides in xihan.overrides; the part selectors work as usual.

Data attributes ​

Produced by connect; valueless attributes are omitted while their condition does not hold.

PartAttributeValue
rootdata-columnscolumns.base
rootdata-columns-lgcolumns.lg
rootdata-columns-mdcolumns.md
rootdata-columns-smcolumns.sm
rootdata-columns-xlcolumns.xl
rootdata-disabled'' (present only while the condition holds)
rootdata-invalid'' (present only while the condition holds)
rootdata-label-alignprops.labelAlign
rootdata-layoutprops.layout
rootdata-readonly'' (present only while the condition holds)
rootdata-state'invalid' | 'idle'
field-groupdata-disabled'' (present only while the condition holds)
field-groupdata-invalid'' (present only while the condition holds)
field-groupdata-readonly'' (present only while the condition holds)
field-groupdata-spanfieldSpan(field.span)
error-summarydata-countString(summaryErrorNames.length)
error-summarydata-state'invalid' | 'idle'
error-summary-itemdata-invalid'' (present only while the condition holds)
error-summary-itemdata-pressed'' (present only while the condition holds)
submit-triggerdata-disabled'' (present only while the condition holds)
submit-triggerdata-loading'' (present only while the condition holds)
submit-triggerdata-pressed'' (present only while the condition holds)
submit-triggerdata-xh-action-control''
submit-triggerdata-xh-action-display'always'
submit-triggerdata-xh-action-profile'text'
submit-triggerdata-xh-action-size'md'
submit-triggerdata-xh-action-variant'solid'
submit-triggerdata-xh-ink-surface''
submit-triggerdata-xh-loading-ring'overlay'
reset-triggerdata-disabled'' (present only while the condition holds)
reset-triggerdata-pressed'' (present only while the condition holds)
reset-triggerdata-xh-action-control''
reset-triggerdata-xh-action-display'always'
reset-triggerdata-xh-action-profile'text'
reset-triggerdata-xh-action-size'md'
reset-triggerdata-xh-action-variant'outline'

CSS variables ​

The public override slots, generated from what the standalone skin actually consumes.

VariablePartCSS propertyStateDefault from
--xh-form-field-gapfield-groupgapdefault--xh-space-1
--xh-form-gaprootcolumn-gap
margin-block-end
layout=grid
where(:not(:last-child)
where([data-scope='form'][data-part='root'])
--xh-space-5
--xh-form-inline-gaprootcolumn-gaplayout=inline--xh-space-6
--xh-form-inline-row-gaprootmargin-block-endlayout=inline
where(:not(:last-child)
where([data-scope='form'][data-part='root'])
--xh-space-2
--xh-form-label-wrootgrid-template-columnslayout=horizontalcalc(100% * 5 / 24)
--xh-form-loading-durationsubmit-triggeranimationxh-loading-ring--xh-motion-loop-spin
--xh-form-submit-bgsubmit-trigger--xh-ink-surface
background-color
default
xh-ink-surface
--xh-_action-variant-bg-rest
--xh-form-submit-bg-activesubmit-triggerbackground-colordisabled
is(:active, [data-pressed])
loading
not([data-disabled])
not([data-loading])
pressed
--xh-_action-variant-bg-pressed
--xh-form-submit-bg-hoversubmit-triggerbackground-colordisabled
hover
loading
not([data-disabled])
not([data-loading])
--xh-_action-variant-bg-hover
--xh-form-submit-bordersubmit-triggerborderdefault--xh-_action-variant-border-rest
--xh-form-submit-border-activesubmit-triggerborder-colordisabled
is(:active, [data-pressed])
loading
not([data-disabled])
not([data-loading])
pressed
--xh-_action-variant-border-pressed
--xh-form-submit-border-hoversubmit-triggerborder-colordisabled
hover
loading
not([data-disabled])
not([data-loading])
--xh-_action-variant-border-hover
--xh-form-submit-fgsubmit-triggerborder
border-color
color
@media (prefers-reduced-motion: reduce)
@media print
default
disabled
hover
is(:active, [data-pressed])
loading
motion=reduce
not([data-disabled])
not([data-loading])
pressed
where([data-motion='reduce'])
xh-loading-ring
xh-loading-ring=overlay
--xh-_action-variant-fg-hover
--xh-_action-variant-fg-loading
--xh-_action-variant-fg-pressed
--xh-_action-variant-fg-rest
--xh-form-submit-shadowsubmit-triggerbox-shadowdefault
disabled
hover
is(:active, [data-pressed])
loading
not([data-disabled])
not([data-loading])
pressed
--xh-_highlight-brand
--xh-form-summary-bgerror-summarybackgrounddefault--xh-bg-surface
--xh-form-summary-bordererror-summaryborderdefault--xh-border-invalid
--xh-form-summary-fgerror-summarycolordefault--xh-fg-danger
--xh-form-summary-font-sizeerror-summaryfont-sizedefault--xh-text-body-size
--xh-form-summary-gaperror-summarygapdefault--xh-space-1_5
--xh-form-summary-item-bgerror-summary-itembackgrounddefaulttransparent
--xh-form-summary-item-bg-hovererror-summary-itembackgroundhover--xh-bg-subtle
--xh-form-summary-item-bg-pressederror-summary-itembackgroundis(:active, [data-pressed])
pressed
--xh-bg-subtle-hover
--xh-form-summary-item-fg-hovererror-summary-itemcolorhover
is(:active, [data-pressed])
pressed
--xh-fg-danger-hover
--xh-form-summary-item-font-sizeerror-summary-itemfont-sizedefault--xh-text-secondary-size
--xh-form-summary-item-pxerror-summary-itempadding-inlinedefault--xh-space-1
--xh-form-summary-item-radiuserror-summary-itemborder-radiusdefault--xh-shape-control
--xh-form-summary-item-underline-offseterror-summary-itemtext-underline-offsetdefault--xh-space-0_5
--xh-form-summary-pxerror-summarypadding-inlinedefault--xh-control-px-md
--xh-form-summary-pyerror-summarypadding-blockdefault--xh-space-3
--xh-form-summary-radiuserror-summaryborder-radiusdefault--xh-shape-surface
--xh-form-summary-shadowerror-summarybox-shadowdefaultnone
--xh-form-trigger-bgreset-trigger--xh-ink-surface
background-color
default
xh-ink-surface
--xh-_action-variant-bg-rest
--xh-form-trigger-bg-activereset-triggerbackground-colordisabled
is(:active, [data-pressed])
loading
not([data-disabled])
not([data-loading])
pressed
--xh-_action-variant-bg-pressed
--xh-form-trigger-bg-disabledreset-trigger
submit-trigger
--xh-ink-surface
background-color
disabled
xh-ink-surface
--xh-_action-variant-bg-disabled
--xh-form-trigger-bg-hoverreset-triggerbackground-colordisabled
hover
loading
not([data-disabled])
not([data-loading])
--xh-_action-variant-bg-hover
--xh-form-trigger-borderreset-triggerborderdefault--xh-_action-variant-border-rest
--xh-form-trigger-border-disabledreset-trigger
submit-trigger
border-colordisabled--xh-_action-variant-border-disabled
--xh-form-trigger-border-hoverreset-triggerborder-colordisabled
hover
loading
not([data-disabled])
not([data-loading])
--xh-_action-variant-border-hover
--xh-form-trigger-fgreset-triggercolordefault
disabled
hover
is(:active, [data-pressed])
loading
not([data-disabled])
not([data-loading])
pressed
--xh-_action-variant-fg-hover
--xh-_action-variant-fg-pressed
--xh-_action-variant-fg-rest
--xh-form-trigger-font-sizereset-trigger
submit-trigger
font-sizedefault--xh-text-body-size
--xh-form-trigger-hreset-trigger
submit-trigger
block-sizedefault--xh-_action-profile-visual-size
--xh-form-trigger-pxreset-trigger
submit-trigger
padding-inlinedefault--xh-_action-profile-padding-inline
--xh-form-trigger-radiusreset-trigger
submit-trigger
border-radiusdefault--xh-shape-control

Released under The MIT License