diff --git a/src/components/forms/fields/SmartGridField.tsx b/src/components/forms/fields/SmartGridField.tsx index 51ab648..307663e 100644 --- a/src/components/forms/fields/SmartGridField.tsx +++ b/src/components/forms/fields/SmartGridField.tsx @@ -1,8 +1,7 @@ -import { useState } from 'react'; +import { useState, useRef } from 'react'; import { Button } from '../../buttons/Button'; import { Select } from '../../reusable/Select'; import { Input } from '../../reusable/Input'; -import { Modal } from '../../reusable/Modal'; import { Trash2, Pencil } from 'lucide-react'; import type { FormScreenField } from '../../../api/types'; @@ -18,6 +17,7 @@ export function SmartGridField({ onChange: (val: Record[]) => void; }) { const [isModalOpen, setIsModalOpen] = useState(false); + const formRef = useRef(null); const [newRow, setNewRow] = useState>({}); const [editingIdx, setEditingIdx] = useState(null); @@ -49,6 +49,9 @@ export function SmartGridField({ setEditingIdx(null); setNewRow({}); setIsModalOpen(true); + setTimeout(() => { + formRef.current?.scrollIntoView({ behavior: 'smooth', block: 'nearest' }); + }, 150); }; const updateNewRowField = (fieldId: string, val: unknown) => { @@ -140,7 +143,11 @@ export function SmartGridField({ onChange([...value, newRow]); } } - setIsModalOpen(false); + + // Only close form if we were editing an existing row. For new rows, stay open. + if (editingIdx !== null) { + setIsModalOpen(false); + } setNewRow({}); setEditingIdx(null); }; @@ -210,80 +217,100 @@ export function SmartGridField({ )} -
-

- {editingIdx !== null ? ( - <> Edit Row - ) : ( - <>Add New Item - )} -

-
- {visibleColumns.map(col => { - const val = newRow[col.id]; - if (col.data_type === 'select' || col.data_type === 'multiselect') { - const allOpts = col.properties?.options || []; - let filteredOpts = allOpts; - - if (col.id === 'product_name' || col.name === 'Product Name' || col.mapped_workflow_field === 'product_name') { - const catCol = columns.find(c => c.id === 'product_category' || c.name === 'Product Category' || c.mapped_workflow_field === 'product_category'); - if (catCol) { - const selectedCategory = newRow[catCol.id] as string; - if (selectedCategory) { - filteredOpts = allOpts.filter(opt => { - const labelStr = String(opt.label || opt.value || ''); - return labelStr.startsWith(selectedCategory); - }); - } - } - } else { - filteredOpts = allOpts.filter(opt => { - if (!opt._raw) return true; - for (const [rowKey, rowVal] of Object.entries(newRow)) { - if (rowKey === col.id || rowVal == null || rowVal === '') continue; - const rawKey = Object.keys(opt._raw).find(rk => rk === rowKey || rk.replace(/_/g, '') === rowKey.replace(/_/g, '')); - if (rawKey && String(opt._raw[rawKey]) !== String(rowVal)) { - return false; +
+ +
+ +
+
+
+

+ {editingIdx !== null ? ( + <> Edit Row + ) : ( + <>Add New Item + )} +

+
+ {visibleColumns.map(col => { + const val = newRow[col.id]; + if (col.data_type === 'select' || col.data_type === 'multiselect') { + const allOpts = col.properties?.options || []; + let filteredOpts = allOpts; + + if (col.id === 'product_name' || col.name === 'Product Name' || col.mapped_workflow_field === 'product_name') { + const catCol = columns.find(c => c.id === 'product_category' || c.name === 'Product Category' || c.mapped_workflow_field === 'product_category'); + if (catCol) { + const selectedCategory = newRow[catCol.id] as string; + if (selectedCategory) { + filteredOpts = allOpts.filter(opt => { + const labelStr = String(opt.label || opt.value || ''); + return labelStr.startsWith(selectedCategory); + }); } } - return true; - }); - } + } else { + filteredOpts = allOpts.filter(opt => { + if (!opt._raw) return true; + for (const [rowKey, rowVal] of Object.entries(newRow)) { + if (rowKey === col.id || rowVal == null || rowVal === '') continue; + const rawKey = Object.keys(opt._raw).find(rk => rk === rowKey || rk.replace(/_/g, '') === rowKey.replace(/_/g, '')); + if (rawKey && String(opt._raw[rawKey]) !== String(rowVal)) { + return false; + } + } + return true; + }); + } + return ( + updateNewRowField(col.id, e.target.value)} - options={[{ value: '', label: 'Select...' }, ...filteredOpts.map((o: any) => ({ value: String(o.value), label: o.label }))]} + onChange={(e) => updateNewRowField(col.id, col.data_type === 'number' ? Number(e.target.value) : e.target.value)} /> ); - } - return ( - updateNewRowField(col.id, col.data_type === 'number' ? Number(e.target.value) : e.target.value)} - /> - ); - })} - -
- {editingIdx !== null && ( - - )} - + +
+
);