Skip to content

The Open Standard for Generative UI

Table (#9)

Ankit Dasc4f4680934e9
diff --git a/frontend-sdk/packages/react-ui/src/components/RadioGroup/RadioGroup.tsx b/frontend-sdk/packages/react-ui/src/components/RadioGroup/RadioGroup.tsx
index b42fb6d7b..6c6c9c8b3 100644
--- a/frontend-sdk/packages/react-ui/src/components/RadioGroup/RadioGroup.tsx
+++ b/frontend-sdk/packages/react-ui/src/components/RadioGroup/RadioGroup.tsx
@@ -1,5 +1,6 @@
import * as Radio from "@radix-ui/react-radio-group";
import clsx from "clsx";
+import { forwardRef } from "react";
import { RadioItemProps } from "../RadioItem";
interface RadioGroupProps extends Radio.RadioGroupProps {
@@ -9,10 +10,11 @@ interface RadioGroupProps extends Radio.RadioGroupProps {
style?: React.CSSProperties;
}
-const RadioGroup = (props: RadioGroupProps) => {
+const RadioGroup = forwardRef<HTMLDivElement, RadioGroupProps>((props, ref) => {
const { children, className, style, variant = "clear", ...rest } = props;
return (
<Radio.Root
+ ref={ref}
className={clsx("crayon-radio-group", `crayon-radio-group-${variant}`, className)}
style={style}
{...rest}
@@ -20,6 +22,8 @@ const RadioGroup = (props: RadioGroupProps) => {
{children}
</Radio.Root>
);
-};
+});
+
+RadioGroup.displayName = "RadioGroup";
export { RadioGroup, type RadioGroupProps };
diff --git a/frontend-sdk/packages/react-ui/src/components/RadioItem/RadioItem.tsx b/frontend-sdk/packages/react-ui/src/components/RadioItem/RadioItem.tsx
index 82407925c..bb083769d 100644
--- a/frontend-sdk/packages/react-ui/src/components/RadioItem/RadioItem.tsx
+++ b/frontend-sdk/packages/react-ui/src/components/RadioItem/RadioItem.tsx
@@ -1,6 +1,6 @@
import * as Radio from "@radix-ui/react-radio-group";
import clsx from "clsx";
-import React, { useId } from "react";
+import React, { forwardRef, useId } from "react";
import { Label } from "../FormControl/Label";
export interface RadioItemProps {
@@ -12,12 +12,13 @@ export interface RadioItemProps {
value: string;
}
-const RadioItem = (props: RadioItemProps) => {
+const RadioItem = forwardRef<HTMLButtonElement, RadioItemProps>((props, ref) => {
const { label, className, style, disabled, required, value } = props;
const id = useId();
return (
<div className="crayon-radio-item-container">
<Radio.Item
+ ref={ref}
className={clsx("crayon-radio-item-root", className)}
value={value}
disabled={disabled}
@@ -56,6 +57,8 @@ const RadioItem = (props: RadioItemProps) => {
)}
</div>
);
-};
+});
+
+RadioItem.displayName = "RadioItem";
export { RadioItem };
diff --git a/frontend-sdk/packages/react-ui/src/components/SwitchGroup/SwitchGroup.tsx b/frontend-sdk/packages/react-ui/src/components/SwitchGroup/SwitchGroup.tsx
index 2ad3eb8cd..5a127192c 100644
--- a/frontend-sdk/packages/react-ui/src/components/SwitchGroup/SwitchGroup.tsx
+++ b/frontend-sdk/packages/react-ui/src/components/SwitchGroup/SwitchGroup.tsx
@@ -1,5 +1,5 @@
import clsx from "clsx";
-import React from "react";
+import React, { forwardRef } from "react";
import { SwitchItemProps } from "../SwitchItem";
export interface SwitchGroupProps {
@@ -9,16 +9,19 @@ export interface SwitchGroupProps {
variant?: "clear" | "card" | "sunk";
}
-const SwitchGroup = (props: SwitchGroupProps) => {
+const SwitchGroup = forwardRef<HTMLDivElement, SwitchGroupProps>((props, ref) => {
const { children, className, style, variant = "clear" } = props;
return (
<div
+ ref={ref}
className={clsx("crayon-switch-group", `crayon-switch-group-${variant}`, className)}
style={style}
>
{children}
</div>
);
-};
+});
+
+SwitchGroup.displayName = "SwitchGroup";
export { SwitchGroup };
diff --git a/frontend-sdk/packages/react-ui/src/components/Table/Table.tsx b/frontend-sdk/packages/react-ui/src/components/Table/Table.tsx
new file mode 100644
index 000000000..230949bfa
--- /dev/null
+++ b/frontend-sdk/packages/react-ui/src/components/Table/Table.tsx
@@ -0,0 +1,76 @@
+import clsx from "clsx";
+import * as React from "react";
+
+const Table = React.forwardRef<HTMLTableElement, React.HTMLAttributes<HTMLTableElement>>(
+ ({ className, ...props }, ref) => (
+ <div className="crayon-table-container">
+ <table ref={ref} className={clsx("crayon-table", className)} {...props} />
+ </div>
+ ),
+);
+Table.displayName = "Table";
+
+const TableHeader = React.forwardRef<
+ HTMLTableSectionElement,
+ React.HTMLAttributes<HTMLTableSectionElement>
+>(({ className, ...props }, ref) => (
+ <thead ref={ref} className={clsx("crayon-table-header", className)} {...props} />
+));
+TableHeader.displayName = "TableHeader";
+
+const TableBody = React.forwardRef<
+ HTMLTableSectionElement,
+ React.HTMLAttributes<HTMLTableSectionElement>
+>(({ className, ...props }, ref) => (
+ <tbody ref={ref} className={clsx("crayon-table-body", className)} {...props} />
+));
+TableBody.displayName = "TableBody";
+
+const TableFooter = React.forwardRef<
+ HTMLTableSectionElement,
+ React.HTMLAttributes<HTMLTableSectionElement>
+>(({ className, ...props }, ref) => (
+ <tfoot ref={ref} className={clsx("crayon-table-footer", className)} {...props} />
+));
+TableFooter.displayName = "TableFooter";
+
+const TableRow = React.forwardRef<HTMLTableRowElement, React.HTMLAttributes<HTMLTableRowElement>>(
+ ({ className, ...props }, ref) => (
+ <tr ref={ref} className={clsx("crayon-table-row", className)} {...props} />
+ ),
+);
+TableRow.displayName = "TableRow";
+
+export interface TableHeadProps extends React.ThHTMLAttributes<HTMLTableCellElement> {
+ icon?: React.ReactNode;
+}
+
+const TableHead = React.forwardRef<HTMLTableCellElement, TableHeadProps>(
+ ({ className, children, icon, ...props }, ref) => (
+ <th ref={ref} className={clsx("crayon-table-head", className)} {...props}>
+ <div className="crayon-table-head-content">
+ {icon && <div className="crayon-table-head-icon">{icon}</div>}
+ <div className="crayon-table-head-label">{children}</div>
+ </div>
+ </th>
+ ),
+);
+TableHead.displayName = "TableHead";
+
+const TableCell = React.forwardRef<
+ HTMLTableCellElement,
+ React.TdHTMLAttributes<HTMLTableCellElement>
+>(({ className, ...props }, ref) => (
+ <td ref={ref} className={clsx("crayon-table-cell", className)} {...props} />
+));
+TableCell.displayName = "TableCell";
+
+const TableCaption = React.forwardRef<
+ HTMLTableCaptionElement,
+ React.HTMLAttributes<HTMLTableCaptionElement>
+>(({ className, ...props }, ref) => (
+ <caption ref={ref} className={clsx("crayon-table-caption", className)} {...props} />
+));
+TableCaption.displayName = "TableCaption";
+
+export { Table, TableBody, TableCaption, TableCell, TableFooter, TableHead, TableHeader, TableRow };
diff --git a/frontend-sdk/packages/react-ui/src/components/Table/index.ts b/frontend-sdk/packages/react-ui/src/components/Table/index.ts
new file mode 100644
index 000000000..e40efa476
--- /dev/null
+++ b/frontend-sdk/packages/react-ui/src/components/Table/index.ts
@@ -0,0 +1 @@
+export * from "./Table";
diff --git a/frontend-sdk/packages/react-ui/src/components/Table/stories/Table.stories.tsx b/frontend-sdk/packages/react-ui/src/components/Table/stories/Table.stories.tsx
new file mode 100644
index 000000000..faffddea9
--- /dev/null
+++ b/frontend-sdk/packages/react-ui/src/components/Table/stories/Table.stories.tsx
@@ -0,0 +1,122 @@
+import type { Meta, StoryObj } from "@storybook/react";
+import { CircleCheck, CreditCard, DollarSign, Package, Stamp } from "lucide-react";
+import {
+ Table,
+ TableBody,
+ TableCaption,
+ TableCell,
+ TableFooter,
+ TableHead,
+ TableHeader,
+ TableRow,
+} from "../Table";
+import "../table.scss";
+
+const meta: Meta<typeof Table> = {
+ title: "Components/Table",
+ component: Table,
+ tags: ["autodocs"],
+};
+
+export default meta;
+type Story = StoryObj<typeof Table>;
+
+// Basic Table Story
+export const Basic: Story = {
+ render: () => (
+ <Table>
+ <TableCaption>A list of your recent invoices.</TableCaption>
+ <TableHeader>
+ <TableRow>
+ <TableHead icon={<Stamp size={14} />}>Invoice</TableHead>
+ <TableHead icon={<CircleCheck size={14} />}>Status</TableHead>
+ <TableHead icon={<CreditCard size={14} />}>Method</TableHead>
+ <TableHead icon={<DollarSign size={14} />}>Amount</TableHead>
+ </TableRow>
+ </TableHeader>
+ <TableBody>
+ <TableRow>
+ <TableCell>INV001</TableCell>
+ <TableCell>Paid</TableCell>
+ <TableCell>Credit Card</TableCell>
+ <TableCell>$250.00</TableCell>
+ </TableRow>
+ <TableRow>
+ <TableCell>INV002</TableCell>
+ <TableCell>Pending</TableCell>
+ <TableCell>PayPal</TableCell>
+ <TableCell>$150.00</TableCell>
+ </TableRow>
+ <TableRow>
+ <TableCell>INV003</TableCell>
+ <TableCell>Unpaid</TableCell>
+ <TableCell>Bank Transfer</TableCell>
+ <TableCell>$350.00</TableCell>
+ </TableRow>
+ </TableBody>
+ </Table>
+ ),
+};
+
+// Table with Footer Story
+export const WithFooter: Story = {
+ render: () => (
+ <Table>
+ <TableHeader>
+ <TableRow>
+ <TableHead icon={<Package size={14} />}>Product</TableHead>
+ <TableHead icon={<Package size={14} />}>Quantity</TableHead>
+ <TableHead icon={<Package size={14} />}>Price</TableHead>
+ <TableHead icon={<Package size={14} />}>Total</TableHead>
+ </TableRow>
+ </TableHeader>
+ <TableBody>
+ <TableRow>
+ <TableCell>Product A</TableCell>
+ <TableCell>2</TableCell>
+ <TableCell>$50.00</TableCell>
+ <TableCell>$100.00</TableCell>
+ </TableRow>
+ <TableRow>
+ <TableCell>Product B</TableCell>
+ <TableCell>1</TableCell>
+ <TableCell>$75.00</TableCell>
+ <TableCell>$75.00</TableCell>
+ </TableRow>
+ </TableBody>
+ <TableFooter>
+ <TableRow>
+ <TableCell colSpan={3}>Total</TableCell>
+ <TableCell>$175.00</TableCell>
+ </TableRow>
+ </TableFooter>
+ </Table>
+ ),
+};
+
+// Table with Custom Styling Story
+export const CustomStyling: Story = {
+ render: () => (
+ <Table className="border rounded-lg">
+ <TableHeader>
+ <TableRow className="bg-gray-100">
+ <TableHead className="font-bold">Name</TableHead>
+ <TableHead className="font-bold">Email</TableHead>
+ <TableHead className="font-bold">Role</TableHead>
+ </TableRow>
+ </TableHeader>
+ <TableBody>
+ <TableRow>
+ <TableCell className="font-medium">John Doe</TableCell>
+ <TableCell>[email protected]</TableCell>
+ <TableCell>Admin</TableCell>
+ </TableRow>
+ <TableRow>
+ <TableCell className="font-medium">Jane Smith</TableCell>
+ <TableCell>[email protected]</TableCell>
+ <TableCell>User</TableCell>
+ </TableRow>
+ </TableBody>
+ </Table>
+ ),
+};
diff --git a/frontend-sdk/packages/react-ui/src/components/Table/table.scss b/frontend-sdk/packages/react-ui/src/components/Table/table.scss
new file mode 100644
index 000000000..6b4c408d9
--- /dev/null
+++ b/frontend-sdk/packages/react-ui/src/components/Table/table.scss
@@ -0,0 +1,77 @@
+@use "sass:map";
+@use "../../cssUtils" as cssUtils;
+
+.crayon-table-container {
+ position: relative;
+ width: 100%;
+ overflow: auto;
+}
+
+.crayon-table {
+ @include cssUtils.typography(label, heavy);
+ width: 100%;
+ caption-side: bottom;
+ border-collapse: collapse;
+ border: 1px solid map.get(cssUtils.$colors, "strokes", "default");
+}
+
+.crayon-table-header {
+ & tr {
+ border-bottom: 1px solid map.get(cssUtils.$colors, "strokes", "default");
+ border-top: 1px solid map.get(cssUtils.$colors, "strokes", "default");
+ background-color: map.get(cssUtils.$colors, "fills", "sunk");
+ }
+
+ & tr td {
+ border-left: 1px solid map.get(cssUtils.$colors, "strokes", "default");
+ }
+}
+
+.crayon-table-body {
+ & tr {
+ &:hover {
+ background-color: map.get(cssUtils.$colors, "fills", "hover");
+ }
+ }
+
+ & tr:last-child {
+ border-bottom: 1px solid map.get(cssUtils.$colors, "strokes", "default");
+ }
+}
+
+.crayon-table-footer {
+ border-top: 1px solid map.get(cssUtils.$colors, "strokes", "default");
+
+ & > tr:last-child {
+ border-bottom: 0;
+ }
+}
+
+.crayon-table-cell,
+.crayon-table-head {
+ padding: map.get(cssUtils.$spacing, "m");
+ border: 1px solid map.get(cssUtils.$colors, "strokes", "default");
+}
+
+.crayon-table-head {
+ background-color: map.get(cssUtils.$colors, "fills", "sunk");
+ & .crayon-table-head-label {
+ @include cssUtils.typography(label, heavy);
+ color: map.get(cssUtils.$colors, "text", "primary");
+ display: inline-flex;
+ flex-grow: 1;
+ justify-content: center;
+ }
+
+ & .crayon-table-head-content {
+ display: flex;
+ align-items: center;
+ gap: map.get(cssUtils.$spacing, "2xs");
+ }
+}
+
+.crayon-table-caption {
+ @include cssUtils.typography(label, heavy);
+ color: map.get(cssUtils.$colors, "text", "primary");
+ margin-top: map.get(cssUtils.$spacing, "m");
+}