From 500d50b674faadf872b59acd817c032b50fe3683 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dafydd=20Ll=C5=B7r=20Pearson?= Date: Thu, 29 Aug 2024 22:18:00 +0100 Subject: [PATCH 1/9] feat: Remove feature from formik state --- .../components/MapAndLabel/Public/Context.tsx | 13 +++++++++++++ .../components/MapAndLabel/Public/index.tsx | 16 +++++++++------- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/editor.planx.uk/src/@planx/components/MapAndLabel/Public/Context.tsx b/editor.planx.uk/src/@planx/components/MapAndLabel/Public/Context.tsx index 87ce318cbb..7f1efb7ab2 100644 --- a/editor.planx.uk/src/@planx/components/MapAndLabel/Public/Context.tsx +++ b/editor.planx.uk/src/@planx/components/MapAndLabel/Public/Context.tsx @@ -24,6 +24,7 @@ interface MapAndLabelContextValue { isFeatureInvalid: (index: number) => boolean; addFeature: () => void; copyFeature: (sourceIndex: number, destinationIndex: number) => void; + removeFeature: (index: number) => void; mapAndLabelProps: PresentationalProps; errors: { min: boolean; @@ -106,6 +107,17 @@ export const MapAndLabelProvider: React.FC = ( const sourceFeature = formik.values.schemaData[sourceIndex]; formik.setFieldValue(`schemaData[${destinationIndex}]`, sourceFeature); }; + + const removeFeature = (index: number) => { + resetErrors(); + + setActiveIndex(-1); + + formik.setFieldValue( + "schemaData", + formik.values.schemaData.filter((_, i) => i !== index), + ); + }; return ( = ( validateAndSubmitForm, addFeature, copyFeature, + removeFeature, isFeatureInvalid, errors: { min: minError, diff --git a/editor.planx.uk/src/@planx/components/MapAndLabel/Public/index.tsx b/editor.planx.uk/src/@planx/components/MapAndLabel/Public/index.tsx index 725ef339a8..19563d4314 100644 --- a/editor.planx.uk/src/@planx/components/MapAndLabel/Public/index.tsx +++ b/editor.planx.uk/src/@planx/components/MapAndLabel/Public/index.tsx @@ -42,8 +42,14 @@ function a11yProps(index: number) { const VerticalFeatureTabs: React.FC<{ features: Feature[] }> = ({ features, }) => { - const { schema, activeIndex, formik, editFeature, isFeatureInvalid } = - useMapAndLabelContext(); + const { + schema, + activeIndex, + formik, + editFeature, + isFeatureInvalid, + removeFeature, + } = useMapAndLabelContext(); return ( = ({ formik={formik} />