Skip to content

Commit 8e3a99d

Browse files
committed
fix Gui issues problem
Signed-off-by: basseche <[email protected]>
1 parent a2b040a commit 8e3a99d

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

src/components/dialogs/limits/limits-pane-utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
ID,
1515
LIMIT_SETS_MODIFICATION_TYPE,
1616
LIMITS,
17+
LIMITS_PROPERTIES,
1718
NAME,
1819
OPERATIONAL_LIMITS_GROUPS,
1920
PERMANENT_LIMIT,
@@ -24,6 +25,7 @@ import {
2425
TEMPORARY_LIMIT_NAME,
2526
TEMPORARY_LIMIT_VALUE,
2627
TEMPORARY_LIMITS,
28+
VALUE,
2729
} from 'components/utils/field-constants';
2830
import {
2931
areArrayElementsUnique,
@@ -53,6 +55,7 @@ const limitsGroupValidationSchema = (isModification: boolean) => ({
5355
[NAME]: yup.string().nonNullable().required(),
5456
[APPLICABIlITY]: yup.string().nonNullable().required(),
5557
[CURRENT_LIMITS]: yup.object().shape(currentLimitsValidationSchema(isModification)),
58+
[LIMITS_PROPERTIES]: yup.array().of(limitsPropertyValidationSchema()),
5659
});
5760

5861
const temporaryLimitsValidationSchema = () => {
@@ -68,6 +71,12 @@ const temporaryLimitsValidationSchema = () => {
6871
}),
6972
});
7073
};
74+
const limitsPropertyValidationSchema = () => {
75+
return yup.object().shape({
76+
[NAME]: yup.string().nullable(),
77+
[VALUE]: yup.string().nullable(),
78+
});
79+
};
7180

7281
const currentLimitsValidationSchema = (isModification = false) => ({
7382
[PERMANENT_LIMIT]: isModification

src/components/dialogs/limits/limits-properties-side-stack.tsx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,19 @@ import { useCallback, useMemo, useState } from 'react';
1010
import { AddCircle, Delete } from '@mui/icons-material';
1111
import { useIntl } from 'react-intl';
1212
import { LimitsProperty } from '../../../services/network-modification-types';
13-
import { useFormContext, useWatch } from 'react-hook-form';
13+
import { useFieldArray } from 'react-hook-form';
1414
import { usePredefinedProperties } from '@gridsuite/commons-ui';
1515

1616
export interface LimitsPropertiesSideStackProps {
1717
formName: string;
1818
disabled?: boolean;
1919
}
2020
export function LimitsPropertiesSideStack({ formName, disabled }: Readonly<LimitsPropertiesSideStackProps>) {
21-
const { setValue } = useFormContext();
22-
const limitsProperties: LimitsProperty[] = useWatch({ name: formName });
21+
const {
22+
fields: limitsProperties,
23+
append,
24+
remove,
25+
} = useFieldArray<{ [key: string]: LimitsProperty[] }>({ name: formName });
2326

2427
const [isEditing, setIsEditing] = useState<boolean>(false);
2528
const [hovered, setHovered] = useState<boolean>(false);
@@ -39,18 +42,17 @@ export function LimitsPropertiesSideStack({ formName, disabled }: Readonly<Limit
3942
if (!propertyName.trim() || !propertyValue.trim()) {
4043
return;
4144
}
42-
if (!limitsProperties?.length) {
43-
setValue(formName, [{ name: propertyName, value: propertyValue }]);
44-
} else if (limitsProperties.some((l) => l.name === propertyName)) {
45+
46+
if (limitsProperties.some((l) => l.name === propertyName)) {
4547
setEditorError(intl.formatMessage({ id: 'NameUnique' }));
4648
return;
4749
} else {
48-
setValue(formName, [...limitsProperties, { name: propertyName, value: propertyValue }]);
50+
append({ name: propertyName, value: propertyValue });
4951
}
5052
setIsEditing(false);
5153
}
5254
},
53-
[formName, intl, limitsProperties, propertyName, propertyValue, setValue]
55+
[append, intl, limitsProperties, propertyName, propertyValue]
5456
);
5557

5658
const handleOnChange = useCallback((value: string) => {
@@ -63,9 +65,9 @@ export function LimitsPropertiesSideStack({ formName, disabled }: Readonly<Limit
6365
if (limitsProperties?.length <= index) {
6466
return;
6567
}
66-
setValue(formName, [...limitsProperties.slice(0, index), ...limitsProperties.slice(index + 1)]);
68+
remove(index);
6769
},
68-
[formName, limitsProperties, setValue]
70+
[limitsProperties?.length, remove]
6971
);
7072

7173
return (

0 commit comments

Comments
 (0)