@@ -20,6 +20,8 @@ import { getTitle, findTitle } from './title';
20
20
import { LinkPreservingSearch , NavLinkPreservingSearch } from './search-preserving-link' ;
21
21
import { dump } from 'js-yaml' ;
22
22
import { isExternalReference } from './type-inference' ;
23
+ import { SchemaValidator } from './SchemaValidator' ;
24
+ import type { editor , IRange } from 'monaco-editor' ;
23
25
24
26
interface SEPHeadProps {
25
27
basePathSegments : Array < string > ;
@@ -416,29 +418,19 @@ export type SchemaExplorerProps = {
416
418
schema : JsonSchema1 ;
417
419
stage : Stage ;
418
420
lookup : Lookup ;
421
+ onSelectValidationRange : ( range : IRange ) => void ;
422
+ validationResults : editor . IMarker [ ]
419
423
} ;
420
424
421
- export type ViewType = 'details' | 'example-json' | 'example-yaml' ;
425
+ export type ViewType = 'details' | 'example-json' | 'example-yaml' | 'validator' ;
422
426
423
427
export type SchemaExplorerState = {
424
428
pathExpanded : boolean ;
425
429
view : ViewType ;
426
430
} ;
427
431
428
- const LabelToViewType : { [ label : string ] : ViewType } = {
429
- 'Details' : 'details' ,
430
- 'Example (JSON)' : 'example-json' ,
431
- 'Example (YAML)' : 'example-yaml'
432
- } ;
433
-
434
- const ViewTypeToTab : { [ viewType : string ] : number } = {
435
- 'details' : 0 ,
436
- 'example-json' : 1 ,
437
- 'example-yaml' : 2
438
- } ;
439
-
440
432
export class SchemaExplorer extends React . PureComponent < SchemaExplorerProps , SchemaExplorerState > {
441
- private static Container = styled . section `
433
+ public static Container = styled . section `
442
434
display: flex;
443
435
flex-direction: column;
444
436
flex-grow: 1;
@@ -447,61 +439,79 @@ export class SchemaExplorer extends React.PureComponent<SchemaExplorerProps, Sch
447
439
max-width: 100%;
448
440
` ;
449
441
450
- private static HeadingContainer = styled . div `
442
+ public static HeadingContainer = styled . div `
451
443
display: flex;
452
444
flex-direction: row;
453
445
justify-content: space-between;
454
446
` ;
455
447
456
- private static Heading = styled . h1 `
448
+ public static Heading = styled . h1 `
457
449
font-size: 16px;
458
450
font-weight: 600;
459
451
padding-top: 24px;
460
452
margin: 5px 8px;
461
453
` ;
462
454
463
- UNSAFE_componentWillMount ( ) {
464
- this . setState ( {
455
+ constructor ( props : SchemaExplorerProps ) {
456
+ super ( props ) ;
457
+ this . state = {
465
458
pathExpanded : false ,
466
459
view : 'details'
467
- } ) ;
460
+ }
468
461
}
469
462
470
463
render ( ) {
471
- const { path, schema, lookup, stage, basePathSegments } = this . props ;
464
+ const { path, schema, lookup, stage, basePathSegments, validationResults , onSelectValidationRange } = this . props ;
472
465
const { pathExpanded } = this . state ;
473
466
if ( path . length === 0 ) {
474
467
return < div > TODO What do we do when the reference could not be found? Error maybe?</ div > ;
475
468
}
476
469
477
470
const currentPathElement = path [ path . length - 1 ] ;
478
471
479
- const tabData : TabData [ ] = [ {
480
- label : 'Details' ,
481
- content : (
482
- < SchemaExplorerDetails
483
- schema = { schema }
484
- reference = { currentPathElement . reference }
485
- lookup = { lookup }
486
- stage = { stage }
487
- clickElement = { createClickElement ( { basePathSegments, path } ) }
488
- />
489
- )
490
- } , {
491
- label : 'Example (JSON)' ,
492
- content : (
493
- < SchemaExplorerExample schema = { schema } lookup = { lookup } stage = { stage } format = "json" />
494
- )
495
- } , {
496
- label : 'Example (YAML)' ,
497
- content : (
498
- < SchemaExplorerExample schema = { schema } lookup = { lookup } stage = { stage } format = "yaml" />
499
- )
500
- } ] ;
501
-
502
- const onTabSelect : OnSelectCallback = tab => {
472
+ type ExtendedTabData = TabData & {
473
+ view : ViewType ;
474
+ }
475
+ const tabData : ExtendedTabData [ ] = [
476
+ {
477
+ view : 'details' ,
478
+ label : 'Details' ,
479
+ content : (
480
+ < SchemaExplorerDetails
481
+ schema = { schema }
482
+ reference = { currentPathElement . reference }
483
+ lookup = { lookup }
484
+ stage = { stage }
485
+ clickElement = { createClickElement ( { basePathSegments, path } ) }
486
+ />
487
+ ) ,
488
+ } ,
489
+ {
490
+ view : 'example-json' ,
491
+ label : 'Example (JSON)' ,
492
+ content : (
493
+ < SchemaExplorerExample schema = { schema } lookup = { lookup } stage = { stage } format = "json" />
494
+ ) ,
495
+ } ,
496
+ {
497
+ view : 'example-yaml' ,
498
+ label : 'Example (YAML)' ,
499
+ content : (
500
+ < SchemaExplorerExample schema = { schema } lookup = { lookup } stage = { stage } format = "yaml" />
501
+ ) ,
502
+ } ,
503
+ {
504
+ view : 'validator' ,
505
+ label : `Validation results (${ validationResults . length } )` ,
506
+ content : (
507
+ < SchemaValidator results = { validationResults } onSelectRange = { onSelectValidationRange } />
508
+ ) ,
509
+ } ,
510
+ ] ;
511
+
512
+ const onTabSelect : OnSelectCallback = ( tab ) => {
503
513
this . setState ( {
504
- view : LabelToViewType [ tab . label || 'Details' ]
514
+ view : ( tab as ExtendedTabData ) . view
505
515
} ) ;
506
516
} ;
507
517
@@ -517,7 +527,11 @@ export class SchemaExplorer extends React.PureComponent<SchemaExplorerProps, Sch
517
527
< SchemaExplorer . Heading > { getTitle ( currentPathElement . reference , schema ) } </ SchemaExplorer . Heading >
518
528
< Permalink />
519
529
</ SchemaExplorer . HeadingContainer >
520
- < Tabs tabs = { tabData } selected = { ViewTypeToTab [ this . state . view || 'details' ] } onSelect = { onTabSelect } />
530
+ < Tabs
531
+ tabs = { tabData }
532
+ onSelect = { onTabSelect }
533
+ selected = { tabData . findIndex ( ( tab ) => tab . view === ( this . state . view || 'details' ) ) }
534
+ />
521
535
</ SchemaExplorer . Container >
522
536
) ;
523
537
}
0 commit comments