Skip to content

Commit 22510f3

Browse files
committed
Don't render EditDialog when not editing
Fixes a bug where the state for the form fields wasn't being populated by the initial values coming from props. As the form was always rendered, regardless of whether anything was currently being edited, the initial values coming from the props and passed to the `useState` calls were empty. Then when a particular section was selected, these props changed to reflect the data from that section. However, `useState` only uses the initial values from the *first render*, so it was ignoring these updated props, and retaining the empty values. The solution is not to render the form while nothing is being edited, and then render it when a section is selected for editing. That way each time a section is selected for editing, the form is considered a new addition to the tree, and therefore the initial data coming from props on the "first render" is the section data.
1 parent 53f90f6 commit 22510f3

File tree

2 files changed

+8
-26
lines changed

2 files changed

+8
-26
lines changed

dotcom-rendering/src/admin/appsNavTool/EditDialog.stories.tsx

Lines changed: 0 additions & 22 deletions
This file was deleted.

dotcom-rendering/src/admin/appsNavTool/EditDialog.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,20 @@ export const EditDialog = (props: Props) => {
2424
dispatch({ kind: 'cancelEdit' });
2525
}, [dispatch]);
2626

27+
if (props.editing === undefined) {
28+
return null;
29+
}
30+
2731
return (
2832
<SectionForm
2933
heading="Edit Section"
30-
open={props.editing !== undefined}
31-
initialTitle={props.editing?.title ?? ''}
34+
open={true}
35+
initialTitle={props.editing.title}
3236
initialPath={new URL(
33-
props.editing?.path ?? '',
37+
props.editing.path,
3438
'https://www.theguardian.com',
3539
).toString()}
36-
initialMobileOverride={props.editing?.mobileOverride}
40+
initialMobileOverride={props.editing.mobileOverride}
3741
submit={submit}
3842
cancel={cancel}
3943
/>

0 commit comments

Comments
 (0)