feat(ui): refine Fieldset API
This commit is contained in:
@@ -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}
|
||||||
|
|||||||
@@ -1,25 +1,42 @@
|
|||||||
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}
|
||||||
>
|
>
|
||||||
|
{props.legend && (
|
||||||
<legend>
|
<legend>
|
||||||
<Typography
|
<Typography
|
||||||
hierarchy="label"
|
hierarchy="label"
|
||||||
@@ -33,9 +50,8 @@ export const Fieldset = (props: FieldsetProps) => {
|
|||||||
{props.legend}
|
{props.legend}
|
||||||
</Typography>
|
</Typography>
|
||||||
</legend>
|
</legend>
|
||||||
<div class="fields">
|
)}
|
||||||
{props.fields({ ...props, orientation: orientation() })}
|
<div class="fields">{props.children(fieldProps)}</div>
|
||||||
</div>
|
|
||||||
{props.error && (
|
{props.error && (
|
||||||
<div class="error" role="alert">
|
<div class="error" role="alert">
|
||||||
<Typography
|
<Typography
|
||||||
|
|||||||
Reference in New Issue
Block a user