Skip to content
ui8kit

component / components

Field

Field component

Install

npx ui8kit add field

Pulls in: box, compose, kit-box, kit-label, kit-text, label, text, utils

Preview

Code

Point the CLI at this runtime once, then add the item:

npx ui8kit init --yes --framework svelte --dir src --registry-url https://registry.buildy.tw/svelte --strict-cdn
npx ui8kit add field
components/field.svelte
<!-- Code generated by packages/kit-compose/src/organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT. -->
<!--
  A form field: label, control (children), hint and error text. `htmlFor` ties the label to the id of the
  control.
-->
<script lang="ts" module>
  import type { Snippet } from "svelte";

  export type FieldProps = {
    label: string;
    htmlFor: string;
    hint?: string;
    error?: string;
    class?: string;
    children?: Snippet;
  };
</script>

<script lang="ts">
  import Box from "../compose/Box.svelte";
  import Label from "../compose/Label.svelte";
  import Text from "../compose/Text.svelte";
  import { cn } from "../utils";

  let { label, htmlFor, hint, error, class: className, children }: FieldProps = $props();
</script>

<Box class={cn("flex min-w-0 flex-col gap-2", className)}>
  <Label class="text-xs font-bold text-foreground" for={htmlFor}>
    {label}
  </Label>
  {@render children?.()}
  {#if error}
    <Text class="m-0 text-xs font-semibold text-destructive" role="alert">
      {error}
    </Text>
  {:else if hint}
    <Text class="m-0 text-xs font-medium text-muted-foreground">
      {hint}
    </Text>
  {/if}
</Box>

Svelte registry item (JSON)

Point the CLI at this runtime once, then add the item:

npx ui8kit init --yes --framework react --dir src --registry-url https://registry.buildy.tw/react --strict-cdn
npx ui8kit add field
components/field.tsx
// Code generated by packages/kit-compose/src/organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT.
/**
 * A form field: label, control (children), hint and error text. `htmlFor` ties the label to the id of the
 * control.
 */
import { type ReactNode } from "react";
import { Box, Label, Text } from "../compose";
import { cn } from "../utils";

export type FieldProps = {
  label: string;
  htmlFor: string;
  hint?: string;
  error?: string;
  className?: string;
  children?: ReactNode;
};

export function Field({ label, htmlFor, hint, error, className, children }: FieldProps) {
  return (
    <Box className={cn("flex min-w-0 flex-col gap-2", className)}>
      <Label className="text-xs font-bold text-foreground" htmlFor={htmlFor}>
        {label}
      </Label>
      {children}
      {error ? (
        <Text className="m-0 text-xs font-semibold text-destructive" role="alert">
          {error}
        </Text>
      ) : hint ? (
        <Text className="m-0 text-xs font-medium text-muted-foreground">
          {hint}
        </Text>
      ) : null}
    </Box>
  );
}

React registry item (JSON)

Point the CLI at this runtime once, then add the item:

npx ui8kit init --yes --framework vue --dir src --registry-url https://registry.buildy.tw/vue --strict-cdn
npx ui8kit add field
components/field.vue
<!-- Code generated by packages/kit-compose/src/organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT. -->
<!--
  A form field: label, control (children), hint and error text. `htmlFor` ties the label to the id of the
  control.
-->
<script lang="ts">
export type FieldProps = {
  label: string;
  htmlFor: string;
  hint?: string;
  error?: string;
  class?: string;
};
</script>

<script setup lang="ts">
import Box from "../compose/Box.vue";
import Label from "../compose/Label.vue";
import Text from "../compose/Text.vue";
import { cn } from "../utils";

const { label, htmlFor, hint, error, class: className } = defineProps<FieldProps>();
defineSlots<{ default?(): unknown }>();
</script>

<template>
  <Box :class="cn('flex min-w-0 flex-col gap-2', className)">
    <Label class="text-xs font-bold text-foreground" :for="htmlFor">
      {{ label }}
    </Label>
    <slot />
    <Text v-if="error" class="m-0 text-xs font-semibold text-destructive" role="alert">
      {{ error }}
    </Text>
    <Text v-else-if="hint" class="m-0 text-xs font-medium text-muted-foreground">
      {{ hint }}
    </Text>
  </Box>
</template>

Vue registry item (JSON)

Point the CLI at this runtime once, then add the item:

npx ui8kit init --yes --framework solid --dir src --registry-url https://registry.buildy.tw/solid --strict-cdn
npx ui8kit add field
components/field.tsx
// Code generated by packages/kit-compose/src/organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT.
/**
 * A form field: label, control (children), hint and error text. `htmlFor` ties the label to the id of the
 * control.
 */
import { type JSX } from "solid-js";
import { Box, Label, Text } from "../compose";
import { cn } from "../utils";

export type FieldProps = {
  label: string;
  htmlFor: string;
  hint?: string;
  error?: string;
  class?: string;
  children?: JSX.Element;
};

export function Field({ label, htmlFor, hint, error, class: className, children }: FieldProps) {
  return (
    <Box class={cn("flex min-w-0 flex-col gap-2", className)}>
      <Label class="text-xs font-bold text-foreground" for={htmlFor}>
        {label}
      </Label>
      {children}
      {error ? (
        <Text class="m-0 text-xs font-semibold text-destructive" role="alert">
          {error}
        </Text>
      ) : hint ? (
        <Text class="m-0 text-xs font-medium text-muted-foreground">
          {hint}
        </Text>
      ) : null}
    </Box>
  );
}

Solid registry item (JSON)

Point the CLI at this runtime once, then add the item:

npx ui8kit init --yes --framework templ --dir src --registry-url https://registry.buildy.tw/templ --strict-cdn
npx ui8kit add field
blocks/field.templ
// Code generated by packages/kit-compose/src/server-organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT.
// Source: packages/compose-gen/specs/parts/field.json

package blocks

import (
	"github.com/ui8kit/codegen/generated/templ/ui/box"
	"github.com/ui8kit/codegen/generated/templ/compose"
	"github.com/ui8kit/codegen/generated/templ/ui/label"
	"github.com/ui8kit/codegen/generated/templ/ui/text"
	uiutils "github.com/ui8kit/codegen/generated/templ/orgutils"
)

// FieldProps are the props of Field; the values are the JSON shapes of the TypeScript props.
type FieldProps struct {
	Label any `json:"label"`
	HtmlFor any `json:"htmlFor"`
	Hint any `json:"hint"`
	Error any `json:"error"`
	Class string `json:"className"`
}

// A form field: label, control (children), hint and error text. `htmlFor` ties the label to the id of the control.
templ Field(p FieldProps) {
	{{ vLabel := p.Label }}
	{{ vHtmlFor := p.HtmlFor }}
	{{ vHint := p.Hint }}
	{{ vError := p.Error }}
	@compose.Box(compose.BoxProps{BoxProps: box.BoxProps{Class: uiutils.S(uiutils.Cnv("flex min-w-0 flex-col gap-2", p.Class))}}) {
		@compose.Label(compose.LabelProps{LabelProps: label.LabelProps{Class: "text-xs font-bold text-foreground", For: uiutils.S(vHtmlFor)}}) {
			{ uiutils.Text(vLabel) }
		}
		{ children... }
		if uiutils.T(vError) {
			@compose.Text(compose.TextProps{TextProps: text.TextProps{Class: "m-0 text-xs font-semibold text-destructive", Attrs: uiutils.Attrs("role", "alert")}}) {
				{ uiutils.Text(vError) }
			}
		} else if uiutils.T(vHint) {
			@compose.Text(compose.TextProps{TextProps: text.TextProps{Class: "m-0 text-xs font-medium text-muted-foreground"}}) {
				{ uiutils.Text(vHint) }
			}
		}
	}
}

templ registry item (JSON)

Point the CLI at this runtime once, then add the item:

npx ui8kit init --yes --framework twig --dir src --registry-url https://registry.buildy.tw/twig --strict-cdn
npx ui8kit add field
blocks/field.html.twig
{#- Code generated by packages/kit-compose/src/server-organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT.
    Source: packages/compose-gen/specs/parts/field.json
    A form field: label, control (children), hint and error text. `htmlFor` ties the label to the id of the control.
    Props (p): label, htmlFor, hint, error, className; children: markup. -#}
{%- set v_label = (p.label ?? null) -%}
{%- set v_htmlFor = (p.htmlFor ?? null) -%}
{%- set v_hint = (p.hint ?? null) -%}
{%- set v_error = (p.error ?? null) -%}
{%- set c_4 -%}
  {%- set c_1 -%}
    {{ og_text(v_label) }}
  {%- endset -%}
  {{ include('compose/Label.html.twig', {'class': 'text-xs font-bold text-foreground', 'htmlFor': og_s(v_htmlFor), 'children': c_1}, with_context = false) }}
  {{ og_slot((children ?? null))|raw }}
  {%- if og_t(v_error) %}
    {%- set c_2 -%}
      {{ og_text(v_error) }}
    {%- endset -%}
    {{ include('compose/Text.html.twig', {'class': 'm-0 text-xs font-semibold text-destructive', 'attrs': og_attrs(['role', 'alert']), 'children': c_2}, with_context = false) }}
  {%- elseif og_t(v_hint) %}
    {%- set c_3 -%}
      {{ og_text(v_hint) }}
    {%- endset -%}
    {{ include('compose/Text.html.twig', {'class': 'm-0 text-xs font-medium text-muted-foreground', 'children': c_3}, with_context = false) }}
  {%- endif %}
{%- endset -%}
{{ include('compose/Box.html.twig', {'class': og_s(og_cnv(['flex min-w-0 flex-col gap-2', (p.className ?? '')])), 'children': c_4}, with_context = false) }}

Twig registry item (JSON)

Point the CLI at this runtime once, then add the item:

npx ui8kit init --yes --framework latte --dir src --registry-url https://registry.buildy.tw/latte --strict-cdn
npx ui8kit add field
blocks/field.latte
{* Code generated by packages/kit-compose/src/server-organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT.
    Source: packages/compose-gen/specs/parts/field.json
    A form field: label, control (children), hint and error text. `htmlFor` ties the label to the id of the control.
    Props (p): label, htmlFor, hint, error, className; children: markup. *}
{var $v_label = ($p['label'] ?? null)}
{var $v_htmlFor = ($p['htmlFor'] ?? null)}
{var $v_hint = ($p['hint'] ?? null)}
{var $v_error = ($p['error'] ?? null)}
{capture $c_4}
  {capture $c_1}
    {=og_text($v_label)}
  {/capture}
  {=og_include('compose/Label.latte', ['class' => 'text-xs font-bold text-foreground', 'htmlFor' => og_s($v_htmlFor), 'children' => (string) $c_1])|noescape}
  {=og_slot(($children ?? null))|noescape}
  {if og_t($v_error)}
    {capture $c_2}
      {=og_text($v_error)}
    {/capture}
    {=og_include('compose/Text.latte', ['class' => 'm-0 text-xs font-semibold text-destructive', 'attrs' => og_attrs(['role', 'alert']), 'children' => (string) $c_2])|noescape}
  {elseif og_t($v_hint)}
    {capture $c_3}
      {=og_text($v_hint)}
    {/capture}
    {=og_include('compose/Text.latte', ['class' => 'm-0 text-xs font-medium text-muted-foreground', 'children' => (string) $c_3])|noescape}
  {/if}
{/capture}
{=og_include('compose/Box.latte', ['class' => og_s(og_cnv(['flex min-w-0 flex-col gap-2', ($p['className'] ?? '')])), 'children' => (string) $c_4])|noescape}

Latte registry item (JSON)

Point the CLI at this runtime once, then add the item:

npx ui8kit init --yes --framework jinja2 --dir src --registry-url https://registry.buildy.tw/jinja2 --strict-cdn
npx ui8kit add field
blocks/field.html.j2
{#- Code generated by packages/kit-compose/src/server-organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT.
    Source: packages/compose-gen/specs/parts/field.json
    A form field: label, control (children), hint and error text. `htmlFor` ties the label to the id of the control.
    Props (p): label, htmlFor, hint, error, className; children: markup. -#}
{%- set v_label = og_def(p['label'], none) -%}
{%- set v_htmlFor = og_def(p['htmlFor'], none) -%}
{%- set v_hint = og_def(p['hint'], none) -%}
{%- set v_error = og_def(p['error'], none) -%}
{%- set c_4 -%}
  {%- set c_1 -%}
    {{ og_text(v_label) }}
  {%- endset -%}
  {{ og_include('compose/label.html.j2', {'p': {'Class': 'text-xs font-bold text-foreground', 'For': og_s(v_htmlFor)}, 'children': c_1}) }}
  {{ og_slot(og_def(children, none))|safe }}
  {%- if og_t(v_error) %}
    {%- set c_2 -%}
      {{ og_text(v_error) }}
    {%- endset -%}
    {{ og_include('compose/text.html.j2', {'p': {'Class': 'm-0 text-xs font-semibold text-destructive', 'Attrs': og_attrs(['role', 'alert'])}, 'children': c_2}) }}
  {%- elif og_t(v_hint) %}
    {%- set c_3 -%}
      {{ og_text(v_hint) }}
    {%- endset -%}
    {{ og_include('compose/text.html.j2', {'p': {'Class': 'm-0 text-xs font-medium text-muted-foreground'}, 'children': c_3}) }}
  {%- endif %}
{%- endset -%}
{{ og_include('compose/box.html.j2', {'p': {'Class': og_s(og_cnv(['flex min-w-0 flex-col gap-2', og_def(p['className'], '')]))}, 'children': c_4}) }}

Jinja2 registry item (JSON)

Props

Names and types come from the xgen specs of the source. Standard HTML attributes (id, role, tabIndex, aria-*) pass through to the root element; the class prop is className in React.

A form field: label, control (children), hint and error text. `htmlFor` ties the label to the id of the control.

Props of Field
PropTypeDefault Description
label (required)string-
htmlFor (required)string-id of the control
hint string-
error string-when set it replaces the hint and is announced (role alert)

Used in