// A form field and its parts: a label, a control, a description, and an error, laid out // vertically, side by side, or (`orientation="responsive"`) side by side on wider screens. // FieldSet and FieldLegend group fields under a heading, and FieldGroup spaces them out. // // // Email // // We'll never share it. // import type { Child, Html } from '../lib/jsx/jsx-runtime.ts' import css from './field.css' with { type: 'text' } export const FIELD_CSS = css type Props = Record & { children?: Child } const part = (tag: string, slot: string, defaults: Record = {}) => (props: Props): Html => { let { children, ...attributes } = props let Tag = tag return ( {children} ) } export const FieldSet = part('fieldset', 'field-set') export const FieldGroup = part('div', 'field-group') export const FieldContent = part('div', 'field-content') export const FieldLabel = part('label', 'field-label') export const FieldTitle = part('div', 'field-title') export const FieldDescription = part('p', 'field-description') // `variant="label"` makes the legend the size of a label, for a set inside a field. export function FieldLegend(props: Props & { variant?: 'legend' | 'label' | undefined }): Html { let { variant = 'legend', children, ...attributes } = props return ( {children} ) } // Set `data-invalid` on a field whose value is wrong, to color its label and description. export function Field( props: Props & { orientation?: 'vertical' | 'horizontal' | 'responsive' | undefined }, ): Html { let { orientation = 'vertical', children, ...attributes } = props return (
{children}
) } // A line between fields, with optional text in the middle, like "Or". export function FieldSeparator(props: Props): Html { let { children, ...attributes } = props return (
{children != null ? {children} : null}
) } // The field's error: its children, or a list of `errors` (one shows on its own). Datastar's // `data-show` or `data-text` can show and fill it as the value changes. export function FieldError(props: Props & { errors?: string[] | undefined }): Html { let { errors, children, ...attributes } = props let content: Child = children if (content == null && errors?.length) { content = errors.length === 1 ? ( errors[0] ) : ( ) } return (
{content}
) }