AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
feat(frontend): add Table component with TableField renderer for tabular data input (#11751)
Abhimanyu Yadav6b6648b29066
diff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/nodes/helpers.ts b/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/nodes/helpers.tsindex 39384485f5..46032a67ea 100644--- a/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/nodes/helpers.ts+++ b/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/nodes/helpers.ts@@ -89,6 +89,18 @@ export function extractOptions(// get display type and color for schema types [need for type display next to field name]export const getTypeDisplayInfo = (schema: any) => {+ if (+ schema?.type === "array" &&+ "format" in schema &&+ schema.format === "table"+ ) {+ return {+ displayType: "table",+ colorClass: "!text-indigo-500",+ hexColor: "#6366f1",+ };+ }+if (schema?.type === "string" && schema?.format) {const formatMap: Record<string,diff --git a/autogpt_platform/frontend/src/components/molecules/Table/Table.stories.tsx b/autogpt_platform/frontend/src/components/molecules/Table/Table.stories.tsxnew file mode 100644index 0000000000..6dfb0b378f--- /dev/null+++ b/autogpt_platform/frontend/src/components/molecules/Table/Table.stories.tsx@@ -0,0 +1,116 @@+import type { Meta, StoryObj } from "@storybook/nextjs";+import { TooltipProvider } from "@/components/atoms/Tooltip/BaseTooltip";+import { Table } from "./Table";++const meta = {+ title: "Molecules/Table",+ component: Table,+ decorators: [+ (Story) => (+ <TooltipProvider>+ <Story />+ </TooltipProvider>+ ),+ ],+ parameters: {+ layout: "centered",+ },+ tags: ["autodocs"],+ argTypes: {+ allowAddRow: {+ control: "boolean",+ description: "Whether to show the Add row button",+ },+ allowDeleteRow: {+ control: "boolean",+ description: "Whether to show delete buttons for each row",+ },+ readOnly: {+ control: "boolean",+ description:+ "Whether the table is read-only (renders text instead of inputs)",+ },+ addRowLabel: {+ control: "text",+ description: "Label for the Add row button",+ },+ },+} satisfies Meta<typeof Table>;++export default meta;+type Story = StoryObj<typeof meta>;++export const Default: Story = {+ args: {+ columns: ["name", "email", "role"],+ allowAddRow: true,+ allowDeleteRow: true,+ },+};++export const WithDefaultValues: Story = {+ args: {+ columns: ["name", "email", "role"],+ defaultValues: [+ { name: "John Doe", email: "[email protected]", role: "Admin" },+ { name: "Jane Smith", email: "[email protected]", role: "User" },+ { name: "Bob Wilson", email: "[email protected]", role: "Editor" },+ ],+ allowAddRow: true,+ allowDeleteRow: true,+ },+};++export const ReadOnly: Story = {+ args: {+ columns: ["name", "email"],+ defaultValues: [+ { name: "John Doe", email: "[email protected]" },+ { name: "Jane Smith", email: "[email protected]" },+ ],+ readOnly: true,+ },+};++export const NoAddOrDelete: Story = {+ args: {+ columns: ["name", "email"],+ defaultValues: [+ { name: "John Doe", email: "[email protected]" },+ { name: "Jane Smith", email: "[email protected]" },+ ],+ allowAddRow: false,+ allowDeleteRow: false,+ },+};++export const SingleColumn: Story = {+ args: {+ columns: ["item"],+ allowAddRow: true,+ allowDeleteRow: true,+ addRowLabel: "Add item",+ },+};++export const CustomAddLabel: Story = {+ args: {+ columns: ["key", "value"],+ allowAddRow: true,+ allowDeleteRow: true,+ addRowLabel: "Add new entry",+ },+};++export const KeyValuePairs: Story = {+ args: {+ columns: ["key", "value"],+ defaultValues: [+ { key: "API_KEY", value: "sk-..." },+ { key: "DATABASE_URL", value: "postgres://..." },+ ],+ allowAddRow: true,+ allowDeleteRow: true,+ addRowLabel: "Add variable",+ },+};diff --git a/autogpt_platform/frontend/src/components/molecules/Table/Table.tsx b/autogpt_platform/frontend/src/components/molecules/Table/Table.tsxnew file mode 100644index 0000000000..a09a8344a5--- /dev/null+++ b/autogpt_platform/frontend/src/components/molecules/Table/Table.tsx@@ -0,0 +1,133 @@+import * as React from "react";+import {+ Table as BaseTable,+ TableBody,+ TableCell,+ TableHead,+ TableHeader,+ TableRow,+} from "@/components/__legacy__/ui/table";+import { Button } from "@/components/atoms/Button/Button";+import { Input } from "@/components/atoms/Input/Input";+import { Text } from "@/components/atoms/Text/Text";+import { Plus, Trash2 } from "lucide-react";+import { cn } from "@/lib/utils";+import { useTable, RowData } from "./useTable";+import { formatColumnTitle, formatPlaceholder } from "./helpers";++export interface TableProps {+ columns: string[];+ defaultValues?: RowData[];+ onChange?: (rows: RowData[]) => void;+ allowAddRow?: boolean;+ allowDeleteRow?: boolean;+ addRowLabel?: string;+ className?: string;+ readOnly?: boolean;+}++export function Table({+ columns,+ defaultValues,+ onChange,+ allowAddRow = true,+ allowDeleteRow = true,+ addRowLabel = "Add row",+ className,+ readOnly = false,+}: TableProps) {+ const { rows, handleAddRow, handleDeleteRow, handleCellChange } = useTable({+ columns,+ defaultValues,+ onChange,+ });++ const showDeleteColumn = allowDeleteRow && !readOnly;+ const showAddButton = allowAddRow && !readOnly;++ return (+ <div className={cn("flex flex-col gap-3", className)}>+ <div className="overflow-hidden rounded-xl border border-zinc-200 bg-white">+ <BaseTable>+ <TableHeader>+ <TableRow className="border-b border-zinc-100 bg-zinc-50/50">+ {columns.map((column) => (+ <TableHead+ key={column}+ className="h-10 px-3 text-sm font-medium text-zinc-600"+ >+ {formatColumnTitle(column)}+ </TableHead>+ ))}+ {showDeleteColumn && <TableHead className="w-[50px]" />}+ </TableRow>+ </TableHeader>+ <TableBody>+ {rows.map((row, rowIndex) => (+ <TableRow key={rowIndex} className="border-none">+ {columns.map((column) => (+ <TableCell key={`${rowIndex}-${column}`} className="p-2">+ {readOnly ? (+ <Text+ variant="body"+ className="px-3 py-2 text-sm text-zinc-800"+ >+ {row[column] || "-"}+ </Text>+ ) : (+ <Input+ id={`table-${rowIndex}-${column}`}+ label={formatColumnTitle(column)}+ hideLabel+ value={row[column] ?? ""}+ onChange={(e) =>+ handleCellChange(rowIndex, column, e.target.value)+ }+ placeholder={formatPlaceholder(column)}+ size="small"+ wrapperClassName="mb-0"+ />+ )}+ </TableCell>+ ))}+ {showDeleteColumn && (+ <TableCell className="p-2">+ <Button+ variant="icon"+ size="icon"+ onClick={() => handleDeleteRow(rowIndex)}+ aria-label="Delete row"+ className="text-zinc-400 transition-colors hover:text-red-500"+ >+ <Trash2 className="h-4 w-4" />+ </Button>+ </TableCell>+ )}+ </TableRow>+ ))}+ {showAddButton && (+ <TableRow>+ <TableCell+ colSpan={columns.length + (showDeleteColumn ? 1 : 0)}+ className="p-2"+ >+ <Button+ variant="outline"+ size="small"+ onClick={handleAddRow}+ leftIcon={<Plus className="h-4 w-4" />}+ className="w-fit"+ >+ {addRowLabel}+ </Button>+ </TableCell>+ </TableRow>+ )}+ </TableBody>+ </BaseTable>+ </div>+ </div>+ );+}++export { type RowData } from "./useTable";diff --git a/autogpt_platform/frontend/src/components/molecules/Table/helpers.ts b/autogpt_platform/frontend/src/components/molecules/Table/helpers.tsnew file mode 100644index 0000000000..3ea116095a--- /dev/null+++ b/autogpt_platform/frontend/src/components/molecules/Table/helpers.ts@@ -0,0 +1,7 @@+export const formatColumnTitle = (key: string): string => {+ return key.charAt(0).toUpperCase() + key.slice(1);+};++export const formatPlaceholder = (key: string): string => {+ return `Enter ${key.toLowerCase()}`;+};diff --git a/autogpt_platform/frontend/src/components/molecules/Table/useTable.ts b/autogpt_platform/frontend/src/components/molecules/Table/useTable.tsnew file mode 100644index 0000000000..085c18aa74--- /dev/null+++ b/autogpt_platform/frontend/src/components/molecules/Table/useTable.ts@@ -0,0 +1,81 @@+import { useState, useEffect } from "react";++export type RowData = Record<string, string>;++interface UseTableOptions {+ columns: string[];+ defaultValues?: RowData[];+ onChange?: (rows: RowData[]) => void;+}++export function useTable({+ columns,+ defaultValues,+ onChange,+}: UseTableOptions) {+ const createEmptyRow = (): RowData => {+ const emptyRow: RowData = {};+ columns.forEach((column) => {+ emptyRow[column] = "";+ });+ return emptyRow;+ };++ const [rows, setRows] = useState<RowData[]>(() => {+ if (defaultValues && defaultValues.length > 0) {+ return defaultValues;+ }+ return [];+ });++ useEffect(() => {+ if (defaultValues !== undefined) {+ setRows(defaultValues);+ }+ }, [defaultValues]);++ const updateRows = (newRows: RowData[]) => {+ setRows(newRows);+ onChange?.(newRows);+ };++ const handleAddRow = () => {+ const newRows = [...rows, createEmptyRow()];+ updateRows(newRows);+ };++ const handleDeleteRow = (rowIndex: number) => {+ const newRows = rows.filter((_, index) => index !== rowIndex);+ updateRows(newRows);+ };++ const handleCellChange = (+ rowIndex: number,+ columnKey: string,+ value: string,+ ) => {+ const newRows = rows.map((row, index) => {+ if (index === rowIndex) {+ return {+ ...row,+ [columnKey]: value,+ };+ }+ return row;+ });+ updateRows(newRows);+ };++ const clearAll = () => {+ updateRows([]);+ };++ return {+ rows,+ handleAddRow,+ handleDeleteRow,+ handleCellChange,+ clearAll,+ createEmptyRow,+ };+}diff --git a/autogpt_platform/frontend/src/components/renderers/InputRenderer/base/anyof/AnyOfField.tsx b/autogpt_platform/frontend/src/components/renderers/InputRenderer/base/anyof/AnyOfField.tsxindex d00925bfde..3040d11b40 100644--- a/autogpt_platform/frontend/src/components/renderers/InputRenderer/base/anyof/AnyOfField.tsx+++ b/autogpt_platform/frontend/src/components/renderers/InputRenderer/base/anyof/AnyOfField.tsx@@ -12,13 +12,7 @@ export const AnyOfField = (props: FieldProps) => {const { fields } = registry;const { SchemaField: _SchemaField } = fields;const { nodeId } = registry.formContext;-const { isInputConnected } = useEdgeStore();-- const uiOptions = getUiOptions(props.uiSchema, props.globalUiOptions);-- const Widget = getWidget({ type: "string" }, "select", registry.widgets);-const {handleOptionChange,enumOptions,@@ -27,6 +21,15 @@ export const AnyOfField = (props: FieldProps) => {field_id,} = useAnyOfField(props);+ const parentCustomFieldId = findCustomFieldId(schema);+ if (parentCustomFieldId) {+ return null;+ }++ const uiOptions = getUiOptions(props.uiSchema, props.globalUiOptions);++ const Widget = getWidget({ type: "string" }, "select", registry.widgets);+const handleId = getHandleId({uiOptions,id: field_id + ANY_OF_FLAG,diff --git a/autogpt_platform/frontend/src/components/renderers/InputRenderer/custom/TableField/TableField.tsx b/autogpt_platform/frontend/src/components/renderers/InputRenderer/custom/TableField/TableField.tsxnew file mode 100644index 0000000000..b48eca3238--- /dev/null+++ b/autogpt_platform/frontend/src/components/renderers/InputRenderer/custom/TableField/TableField.tsx@@ -0,0 +1,52 @@+import { descriptionId, FieldProps, getTemplate, titleId } from "@rjsf/utils";+import { Table, RowData } from "@/components/molecules/Table/Table";+import { useMemo } from "react";++export const TableField = (props: FieldProps) => {+ const { schema, formData, onChange, fieldPathId, registry, uiSchema } = props;++ const itemSchema = schema.items as any;+ const properties = itemSchema?.properties || {};++ const columns: string[] = useMemo(() => {+ return Object.keys(properties);+ }, [properties]);++ const handleChange = (rows: RowData[]) => {+ onChange(rows, fieldPathId?.path.slice(0, -1));+ };++ const TitleFieldTemplate = getTemplate("TitleFieldTemplate", registry);+ const DescriptionFieldTemplate = getTemplate(+ "DescriptionFieldTemplate",+ registry,+ );++ return (+ <div className="flex flex-col gap-2">+ <TitleFieldTemplate+ id={titleId(fieldPathId)}+ title={schema.title || ""}+ required={true}+ schema={schema}+ uiSchema={uiSchema}+ registry={registry}+ />+ <DescriptionFieldTemplate+ id={descriptionId(fieldPathId)}+ description={schema.description || ""}+ schema={schema}+ registry={registry}+ />++ <Table+ columns={columns}+ defaultValues={formData}+ onChange={handleChange}+ allowAddRow={true}+ allowDeleteRow={true}+ addRowLabel="Add row"+ />+ </div>+ );+};diff --git a/autogpt_platform/frontend/src/components/renderers/InputRenderer/custom/custom-registry.ts b/autogpt_platform/frontend/src/components/renderers/InputRenderer/custom/custom-registry.tsindex cf5d916164..caec56d15a 100644--- a/autogpt_platform/frontend/src/components/renderers/InputRenderer/custom/custom-registry.ts+++ b/autogpt_platform/frontend/src/components/renderers/InputRenderer/custom/custom-registry.ts@@ -3,6 +3,7 @@ import { CredentialsField } from "./CredentialField/CredentialField";import { GoogleDrivePickerField } from "./GoogleDrivePickerField/GoogleDrivePickerField";import { MultiSelectField } from "./MultiSelectField/MultiSelectField";import { isMultiSelectSchema } from "../utils/schema-utils";+import { TableField } from "./TableField/TableField";export interface CustomFieldDefinition {id: string;@@ -37,6 +38,17 @@ export const CUSTOM_FIELDS: CustomFieldDefinition[] = [matcher: isMultiSelectSchema,component: MultiSelectField,},+ {+ id: "custom/table_field",+ matcher: (schema: any) => {+ return (+ schema.type === "array" &&+ "format" in schema &&+ schema.format === "table"+ );+ },+ component: TableField,+ },];export function findCustomFieldId(schema: any): string | null {