@@ -99,6 +99,8 @@ export const ObjectRuleForm = (props: IProps) => {
99
99
uriPatternSuggestions . filter ( ( p ) => p . value !== ( modifiedValues ( ) as any ) . pattern ) . map ( ( p ) => [ p . value , p ] )
100
100
) . values ( )
101
101
) ;
102
+ const currentUriPatterns = React . useRef < IUriPattern [ ] > ( [ ] )
103
+ currentUriPatterns . current = distinctUriPatterns
102
104
103
105
useEffect ( ( ) => {
104
106
const { id, scrollIntoView } = props ;
@@ -272,6 +274,46 @@ export const ObjectRuleForm = (props: IProps) => {
272
274
return validationResult ;
273
275
} ;
274
276
277
+ const uriPatternSelector = React . useMemo ( ( ) => {
278
+ return currentUriPatterns . current . length > 0 ? (
279
+ < >
280
+ < Spacing vertical = { true } size = { "tiny" } />
281
+ < Button
282
+ data-test-id = "object-rule-form-uri-pattern-selection-btn"
283
+ elevated = { true }
284
+ tooltip = { `Choose URI pattern from ${ currentUriPatterns . current . length } existing URI pattern/s.` }
285
+ onClick = { ( ) => setShowUriPatternModal ( true ) }
286
+ >
287
+ Choose
288
+ </ Button >
289
+ </ >
290
+ ) : undefined
291
+ } , [ uriPatternSuggestions . length > 0 ] ) ;
292
+
293
+ const uriPatternComponent = React . useMemo ( ( ) =>
294
+ < CodeAutocompleteField
295
+ id = { "uri-pattern-auto-suggestion" }
296
+ label = "URI pattern"
297
+ initialValue = { initialUriPattern }
298
+ clearIconText = { "Clear URI pattern" }
299
+ validationErrorText = { "The entered URI pattern is invalid." }
300
+ onChange = { ( value ) => {
301
+ handleChangeValue ( "pattern" , value ) ;
302
+ } }
303
+ fetchSuggestions = { ( input , cursorPosition ) =>
304
+ fetchUriPatternAutoCompletions (
305
+ parentId ? parentId : "root" ,
306
+ input ,
307
+ cursorPosition ,
308
+ modifiedValues ( ) . sourceProperty
309
+ )
310
+ }
311
+ onFocusChange = { setUriPatternInputHasFocus }
312
+ checkInput = { checkUriPattern }
313
+ rightElement = { uriPatternSelector }
314
+ reInitOnInitialValueChange = { true }
315
+ /> , [ initialUriPattern , uriPatternSelector ] )
316
+
275
317
if ( loading ) {
276
318
return < Spinner /> ;
277
319
}
@@ -378,44 +420,7 @@ export const ObjectRuleForm = (props: IProps) => {
378
420
</ FieldItem >
379
421
) ;
380
422
} else {
381
- patternInput = (
382
- < CodeAutocompleteField
383
- id = { "uri-pattern-auto-suggestion" }
384
- label = "URI pattern"
385
- initialValue = { initialUriPattern }
386
- clearIconText = { "Clear URI pattern" }
387
- validationErrorText = { "The entered URI pattern is invalid." }
388
- onChange = { ( value ) => {
389
- handleChangeValue ( "pattern" , value ) ;
390
- } }
391
- fetchSuggestions = { ( input , cursorPosition ) =>
392
- fetchUriPatternAutoCompletions (
393
- parentId ? parentId : "root" ,
394
- input ,
395
- cursorPosition ,
396
- modifiedValues ( ) . sourceProperty
397
- )
398
- }
399
- onFocusChange = { setUriPatternInputHasFocus }
400
- checkInput = { checkUriPattern }
401
- rightElement = {
402
- distinctUriPatterns . length > 0 ? (
403
- < >
404
- < Spacing vertical = { true } size = { "tiny" } />
405
- < Button
406
- data-test-id = "object-rule-form-uri-pattern-selection-btn"
407
- elevated = { true }
408
- tooltip = { `Choose URI pattern from ${ distinctUriPatterns . length } existing URI pattern/s.` }
409
- onClick = { ( ) => setShowUriPatternModal ( true ) }
410
- >
411
- Choose
412
- </ Button >
413
- </ >
414
- ) : undefined
415
- }
416
- reInitOnInitialValueChange = { true }
417
- />
418
- ) ;
423
+ patternInput = uriPatternComponent
419
424
}
420
425
} else {
421
426
patternInput = (
@@ -501,7 +506,11 @@ export const ObjectRuleForm = (props: IProps) => {
501
506
onClose = { ( ) => setShowUriPatternModal ( false ) }
502
507
uriPatterns = { distinctUriPatterns }
503
508
onSelect = { ( uriPattern ) => {
504
- setInitialUriPattern ( uriPattern . value ) ;
509
+ // Necessary if the URI pattern has been changed and the selected URI pattern is the initial pattern
510
+ if ( initialUriPattern === uriPattern . value ) {
511
+ setInitialUriPattern ( "" ) ;
512
+ }
513
+ setTimeout ( ( ) => setInitialUriPattern ( uriPattern . value ) , 0 ) ;
505
514
handleChangeValue ( "pattern" , uriPattern . value ) ;
506
515
} }
507
516
/>
0 commit comments