feat(ui): refine Fieldset API

This commit is contained in:
Brian McGee
2025-07-07 10:51:43 +01:00
parent dd7477a818
commit f04089a406
2 changed files with 41 additions and 25 deletions

View File

@@ -40,7 +40,7 @@ export type Story = StoryObj<typeof meta>;
export const Default: Story = { export const Default: Story = {
args: { args: {
legend: "Signup", legend: "Signup",
fields: (props: FieldProps) => ( children: (props: FieldProps) => (
<> <>
<TextInput <TextInput
{...props} {...props}
@@ -90,7 +90,7 @@ export const Error: Story = {
args: { args: {
legend: "Signup", legend: "Signup",
error: "You must enter a First Name", error: "You must enter a First Name",
fields: (props: FieldProps) => ( children: (props: FieldProps) => (
<> <>
<TextInput <TextInput
{...props} {...props}

View File

@@ -1,41 +1,57 @@
import "./Fieldset.css"; import "./Fieldset.css";
import { JSX } from "solid-js"; import { JSX, splitProps } from "solid-js";
import cx from "classnames"; import cx from "classnames";
import { Typography } from "@/src/components/v2/Typography/Typography"; import { Typography } from "@/src/components/v2/Typography/Typography";
import { FieldProps } from "./Field"; import { FieldProps } from "./Field";
export interface FieldsetProps extends FieldProps { export type FieldsetFieldProps = Pick<
legend: string; FieldProps,
disabled: boolean; "orientation" | "inverted"
> & {
error?: string; error?: string;
fields: (props: FieldProps) => JSX.Element; disabled?: boolean;
};
export interface FieldsetProps
extends Pick<FieldProps, "orientation" | "inverted"> {
legend?: string;
disabled?: boolean;
error?: string;
children: (props: FieldsetFieldProps) => JSX.Element;
} }
export const Fieldset = (props: FieldsetProps) => { export const Fieldset = (props: FieldsetProps) => {
const orientation = () => props.orientation || "vertical"; const orientation = () => props.orientation || "vertical";
const [fieldProps] = splitProps(props, [
"orientation",
"inverted",
"disabled",
"error",
]);
return ( return (
<fieldset <fieldset
role="group" role="group"
class={cx(orientation(), { inverted: props.inverted })} class={cx({ inverted: props.inverted })}
disabled={props.disabled} disabled={props.disabled || false}
> >
<legend> {props.legend && (
<Typography <legend>
hierarchy="label" <Typography
family="mono" hierarchy="label"
size="default" family="mono"
weight="normal" size="default"
color="tertiary" weight="normal"
transform="uppercase" color="tertiary"
inverted={props.inverted} transform="uppercase"
> inverted={props.inverted}
{props.legend} >
</Typography> {props.legend}
</legend> </Typography>
<div class="fields"> </legend>
{props.fields({ ...props, orientation: orientation() })} )}
</div> <div class="fields">{props.children(fieldProps)}</div>
{props.error && ( {props.error && (
<div class="error" role="alert"> <div class="error" role="alert">
<Typography <Typography