@@ -44,6 +44,9 @@ export function PanelEditorForm(props: PanelEditorFormProps): ReactElement {
4444 const { plugin } = panelDefinition . spec ;
4545 const [ isDiscardDialogOpened , setDiscardDialogOpened ] = useState < boolean > ( false ) ;
4646
47+ // Ensure display is initialized (can be undefined)
48+ const displayData = panelDefinition . spec . display ?? { name : undefined , description : undefined } ;
49+
4750 const { panelEditorSchema } = useValidationSchemas ( ) ;
4851 const form = useForm < PanelEditorValues > ( {
4952 resolver : zodResolver ( panelEditorSchema ) ,
@@ -87,7 +90,21 @@ export function PanelEditorForm(props: PanelEditorFormProps): ReactElement {
8790 // - update action: ask for discard approval if changed
8891 // - read action: don´t ask for discard approval
8992 function handleCancel ( ) : void {
90- if ( JSON . stringify ( initialValues ) !== JSON . stringify ( form . getValues ( ) ) ) {
93+ const currentValues = form . getValues ( ) ;
94+
95+ // Normalize display: if both name and description are undefined, set display to undefined
96+ const normalizeDisplay = ( values : PanelEditorValues ) => {
97+ if ( values . panelDefinition . spec . display ?. name === undefined &&
98+ values . panelDefinition . spec . display ?. description === undefined ) {
99+ values . panelDefinition . spec . display = undefined ;
100+ }
101+ return values ;
102+ } ;
103+
104+ const normalizedInitial = normalizeDisplay ( JSON . parse ( JSON . stringify ( initialValues ) ) ) ;
105+ const normalizedCurrent = normalizeDisplay ( JSON . parse ( JSON . stringify ( currentValues ) ) ) ;
106+
107+ if ( JSON . stringify ( normalizedInitial ) !== JSON . stringify ( normalizedCurrent ) ) {
91108 setDiscardDialogOpened ( true ) ;
92109 } else {
93110 onClose ( ) ;
@@ -111,6 +128,10 @@ export function PanelEditorForm(props: PanelEditorFormProps): ReactElement {
111128 const watchedDescription = useWatch ( { control : form . control , name : 'panelDefinition.spec.display.description' } ) ;
112129 const watchedPluginKind = useWatch ( { control : form . control , name : 'panelDefinition.spec.plugin.kind' } ) ;
113130
131+ // Handle undefined display for rendering purposes
132+ const displayName = displayData . name ;
133+ const displayDescription = displayData . description ;
134+
114135 const handleSubmit = useCallback ( ( ) => {
115136 form . handleSubmit ( processForm ) ( ) ;
116137 } , [ form , processForm ] ) ;
@@ -145,7 +166,6 @@ export function PanelEditorForm(props: PanelEditorFormProps): ReactElement {
145166 render = { ( { field, fieldState } ) => (
146167 < TextField
147168 { ...field }
148- required
149169 fullWidth
150170 label = "Name"
151171 error = { ! ! fieldState . error }
@@ -196,7 +216,7 @@ export function PanelEditorForm(props: PanelEditorFormProps): ReactElement {
196216 label = "Description"
197217 error = { ! ! fieldState . error }
198218 helperText = { fieldState . error ?. message }
199- value = { watchedDescription ?? '' }
219+ value = { displayDescription ?? '' }
200220 onChange = { ( event ) => {
201221 field . onChange ( event ) ;
202222 setDescription ( event . target . value ) ;
0 commit comments