11import { Dictionary , Omit } from '@stoplight/types' ;
22import { Box , Button , IBox } from '@stoplight/ui-kit' ;
33import { JSONSchema4 } from 'json-schema' ;
4+ import _get = require( 'lodash/get' ) ;
45import * as React from 'react' ;
56import { MutedText } from './components/common/MutedText' ;
7+ import { MaskedSchema } from './components/MaskedSchema' ;
68import { IProperty , Property } from './components/Property' ;
79import { TopBar } from './components/TopBar' ;
810import { useMetadata } from './hooks/useMetadata' ;
911import { useProperties } from './hooks/useProperties' ;
1012import { useTheme } from './theme' ;
13+ import { IMasking } from './types' ;
1114import { isExpanded } from './utils/isExpanded' ;
1215import { pathToString } from './utils/pathToString' ;
1316
14- export interface ISchemaView extends Omit < IBox , 'onSelect' > {
17+ export interface ISchemaView extends Omit < IBox , 'onSelect' > , IMasking {
1518 name ?: string ;
1619 defaultExpandedDepth ?: number ;
1720 dereferencedSchema ?: JSONSchema4 ;
@@ -29,17 +32,22 @@ export const SchemaView: React.FunctionComponent<ISchemaView> = props => {
2932 limitPropertyCount,
3033 schema,
3134 dereferencedSchema,
35+ selected,
36+ canSelect,
37+ onSelect,
3238 name,
3339 ...rest
3440 } = props ;
3541
3642 const theme = useTheme ( ) ;
3743 const [ showExtra , setShowExtra ] = React . useState < boolean > ( false ) ;
44+ const [ maskedSchema , setMaskedSchema ] = React . useState < JSONSchema4 | null > ( null ) ;
3845 const [ expandedRows , setExpandedRows ] = React . useState < Dictionary < boolean > > ( { all : expanded } ) ;
3946 const { properties, isOverflow } = useProperties ( schema , dereferencedSchema , {
4047 expandedRows,
4148 defaultExpandedDepth,
4249 ...( ! showExtra && { limitPropertyCount } ) ,
50+ ...( canSelect && selected && { selected } ) ,
4351 } ) ;
4452 const metadata = useMetadata ( schema ) ;
4553
@@ -62,15 +70,34 @@ export const SchemaView: React.FunctionComponent<ISchemaView> = props => {
6270 [ showExtra ]
6371 ) ;
6472
73+ const handleMaskEdit = React . useCallback < IProperty [ 'onMaskEdit' ] > ( node => {
74+ setMaskedSchema ( _get ( dereferencedSchema , node . path ) ) ;
75+ } , [ ] ) ;
76+
77+ const handleMaskedSchemaClose = React . useCallback ( ( ) => {
78+ setMaskedSchema ( null ) ;
79+ } , [ ] ) ;
80+
6581 if ( properties . length === 0 ) {
6682 return < MutedText > { emptyText } </ MutedText > ;
6783 }
6884
6985 return (
7086 < Box backgroundColor = { theme . canvas . bg } color = { theme . canvas . fg } { ...rest } >
87+ { maskedSchema && (
88+ < MaskedSchema onClose = { handleMaskedSchemaClose } onSelect = { onSelect } selected = { selected } schema = { maskedSchema } />
89+ ) }
7190 < TopBar name = { name } metadata = { metadata } />
7291 { properties . map ( ( node , i ) => (
73- < Property key = { i } node = { node } onClick = { toggleExpandRow } />
92+ < Property
93+ key = { i }
94+ node = { node }
95+ onClick = { toggleExpandRow }
96+ onSelect = { onSelect }
97+ onMaskEdit = { handleMaskEdit }
98+ selected = { selected }
99+ canSelect = { canSelect }
100+ />
74101 ) ) }
75102 { showExtra || isOverflow ? (
76103 < Button onClick = { toggleShowExtra } > { showExtra ? 'collapse' : `...show more properties` } </ Button >
0 commit comments