Skip to content

Commit

Permalink
fix: Remove unused defaults from the Calculate component (#3522)
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr authored Aug 16, 2024
1 parent bbc137f commit 04ed5fb
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion editor.planx.uk/src/@planx/components/Calculate/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,24 @@ export default function Component(props: Props) {
},
});

/**
* When the formula is updated, remove any defaults which are no longer used
*/
const removeUnusedDefaults = (variables: Set<string>) => {
const defaultKeys = Object.keys(formik.values.defaults);

defaultKeys.forEach((key) => {
if (!variables.has(key)) {
formik.setFieldValue(`defaults[${key}]`, undefined);
}
});
};

const variables = React.useMemo(() => {
try {
return [...getVariables(formik.values.formula)];
const variables = getVariables(formik.values.formula)
removeUnusedDefaults(variables);
return [...variables];
} catch (e) {
return [];
}
Expand Down

0 comments on commit 04ed5fb

Please sign in to comment.