Skip to content

Commit c8c9d9b

Browse files
committed
feat: Enhance FeaturesV1 with dynamic validation for Standard MH/UOM based on operation type and rename Module Description to Comment
1 parent 311f812 commit c8c9d9b

File tree

1 file changed

+81
-32
lines changed

1 file changed

+81
-32
lines changed

src/components/FeaturesV1.js

Lines changed: 81 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ function FeaturesV1({
109109
{ value: 'HRS', label: 'HRS' }
110110
];
111111

112-
const operationOptions = ['+', '-', '*', '/'];
112+
const operationOptions = ['+', '-', '*', '/', 'Number', 'String'];
113113
const conditionOptions = ['==', '>', '<', '<>'];
114114
const typeOptions = ['PARAM ID', 'NUMBER', 'TEXT'];
115115

@@ -145,9 +145,9 @@ function FeaturesV1({
145145
errors[`${rowPath}.paramId`] = 'Param ID is required';
146146
}
147147

148-
// Validate Module Description
148+
// Validate Comment
149149
if (!row.moduleDesc || row.moduleDesc.trim() === '') {
150-
errors[`${rowPath}.moduleDesc`] = 'Module Description is required';
150+
errors[`${rowPath}.moduleDesc`] = 'Comment is required';
151151
}
152152

153153
// Enhanced: Check condition type instead of ifChecked
@@ -161,10 +161,29 @@ function FeaturesV1({
161161
if (row.standardMH === null || row.standardMH === undefined || row.standardMH === '') {
162162
errors[`${rowPath}.standardMH`] = 'Standard MH/UOM is required';
163163
} else {
164-
// Validate that it only contains allowed mathematical characters
165-
const mathRegex = /^[0-9+\-*/.()]+$/;
166-
if (!mathRegex.test(row.standardMH)) {
167-
errors[`${rowPath}.standardMH`] = 'Standard MH/UOM must contain only numbers and math operators (+, -, *, /, ., (, ))';
164+
// Enhanced validation based on operation type
165+
const operation = row.operation;
166+
if (operation === 'Number') {
167+
// Validate mathematical characters
168+
const mathRegex = /^[0-9+\-*/.()]+$/;
169+
if (!mathRegex.test(row.standardMH)) {
170+
errors[`${rowPath}.standardMH`] = 'Standard MH/UOM must contain only numbers and math operators (+, -, *, /, ., (, ))';
171+
}
172+
} else if (operation === 'String') {
173+
// Validate string characters (A-Z, a-z, _, -, single space)
174+
const stringRegex = /^[A-Za-z_\- ]+$/;
175+
const hasMultipleSpaces = /\s{2,}/.test(row.standardMH);
176+
if (!stringRegex.test(row.standardMH)) {
177+
errors[`${rowPath}.standardMH`] = 'Standard MH/UOM must contain only letters, underscore, dash, and single spaces';
178+
} else if (hasMultipleSpaces) {
179+
errors[`${rowPath}.standardMH`] = 'Standard MH/UOM cannot contain multiple consecutive spaces';
180+
}
181+
} else {
182+
// For +, -, *, / operations - validate mathematical characters
183+
const mathRegex = /^[0-9+\-*/.()]+$/;
184+
if (!mathRegex.test(row.standardMH)) {
185+
errors[`${rowPath}.standardMH`] = 'Standard MH/UOM must contain only numbers and math operators (+, -, *, /, ., (, ))';
186+
}
168187
}
169188
}
170189
} else {
@@ -524,15 +543,22 @@ function FeaturesV1({
524543
const paramDisplay = row.paramId ? `[${row.paramId}]` : '[PARAM]';
525544
// Convert to string and handle both string and number types
526545
const standardMH = String(row.standardMH || '');
546+
const operation = row.operation || '*';
527547

528-
// If Standard MH/UOM is empty, show just Param ID
529-
if (!standardMH || standardMH.trim() === '' || standardMH === '0') {
530-
return paramDisplay;
548+
// Special handling for Number and String operations
549+
if (operation === 'Number' || operation === 'String') {
550+
// For Number and String operations, show only Standard MH/UOM value
551+
if (!standardMH || standardMH.trim() === '') {
552+
return operation === 'Number' ? '0' : 'EMPTY_STRING';
553+
}
554+
return standardMH;
555+
} else {
556+
// For math operations (+, -, *, /), show full format
557+
if (!standardMH || standardMH.trim() === '' || standardMH === '0') {
558+
return paramDisplay;
559+
}
560+
return `${paramDisplay} ${operation} ${standardMH}`;
531561
}
532-
533-
// Otherwise show full format: Param ID + Operation + Standard MH/UOM
534-
const operation = row.operation || '*';
535-
return `${paramDisplay} ${operation} ${standardMH}`;
536562
} else {
537563
const leftVal = row.leftValue || 'LEFT';
538564
const condition = row.condition || '==';
@@ -929,18 +955,6 @@ function FeaturesV1({
929955
/>
930956
</div>
931957

932-
{/* Module Description */}
933-
<div className='col-block w200'>
934-
<TextField
935-
label="Module Description"
936-
value={row.moduleDesc}
937-
onChange={(e) => updateRow(row.id, 'moduleDesc', e.target.value)}
938-
variant="outlined"
939-
size="small"
940-
error={hasFieldError(row, 'moduleDesc')}
941-
/>
942-
</div>
943-
944958
{/* UOM - Disabled when IF checked */}
945959
<div className='col-block'>
946960
<FormControl
@@ -987,25 +1001,48 @@ function FeaturesV1({
9871001
</FormControl>
9881002
</div>
9891003

990-
{/* Standard MH/UOM - String input with math validation */}
1004+
{/* Standard MH/UOM - Dynamic validation based on operation */}
9911005
<div className='col-block'>
9921006
<TextField
9931007
label="Standard MH/UOM"
9941008
type="text"
9951009
value={String(row.standardMH || '')}
9961010
onChange={(e) => {
9971011
const value = e.target.value;
998-
// Only allow numbers, +, -, *, /, ., (, )
999-
const mathRegex = /^[0-9+\-*/.()]*$/;
1000-
if (mathRegex.test(value)) {
1001-
updateRow(row.id, 'standardMH', value);
1012+
const operation = row.operation;
1013+
1014+
// Different validation based on operation type
1015+
if (operation === 'Number') {
1016+
// Only allow numbers, +, -, *, /, ., (, )
1017+
const mathRegex = /^[0-9+\-*/.()]*$/;
1018+
if (mathRegex.test(value)) {
1019+
updateRow(row.id, 'standardMH', value);
1020+
}
1021+
} else if (operation === 'String') {
1022+
// Allow A-Z, a-z, _, -, and single space
1023+
const stringRegex = /^[A-Za-z_\- ]*$/;
1024+
// Check for single space (no multiple consecutive spaces)
1025+
const hasMultipleSpaces = /\s{2,}/.test(value);
1026+
if (stringRegex.test(value) && !hasMultipleSpaces) {
1027+
updateRow(row.id, 'standardMH', value);
1028+
}
1029+
} else {
1030+
// For +, -, *, / operations - math validation
1031+
const mathRegex = /^[0-9+\-*/.()]*$/;
1032+
if (mathRegex.test(value)) {
1033+
updateRow(row.id, 'standardMH', value);
1034+
}
10021035
}
10031036
}}
10041037
variant="outlined"
10051038
size="small"
10061039
disabled={row.conditionType !== 'None'}
10071040
error={hasFieldError(row, 'standardMH')}
1008-
placeholder="e.g. 10, (2+3)*4, 15.5"
1041+
placeholder={
1042+
row.operation === 'Number' ? "e.g. 10, (2+3)*4, 15.5" :
1043+
row.operation === 'String' ? "e.g. Product_Name, Test-Case" :
1044+
"e.g. 10, (2+3)*4, 15.5"
1045+
}
10091046
/>
10101047
</div>
10111048

@@ -1113,6 +1150,18 @@ function FeaturesV1({
11131150
<span>{generateFormula(row)}</span>
11141151
</div>
11151152

1153+
{/* Comment */}
1154+
<div className='col-block w200'>
1155+
<TextField
1156+
label="Comment"
1157+
value={row.moduleDesc}
1158+
onChange={(e) => updateRow(row.id, 'moduleDesc', e.target.value)}
1159+
variant="outlined"
1160+
size="small"
1161+
error={hasFieldError(row, 'moduleDesc')}
1162+
/>
1163+
</div>
1164+
11161165
{/* Delete Button - Only show for main rows, not for TRUE/FALSE child rows */}
11171166
{!isChild && (
11181167
<div className='col-block w60'>

0 commit comments

Comments
 (0)