Billing details

Installation

  1. Set up the project

    Once per project: the packages, Tailwind CSS, the theme tokens and the cn helper. Follow Installation.

  2. Copy the component into your project

    The kit owns behaviour, ARIA and data attributes. This file is the look, written with Tailwind classes, and it is yours to change.

    src/ui/fieldset.ts
    import type { Component } from "@effect/ui"
    import type { Html } from "@effect/ui/html"
    import type { Fieldset } from "@effect/ui-kit"
    import { cn } from "#lib/cn"
    
    /** What the kit's `Fieldset` hands its `view` closure. */
    export type FieldsetParts = Component.Headless.Parts<typeof Fieldset>
    
    interface ClassOption {
      readonly class?: string | undefined
    }
    
    const variants = {
      default: "border-0 p-0",
      card: "rounded-lg border border-border bg-card p-5"
    } as const
    
    export type FieldsetVariant = keyof typeof variants
    
    /** The `<fieldset>`: a column of fields under one legend. `card` draws a border around them. */
    export const FieldsetRoot = <Msg>(
      h: Html.H<Msg>,
      fieldset: FieldsetParts,
      options: ClassOption & { readonly variant?: FieldsetVariant | undefined } = {}
    ) =>
      fieldset.root(
        h.fieldset.class(cn("m-0 flex min-w-0 flex-col gap-4", variants[options.variant ?? "default"], options.class))
      )
    
    /** The fieldset's name. Always draw it: the root points at it. */
    export const FieldsetLegend = <Msg>(h: Html.H<Msg>, fieldset: FieldsetParts, options: ClassOption = {}) =>
      fieldset.legend(h.div.class(cn("text-sm font-semibold data-[disabled]:opacity-50", options.class)))
    
    /** A sentence under the legend. Point the root's `aria-describedby` at its id. */
    export const FieldsetDescription = <Msg>(h: Html.H<Msg>, options: ClassOption = {}) =>
      h.p.class(cn("m-0 mt-1 text-[13px] text-muted-foreground", options.class))
    
  3. Import it

    import { FieldsetRoot, FieldsetLegend, FieldsetDescription } from "#ui/fieldset"
    

Usage

Basic.ts
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Field, Fieldset } from "@effect/ui-kit"
import { Schema } from "effect"
import { FieldLabel, FieldRoot } from "#ui/field"
import { FieldsetLegend, FieldsetRoot } from "#ui/fieldset"
import { Input } from "#ui/input"

const fields = [
  { name: "company", label: "Company", placeholder: "Effectful Technologies" },
  { name: "taxId", label: "Tax ID", placeholder: "IT 0123456789" }
] as const

const FieldsetBasicMessage = UiSchema.TaggedEnum({
  UpdatedField: { name: Schema.Literals(["company", "taxId"]), value: Schema.String }
})

export const FieldsetBasic = Html.component({
  name: "FieldsetBasic",
  model: Schema.Struct({ company: Schema.String, taxId: Schema.String }),
  message: FieldsetBasicMessage
}).make({
  init: () => ({ company: "", taxId: "" }),
  update: {
    UpdatedField: ($, { name, value }) => $.evolve({ [name]: value })
  },
  view: (h, model) =>
    h.div.class("flex w-full max-w-90 flex-col gap-3.5").children(
      Fieldset(h, {
        view: (fieldset) =>
          FieldsetRoot(h, fieldset).children(
            FieldsetLegend(h, fieldset).children("Billing details"),
            fields.map(({ label, name, placeholder }) =>
              Field(h, {
                key: name,
                view: (field) =>
                  FieldRoot(h, field).children(
                    FieldLabel(h, field).children(label),
                    Input(h, {
                      element: field.control(h.input),
                      props: { value: model[name] },
                      onUpdate: ({ value }) => FieldsetBasicMessage.UpdatedField({ name, value })
                    }).attrs({ name, placeholder })
                  )
              })
            )
          )
      })
    )
})

Examples

Disabled

One prop disables every field inside. Each Field reads the fieldset, so nothing is passed to it.

Profile

Card with a description

The card variant, a description under the legend and the fields in a grid.

Invoice recipient

Printed at the top of every invoice we send you.

Nested

A fieldset inside another is disabled while the outer one is.

Shipping address
Delivery contact

Controls without a field

A slider and a number field take disabled from fieldset.disabled.

Playback
60%

Anatomy

The parts the view closure receives. Apply each one to an element of your own.

PartDescription
rootThe <fieldset>. Its aria-labelledby points at the legend.
legendAny element, usually a <div>. Always draw it.
disabledA boolean, for a control inside that is drawn without a Field.

API reference

Props

PropTypeDescription
disabledbooleanSets the native disabled on the <fieldset>, which disables every control inside, and disables each Field inside.

Data attributes

AttributeDescription
data-disabledOn the root and the legend, while the fieldset or one around it is disabled.

The components this page's file and examples import.

  • Button: A native button that can stay focusable while disabled.
  • Field: Groups a control with its label, description and error, and reports whether it is invalid, touched or focused on each of them.
  • Input: A native text input whose value lives in your Model.
  • NumberField: A controlled input for a number, with stepper buttons, locale formatting and an area to drag across.
  • Slider: A controlled slider with one thumb or several, dragged or moved with the keyboard.

effectcn is built with Effect UI and @effect/ui-kit. Not affiliated with Effectful Technologies.