Skip to content

Commit

Permalink
fix: fix default state of enum with dynamic values in scenario parame…
Browse files Browse the repository at this point in the history
…ters
  • Loading branch information
csm-thu committed May 2, 2024
1 parent d8447c3 commit 8b21105
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const GenericEnumInput = ({
context,
parameterValue,
setParameterValue,
resetParameterValue, // Set a new value without triggering the form 'dirty' state
isDirty,
gridItemProps,
}) => {
Expand Down Expand Up @@ -55,14 +56,14 @@ export const GenericEnumInput = ({
}, [t, parameterData, dynamicEnumValues]);

useEffect(() => {
// Call setParameterValue when dynamic values are enabled to trigger an update of the form validity. This
// Call resetParameterValue when dynamic values are enabled to trigger an update of the form validity. This
// is required to automatically enable a form confirmation button when a default value is selected from
// dynamic values (e.g. in the dataset creation wizard). Yet, we don't want this behavior in the scenario
// parameters, because it tends to set the form state to "dirty" for enums without default values.
if (dynamicEnumValues != null && parameterValue == null && enumValues?.length > 0) {
setParameterValue(enumValues[0]?.key);
resetParameterValue(enumValues[0]?.key);
}
}, [enumValues, dynamicEnumValues, parameterValue, setParameterValue]);
}, [enumValues, dynamicEnumValues, parameterValue, resetParameterValue]);

if (dynamicValuesError) return dynamicValuesError;
return (
Expand Down Expand Up @@ -90,6 +91,7 @@ GenericEnumInput.propTypes = {
context: PropTypes.object.isRequired,
parameterValue: PropTypes.any,
setParameterValue: PropTypes.func.isRequired,
resetParameterValue: PropTypes.func.isRequired,
isDirty: PropTypes.bool,
gridItemProps: PropTypes.object,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Cosmo Tech.
// Licensed under the MIT license.
import React, { useEffect, useMemo, useState } from 'react';
import { Controller } from 'react-hook-form';
import { Controller, useFormContext } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import PropTypes from 'prop-types';
import { Grid, Typography } from '@mui/material';
Expand All @@ -21,6 +21,7 @@ const clone = rfdc();

export const DatasetCreationParameters = ({ dataSourceRunTemplates, parentDataset }) => {
const { t } = useTranslation();
const { resetField } = useFormContext();
const { datasourceParameterHelpers, getDataSourceTypeEnumValues, getUploadFileLabels, getDefaultFileTypeFilter } =
useDatasetCreationParameters();

Expand Down Expand Up @@ -93,6 +94,7 @@ export const DatasetCreationParameters = ({ dataSourceRunTemplates, parentDatase
context={{ editMode: true, targetDatasetId: parentDataset?.id }}
parameterValue={value}
setParameterValue={onChange}
resetParameterValue={(newDefaultValue) => resetField(fieldPath, { defaultValue: newDefaultValue })}
gridItemProps={{ xs: 6, sx: { pt: 2 } }}
isDirty={null}
/>
Expand Down Expand Up @@ -142,6 +144,7 @@ export const DatasetCreationParameters = ({ dataSourceRunTemplates, parentDatase
dataSourceType,
parentDataset?.id,
datasourceParameterHelpers,
resetField,
]);

const labels = useMemo(() => {
Expand Down

0 comments on commit 8b21105

Please sign in to comment.