11/* @jsx jsx */
22
33import { jsx } from '@emotion/core' ;
4+ import { Dictionary , ISchema } from '@stoplight/types' ;
5+ import { Box , Button , IBox } from '@stoplight/ui-kit' ;
46import dropRight = require( 'lodash/dropRight' ) ;
57import isEmpty = require( 'lodash/isEmpty' ) ;
6- import { Fragment , FunctionComponent , MouseEventHandler , ReactNodeArray , useCallback , useState } from 'react' ;
7-
8- import { Dictionary , ISchema } from '@stoplight/types' ;
9- import { Button } from '@stoplight/ui-kit' ;
8+ import { FunctionComponent , MouseEventHandler , ReactNodeArray , useCallback , useState } from 'react' ;
9+ import { MutedText } from './common/MutedText' ;
1010import { dereferenceSchema } from './dereferenceSchema' ;
1111import { renderSchema } from './renderers/renderSchema' ;
12+ import { useTheme } from './theme' ;
1213import { buildAllOfSchema } from './util/buildAllOfSchema' ;
1314
14- export interface ISchemaView {
15+ export interface ISchemaViewProps {
1516 name ?: string ;
1617 dereferencedSchema ?: ISchema ;
1718 defaultExpandedDepth ?: number ;
@@ -21,20 +22,26 @@ export interface ISchemaView {
2122 hideRoot ?: boolean ;
2223 expanded ?: boolean ;
2324 hideInheritedFrom ?: boolean ;
25+ emptyText : string ;
2426}
2527
28+ export interface ISchemaView extends ISchemaViewProps , IBox { }
29+
2630export const SchemaView : FunctionComponent < ISchemaView > = props => {
2731 const {
2832 defaultExpandedDepth = 1 ,
2933 dereferencedSchema,
34+ emptyText,
3035 expanded = false ,
3136 hideInheritedFrom = false ,
3237 hideRoot,
3338 limitPropertyCount = 0 ,
3439 schema,
3540 schemas = { } ,
41+ ...rest
3642 } = props ;
3743
44+ const theme = useTheme ( ) ;
3845 const [ showExtra , setShowExtra ] = useState < boolean > ( false ) ;
3946 const [ expandedRows , setExpandedRows ] = useState < Dictionary < boolean > > ( { all : expanded } ) ;
4047
@@ -56,15 +63,15 @@ export const SchemaView: FunctionComponent<ISchemaView> = props => {
5663 ! Object . keys ( actualSchema ) . length ||
5764 ( actualSchema . properties && ! Object . keys ( actualSchema . properties ) . length )
5865 ) {
59- return null ;
66+ return < MutedText > { emptyText } </ MutedText > ;
6067 }
6168
6269 if ( actualSchema . allOf ) {
63- const props = actualSchema . allOf ;
70+ const schemaProps = actualSchema . allOf ;
6471
65- if ( actualSchema . type ) props . push ( { type : actualSchema . type } ) ;
72+ if ( actualSchema . type ) schemaProps . push ( { type : actualSchema . type } ) ;
6673
67- actualSchema = buildAllOfSchema ( props ) ;
74+ actualSchema = buildAllOfSchema ( schemaProps ) ;
6875 }
6976
7077 let rowElems : ReactNodeArray = [ ] ;
@@ -90,17 +97,17 @@ export const SchemaView: FunctionComponent<ISchemaView> = props => {
9097 }
9198
9299 if ( rowElems . length === 0 ) {
93- return null ;
100+ return < MutedText > { emptyText } </ MutedText > ;
94101 }
95102
96103 return (
97- < Fragment >
104+ < Box backgroundColor = { theme . canvas . bg } color = { theme . canvas . fg } { ... rest } >
98105 { rowElems }
99106 { showExtra || propOverflowCount > 0 ? (
100107 < Button onClick = { toggleShowExtra } >
101108 { showExtra ? 'collapse' : `...show ${ propOverflowCount } more properties` }
102109 </ Button >
103110 ) : null }
104- </ Fragment >
111+ </ Box >
105112 ) ;
106113} ;
0 commit comments