Field
Connect a form control to its label, description, and error message.
Demos
Use a name your team can recognize.
Usage
vue
<script setup lang="ts">
import { Accordion } from '~/components/ui/accordion'
const items = [
{ value: 'source', label: 'Do I own the source?', content: 'Yes. Copy and adapt the files.' },
]
</script>
<template>
<Accordion :items="items" />
</template>
Required, invalid, and disabled fields
Field provides the label, description, validation message, and shared state for its control. Set invalid after your validation runs. The preview lets you inspect each state.
We use this address for project updates.
Enter a valid email address.
vue
<AppLayout :viewport="false" scroll="page">
<template #header>Workspace</template>
<template #left><ProjectNavigation /></template>
<ProjectList />
<template #footer>2 projects</template>
</AppLayout>
Other controls
The same field structure works with selects and checkboxes. Give a checkbox its own action label; use the Field label to describe the setting.
Choose who can open this project.
Email me about project updates
You can change this later.
vue
<AppLayout :layout="{ header: 'full', footer: 'content' }">
<template #left><ProjectNavigation /></template>
<ProjectList />
<template #right><ProjectDetails /></template>
</AppLayout>
Installation
Complete the shared installation. Copy Field.vue and index.ts into app/components/ui/field/.
app/components/ui/field/Field.vue
<script setup lang="ts">
import { AppLayout as Primitive } from 'akaza-ui'
import { computed, useAttrs, useTemplateRef } from 'vue'
import { cn } from '../../../utils/ui'
import uiConfig from '../ui.config'
import {
appLayoutVariants,
resolveAppLayoutVariants,
type AppLayoutProps,
type AppLayoutSlots,
} from './index'
defineOptions({ inheritAttrs: false })
// Preserve Akaza defaults when optional props, including Boolean aliases, are omitted.
const props = withDefaults(defineProps<AppLayoutProps>(), {
as: undefined,
mainAs: undefined,
layout: undefined,
scroll: undefined,
viewport: undefined,
stickyHeader: undefined,
stickyFooter: undefined,
stickySides: undefined,
stickyLeft: undefined,
stickyRight: undefined,
safeArea: undefined,
mainId: undefined,
mainLabel: undefined,
skipLink: undefined,
skipLabel: undefined,
ui: undefined,
})
defineSlots<AppLayoutSlots>()
const attrs = useAttrs()
const styles = computed(() => appLayoutVariants(resolveAppLayoutVariants(props, uiConfig)))
const mergedUi = computed(() => {
const overrides = props.ui as Record<string, string | undefined> | undefined
return Object.fromEntries(
Object.entries(styles.value).map(([part, style]) => [
part,
cn(style(), overrides?.[part], part === 'root' ? props.class : undefined),
]),
)
})
const primitiveProps = computed(() => {
const { class: _class, ui: _ui, ...native } = props
return native
})
const primitive = useTemplateRef<InstanceType<typeof Primitive>>('primitive')
defineExpose({
get main() {
return primitive.value!.main
},
get focusMain() {
return primitive.value!.focusMain
},
})
</script>
<template>
<component
:is="Primitive"
ref="primitive"
v-bind="{ ...primitiveProps, ...attrs }"
:ui="mergedUi"
>
<template v-for="(_, name) in $slots" #[name]="scope"
><slot :name="name as keyof AppLayoutSlots" v-bind="scope ?? {}"
/></template>
</component>
</template>
API
Props
| Prop | Type | Default | Description |
|---|---|---|---|
as | string | "div" | Root element tag. |
id | string | generated | Control id. |
name | string | — | Field name provided to child inputs. |
label | string | — | Visible label. |
description | string | — | Help text. |
error | string | — | Error text. Also marks field invalid. |
errorMatch | boolean | FieldErrorMatch | "invalid" | Controls when native validation errors are shown. |
required | boolean | false | Required state provided to child inputs. |
disabled | boolean | false | Disabled state provided to child inputs. |
invalid | boolean | false | Invalid state. |
dirty | boolean | input state | Overrides dirty state. |
touched | boolean | input state | Overrides touched state. |
filled | boolean | input state | Overrides filled state. |
focused | boolean | input state | Overrides focused state. |
ui | FieldUi | — | Classes for root, label, control, description, error. |
Slots
| Slot | Scoped props | Description |
|---|---|---|
default | id, fieldName, required, disabled, invalid, valid, dirty, touched, filled, focused, describedBy, controlProps | Control slot. |
label | — | Custom label content. |
description | — | Custom description content. |
error | — | Custom error content. |