From b0d1b87bcf882cbb74a06f25a4ae87d4d3058cd6 Mon Sep 17 00:00:00 2001 From: suryacp23 Date: Wed, 22 Jul 2026 11:31:59 +0530 Subject: [PATCH] added required fields --- .../forms/fields/SmartGridField.tsx | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/components/forms/fields/SmartGridField.tsx b/src/components/forms/fields/SmartGridField.tsx index 72e1311..ef04f59 100644 --- a/src/components/forms/fields/SmartGridField.tsx +++ b/src/components/forms/fields/SmartGridField.tsx @@ -22,6 +22,7 @@ export function SmartGridField({ const formRef = useRef(null); const [newRow, setNewRow] = useState>({}); const [editingIdx, setEditingIdx] = useState(null); + const [errors, setErrors] = useState>({}); const visibleColumns = columns.filter(c => { const isOrderDetails = label.toLowerCase().includes('order'); @@ -61,6 +62,7 @@ export function SmartGridField({ const updateNewRowField = (fieldId: string, val: unknown) => { let row = { ...newRow, [fieldId]: val }; + setErrors(prev => ({ ...prev, [fieldId]: '' })); const colDef = columns.find(c => c.id === fieldId); @@ -122,6 +124,26 @@ export function SmartGridField({ }; const submitNewRow = () => { + const newErrors: Record = {}; + const getBaseId = (id: string) => id.replace(/_\d+$/, ''); + const catColId = columns.find(c => c.id === 'product_category' || c.name === 'Product Category' || c.mapped_workflow_field === 'product_category' || getBaseId(c.id) === 'product_category')?.id; + const isCatSelected = catColId ? !!newRow[catColId] : false; + + for (const col of visibleColumns) { + const isProductName = col.id === 'product_name' || col.name === 'Product Name' || col.mapped_workflow_field === 'product_name' || getBaseId(col.id) === 'product_name'; + const isBags = col.id === 'bags' || col.name === 'Bags' || col.mapped_workflow_field === 'bags' || getBaseId(col.id) === 'bags'; + const isRequired = col.mandatory || (isCatSelected && (isProductName || isBags)); + + if (isRequired && !newRow[col.id]) { + newErrors[col.id] = 'This field is required'; + } + } + + if (Object.keys(newErrors).length > 0) { + setErrors(newErrors); + return; + } + if (editingIdx !== null) { const next = [...value]; next[editingIdx] = newRow; @@ -255,6 +277,14 @@ export function SmartGridField({
{visibleColumns.map(col => { const val = newRow[col.id]; + + const getBaseId = (id: string) => id.replace(/_\d+$/, ''); + const catColId = columns.find(c => c.id === 'product_category' || c.name === 'Product Category' || c.mapped_workflow_field === 'product_category' || getBaseId(c.id) === 'product_category')?.id; + const isCatSelected = catColId ? !!newRow[catColId] : false; + const isProductName = col.id === 'product_name' || col.name === 'Product Name' || col.mapped_workflow_field === 'product_name' || getBaseId(col.id) === 'product_name'; + const isBags = col.id === 'bags' || col.name === 'Bags' || col.mapped_workflow_field === 'bags' || getBaseId(col.id) === 'bags'; + const isRequired = col.mandatory || (isCatSelected && (isProductName || isBags)); + if (col.data_type === 'select' || col.data_type === 'multiselect') { const allOpts = col.properties?.options || []; let filteredOpts = allOpts; @@ -288,7 +318,8 @@ export function SmartGridField({ updateNewRowField(col.id, col.data_type === 'number' ? Number(e.target.value) : e.target.value)}