// packages/core/src/components/text-field/text-field-description.tsx
import type { JSX } from "solid-js";
import { createUniqueId, onCleanup, onMount, splitProps } from "solid-js";
import { useTextFieldContext } from "./text-field-context";

export interface TextFieldDescriptionProps extends JSX.HTMLAttributes<HTMLParagraphElement> {
  children?: JSX.Element;
}

/** Helper text for the TextField. Linked via aria-describedby. */
export function TextFieldDescription(props: TextFieldDescriptionProps): JSX.Element {
  const [local, rest] = splitProps(props, ["children"]);
  const ctx = useTextFieldContext();
  const id = createUniqueId();
  onMount(() => ctx.setDescriptionId(id));
  onCleanup(() => ctx.setDescriptionId(undefined));
  return (
    <p id={id} {...rest}>
      {local.children}
    </p>
  );
}
