useFormFieldContext
字段上下文。Reka UI createContext,和 provideFormFieldContext 成对。没包在 FormField 里时返回 null,控件可以单独用。控件走 useFormFieldControl 拼 id、aria 和 data-*。
Installation
pnpm dlx @weme-ui/weme-ui add weme-ui/slim/use-form-field-contextUsage
import {
provideFormFieldContext,
useFormFieldContext,
useFormFieldControl,
} from '~/composables/use-form-field-context'
provideFormFieldContext({/* FormFieldContextValue */})
const field = useFormFieldContext()
const control = useFormFieldControl(props)Returns
useFormFieldContext() 给出 FormFieldContextValue。没提供上下文时返回 null,不抛错。
provideFormFieldContext
配对的 Provider,参数是 FormFieldContextValue。
useFormFieldControl
参数是 FormFieldSharedProps。合并 props 与 Context。没有 Context 时用 props,并自己记聚焦、写 data-focused。registerControl 在没有 Context 时是空操作。
FieldType
bindingsComputedRef<object>
控件 attrs、aria
ComputedRef<object>
idComputedRef<string | undefined>
控件 id
ComputedRef<string | undefined>
nameComputedRef<string | undefined>
字段名
ComputedRef<string | undefined>
requiredComputedRef<boolean | undefined>
是否必填
ComputedRef<boolean | undefined>
loadingComputedRef<boolean | undefined>
是否加载中
ComputedRef<boolean | undefined>
disabledComputedRef<boolean | undefined>
是否禁用
ComputedRef<boolean | undefined>
invalidComputedRef<boolean | undefined>
是否无效
ComputedRef<boolean | undefined>
dataAttributesComputedRef<Record<string, '' | undefined>>
状态
data-*ComputedRef<Record<string, '' | undefined>>
registerControl(control: { id?: () => string | undefined; element: () => HTMLElement | null | undefined; getValue: () => unknown }) => (() => void) | undefined
注册控件;返回取消注册函数
(control: { id?: () => string | undefined; element: () => HTMLElement | null | undefined; getValue: () => unknown }) => (() => void) | undefined
handleControlFocus() => boolean
控件 focus;已聚焦时返回
false() => boolean
handleControlBlur(detail?: { value: unknown }) => boolean
控件 blur;未聚焦时返回
false(detail?: { value: unknown }) => boolean
Types
FormFieldSharedProps
FieldType
idstring
可选
stringnamestring
可选
stringdisabledboolean
可选
booleanrequiredboolean
可选
booleaninvalidboolean
可选
booleanloadingboolean
可选
booleanFormFieldContextValue
FieldType
fieldIdComputedRef<string | undefined>
字段 id
ComputedRef<string | undefined>
labelIdComputedRef<string | undefined>
label id
ComputedRef<string | undefined>
descriptionIdComputedRef<string | undefined>
description id
ComputedRef<string | undefined>
nameRef<string | undefined>
字段名
Ref<string | undefined>
disabledComputedRef<boolean>
是否禁用
ComputedRef<boolean>
requiredRef<boolean>
是否必填
Ref<boolean>
loadingComputedRef<boolean>
是否加载中
ComputedRef<boolean>
validComputedRef<boolean>
是否有效
ComputedRef<boolean>
invalidComputedRef<boolean>
是否无效
ComputedRef<boolean>
dirtyComputedRef<boolean>
是否已修改
ComputedRef<boolean>
touchedComputedRef<boolean>
是否已触碰
ComputedRef<boolean>
filledComputedRef<boolean>
是否已填写
ComputedRef<boolean>
focusedRef<boolean>
是否聚焦
Ref<boolean>
dataAttributesComputedRef<Record<string, '' | undefined>>
状态
data-*ComputedRef<Record<string, '' | undefined>>
getControlId() => string | undefined
控件 id
() => string | undefined
focusControl() => void
聚焦控件
() => void
resetField() => void
重置字段
() => void
registryControl(control: { id?: () => string | undefined; element: () => HTMLElement | null | undefined; getValue: () => unknown }) => () => void
注册控件;返回取消注册函数
(control: { id?: () => string | undefined; element: () => HTMLElement | null | undefined; getValue: () => unknown }) => () => void
handleControlFocus() => void
控件 focus
() => void
handleControlBlur(detail?: { value: unknown }) => void
控件 blur;可选携带当前值
(detail?: { value: unknown }) => void
Files
src/composables/use-form-field-context.ts