Skip to content
Snippets Groups Projects
Commit 1b1b9a28 authored by deep1401's avatar deep1401
Browse files

UseMemo to eliminate the usage of useEffect and correct minor bug which...

UseMemo to eliminate the usage of useEffect and correct minor bug which happens when setting some tasks in AutoComplete
parent 0df3cd82
No related branches found
No related tags found
No related merge requests found
...@@ -422,7 +422,13 @@ function CustomAutocompleteWidget<T = any, S extends StrictRJSFSchema = RJSFSche ...@@ -422,7 +422,13 @@ function CustomAutocompleteWidget<T = any, S extends StrictRJSFSchema = RJSFSche
// Determine default value. // Determine default value.
const defaultValue = _multiple ? [] : ''; const defaultValue = _multiple ? [] : '';
// Use the provided value or fallback to default. // Use the provided value or fallback to default.
const currentValue = value !== undefined ? value : defaultValue; let currentValue = value !== undefined ? value : defaultValue;
// Check if currentValue is an array, if a string, convert it to an array.
const isString = typeof currentValue === 'string';
if (isString) {
currentValue = currentValue.split(',');
}
// Map enumOptions into objects with label and value. // Map enumOptions into objects with label and value.
const processedOptionsValues = enumOptions.map((opt) => const processedOptionsValues = enumOptions.map((opt) =>
......
import React from 'react'; import React from 'react';
import { WidgetProps } from '@rjsf/core'; import { WidgetProps } from '@rjsf/core';
import { import { Button, Input, Select, Option } from '@mui/joy';
Button,
Input,
Select,
Option,
} from '@mui/joy';
type EvaluationField = { type EvaluationField = {
name: string; name: string;
...@@ -14,55 +8,44 @@ type EvaluationField = { ...@@ -14,55 +8,44 @@ type EvaluationField = {
return_type: string; return_type: string;
}; };
const parseValue = (val: any): EvaluationField[] => {
const CustomEvaluationWidget = (props: WidgetProps<any>) => { if (Array.isArray(val)) {
const { id, value, onChange, disabled, readonly } = props; if (val.every(item => typeof item === "string")) {
// If every element is a string: join them and parse the result.
const parseValue = (val: any): EvaluationField[] => {
if (Array.isArray(val)) {
if (val.every(item => typeof item === "string")) {
// If every element is a string: join them and parse the result.
try {
const joined = val.join(',');
const parsed = JSON.parse(joined);
return Array.isArray(parsed) ? parsed : [];
} catch (err) {
console.error("Error parsing evaluation widget value:", err);
return [];
}
} else {
// If not all elements are strings, assume it's already an array of EvaluationField.
return val;
}
} else if (typeof val === "string") {
try { try {
return JSON.parse(val); const joined = val.join(',');
const parsed = JSON.parse(joined);
return Array.isArray(parsed) ? parsed : [];
} catch (err) { } catch (err) {
console.error("Error parsing evaluation widget value string:", err); console.error("Error parsing evaluation widget value:", err);
return []; return [];
} }
} else {
// If not all elements are strings, assume it's already an array of EvaluationField.
return val;
} }
return []; } else if (typeof val === "string") {
}; try {
return JSON.parse(val);
const [evalMetrics, setEvalMetrics] = React.useState<EvaluationField[]>(parseValue(value)); } catch (err) {
console.error("Error parsing evaluation widget value string:", err);
return [];
}
}
return [];
};
const CustomEvaluationWidget = (props: WidgetProps<any>) => {
const { id, value, onChange, disabled, readonly } = props;
// Update state if a new default value is provided // Directly derive evaluation metrics from the value prop.
React.useEffect(() => { const evalMetrics: EvaluationField[] = React.useMemo(() => parseValue(value), [value]);
const parsed = parseValue(value);
if (JSON.stringify(parsed) !== JSON.stringify(evalMetrics) && parsed.length > 0) {
setEvalMetrics(parsed);
}
}, [value]);
const handleAddField = () => { const handleAddField = () => {
const updatedMetrics = [ const updatedMetrics = [
...evalMetrics, ...evalMetrics,
{ name: '', expression: '', return_type: 'boolean' } { name: '', expression: '', return_type: 'boolean' }
]; ];
setEvalMetrics(updatedMetrics);
onChange(updatedMetrics); onChange(updatedMetrics);
}; };
...@@ -74,13 +57,11 @@ const CustomEvaluationWidget = (props: WidgetProps<any>) => { ...@@ -74,13 +57,11 @@ const CustomEvaluationWidget = (props: WidgetProps<any>) => {
const updated = evalMetrics.map((evaluation, i) => const updated = evalMetrics.map((evaluation, i) =>
i === index ? { ...evaluation, [field]: newValue } : evaluation i === index ? { ...evaluation, [field]: newValue } : evaluation
); );
setEvalMetrics(updated);
onChange(updated); onChange(updated);
}; };
const handleRemoveField = (index: number) => { const handleRemoveField = (index: number) => {
const updated = evalMetrics.filter((_, i) => i !== index); const updated = evalMetrics.filter((_, i) => i !== index);
setEvalMetrics(updated);
onChange(updated); onChange(updated);
}; };
...@@ -126,7 +107,6 @@ const CustomEvaluationWidget = (props: WidgetProps<any>) => { ...@@ -126,7 +107,6 @@ const CustomEvaluationWidget = (props: WidgetProps<any>) => {
<Option value="number">Number</Option> <Option value="number">Number</Option>
<Option value="contains">Contains</Option> <Option value="contains">Contains</Option>
<Option value="isequal">IsEqual</Option> <Option value="isequal">IsEqual</Option>
</Select> </Select>
<Button <Button
onClick={() => handleRemoveField(index)} onClick={() => handleRemoveField(index)}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment