Skip to content

Commit 7126012

Browse files
lottamusP0lip
authored andcommitted
fix: align required, validations, and button
1 parent 2c722a2 commit 7126012

File tree

10 files changed

+55
-147
lines changed

10 files changed

+55
-147
lines changed

src/components/DetailDialog.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { SchemaNodeWithMeta } from '../types';
1212
import { isCombiner, isRef } from '../utils';
1313
import { Types } from './';
1414

15-
export interface IDetailDialog extends React.HTMLAttributes<HTMLDivElement> {
15+
export interface IDetailDialog {
1616
treeStore: TreeStore;
1717
}
1818

src/components/Divider.tsx

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/components/JsonSchemaViewer.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import { isSchemaViewerEmpty, renderSchema } from '../utils';
88
import { ISchemaTree, SchemaTree } from './SchemaTree';
99

1010
export interface IJsonSchemaViewer extends ErrorBoundaryProps, Omit<ISchemaTree, 'treeStore'> {
11+
style?: object;
1112
emptyText?: string;
1213
defaultExpandedDepth?: number;
14+
expanded?: boolean;
1315
}
1416

1517
class JsonSchemaViewerComponent extends React.PureComponent<IJsonSchemaViewer> {

src/components/MaskedSchema.tsx

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/components/SchemaRow.tsx

Lines changed: 23 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,23 @@
11
import { IRowRendererOptions, ITreeListNode, TreeStore } from '@stoplight/tree-list';
2-
import { Omit } from '@stoplight/types';
3-
import { Button, Checkbox, Colors, Icon } from '@stoplight/ui-kit';
4-
import * as cn from 'classnames';
2+
import { Button, Colors, Icon } from '@stoplight/ui-kit';
53
import * as pluralize from 'pluralize';
64
import * as React from 'react';
75

86
import { IMasking, SchemaNodeWithMeta } from '../types';
9-
import { formatRef, isCombiner, isRef, pathToString } from '../utils';
10-
import { Divider, Types } from './';
7+
import { formatRef, isCombiner, isRef } from '../utils';
8+
import { Types } from './';
119

12-
export interface ISchemaRow extends Omit<React.HTMLAttributes<HTMLDivElement>, 'onClick' | 'onSelect'>, IMasking {
10+
export interface ISchemaRow extends IMasking {
1311
node: ITreeListNode<object>;
1412
rowOptions: IRowRendererOptions;
15-
onMaskEdit(node: SchemaNodeWithMeta): void;
1613
treeStore: TreeStore;
1714
}
1815

1916
const ICON_SIZE = 12;
2017

21-
export const SchemaRow: React.FunctionComponent<ISchemaRow> = ({
22-
node,
23-
treeStore,
24-
canSelect,
25-
onSelect,
26-
onMaskEdit,
27-
selected,
28-
}) => {
18+
export const SchemaRow: React.FunctionComponent<ISchemaRow> = ({ node, treeStore }) => {
2919
const schemaNode = node.metadata as SchemaNodeWithMeta;
30-
const { showDivider, name, $ref, subtype, required, path, inheritedFrom } = schemaNode;
31-
32-
const handleChange = React.useCallback(
33-
() => {
34-
if (onSelect !== undefined) {
35-
onSelect(pathToString(path));
36-
}
37-
},
38-
[onSelect]
39-
);
40-
41-
const handleEditMask = React.useCallback<React.MouseEventHandler<HTMLButtonElement>>(
42-
e => {
43-
e.stopPropagation();
44-
onMaskEdit(schemaNode);
45-
},
46-
[onMaskEdit]
47-
);
20+
const { showDivider, name, $ref, subtype, required, inheritedFrom } = schemaNode;
4821

4922
const handleButtonClick = React.useCallback((e: React.MouseEvent) => {
5023
e.stopPropagation();
@@ -82,50 +55,40 @@ export const SchemaRow: React.FunctionComponent<ISchemaRow> = ({
8255
/>
8356
)}
8457

85-
{showDivider && <Divider>or</Divider>}
58+
{showDivider && (
59+
<div className="flex items-center w-full h-2 absolute" style={{ top: -16, left: -16 }}>
60+
<div className="font-bold text-darken-7 pr-2">OR</div>
61+
<div className="flex-1 bg-darken-5" style={{ height: 2 }} />
62+
</div>
63+
)}
8664

8765
<div className="flex-1 truncate">
8866
<div className="flex items-baseline">
89-
{name && <span className="mr-3">{name}</span>}
67+
{name && <span>{name}</span>}
9068

91-
<Types type={type} subtype={subtype}>
69+
<Types className="ml-2" type={type} subtype={subtype}>
9270
{type === '$ref' ? `[${$ref}]` : null}
9371
</Types>
9472

95-
{inheritedFrom ? (
96-
<>
97-
<span className="text-darken-7 mx-2">{`{${formatRef(inheritedFrom)}}`}</span>
98-
{onMaskEdit !== undefined && <span onClick={handleEditMask}>(edit mask)</span>}
99-
</>
100-
) : null}
73+
{inheritedFrom && <span className="text-darken-7 ml-2">{`{${formatRef(inheritedFrom)}}`}</span>}
10174
</div>
10275

10376
{description && <span className="text-darken-7 text-xs">{description}</span>}
10477
</div>
10578

106-
{(canSelect || validationCount || required) && (
107-
<div className="items-center text-right ml-auto text-xs">
108-
{canSelect ? (
109-
<Checkbox onChange={handleChange} checked={selected && selected.includes(pathToString(path))} />
110-
) : (
111-
<>
112-
{validationCount ? (
113-
<span className="mr-2 text-darken-7">
114-
{validationCount} {pluralize('validation', validationCount)}
115-
</span>
116-
) : null}
117-
118-
{required && <span className="font-semibold">required</span>}
119-
</>
120-
)}
79+
{validationCount > 0 && (
80+
<div className="ml-2 text-darken-7 text-xs">
81+
{validationCount} {pluralize('validation', validationCount)}
12182
</div>
12283
)}
12384

124-
{(validationCount || description) &&
125-
node.canHaveChildren && (
85+
{required && <div className="ml-2 font-semibold text-xs">required</div>}
86+
87+
{node.canHaveChildren &&
88+
(validationCount || description) && (
12689
<Button
12790
small
128-
className={cn(required && 'ml-2')}
91+
className="ml-2"
12992
id={`${node.id}-showMore`}
13093
icon={<Icon icon="info-sign" className="opacity-75" iconSize={ICON_SIZE} />}
13194
onClick={handleButtonClick}

src/components/SchemaTree.tsx

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,33 @@
11
import { TreeList, TreeListEvents, TreeStore } from '@stoplight/tree-list';
2-
import { Omit } from '@stoplight/types';
32

43
import * as cn from 'classnames';
54
import { JSONSchema4 } from 'json-schema';
65
import { observer } from 'mobx-react-lite';
76
import * as React from 'react';
87

8+
import { Dialog } from '@blueprintjs/core';
99
import { IMasking } from '../types';
10-
import { lookupRef } from '../utils';
11-
import { DetailDialog, ISchemaRow, MaskedSchema, SchemaRow, TopBar } from './';
10+
import { DetailDialog, SchemaRow } from './';
11+
import { JsonSchemaViewer } from './JsonSchemaViewer';
1212

1313
const ROW_HEIGHT = 40;
1414
const canDrag = () => false;
1515

16-
export interface ISchemaTree extends Omit<React.HTMLAttributes<HTMLDivElement>, 'onSelect' | 'onError'>, IMasking {
16+
export interface ISchemaTree extends IMasking {
17+
treeStore: TreeStore;
18+
schema: JSONSchema4;
19+
className?: string;
1720
name?: string;
1821
dereferencedSchema?: JSONSchema4;
19-
schema: JSONSchema4;
20-
expanded?: boolean;
2122
hideTopBar?: boolean;
22-
treeStore: TreeStore;
23+
expanded?: boolean;
2324
}
2425

2526
export const SchemaTree = observer<ISchemaTree>(props => {
26-
const {
27-
expanded = false,
28-
schema,
29-
dereferencedSchema,
30-
hideTopBar,
31-
selected,
32-
canSelect,
33-
onSelect,
34-
name,
35-
treeStore,
36-
className,
37-
...rest
38-
} = props;
27+
const { hideTopBar, selected, name, treeStore, className } = props;
3928

4029
const [maskedSchema, setMaskedSchema] = React.useState<JSONSchema4 | null>(null);
4130

42-
const handleMaskEdit = React.useCallback<ISchemaRow['onMaskEdit']>(
43-
node => {
44-
setMaskedSchema(lookupRef(node.path, dereferencedSchema));
45-
},
46-
[dereferencedSchema]
47-
);
48-
4931
treeStore.on(TreeListEvents.NodeClick, (e, node) => {
5032
if (node.level === 0) return; // Don't allow collapsing the root
5133

@@ -59,10 +41,7 @@ export const SchemaTree = observer<ISchemaTree>(props => {
5941
const handleMaskedSchemaClose = React.useCallback(() => setMaskedSchema(null), []);
6042

6143
const itemData = {
62-
onSelect,
63-
onMaskEdit: handleMaskEdit,
6444
selected,
65-
canSelect,
6645
treeStore,
6746
count: treeStore.nodes.length,
6847
};
@@ -73,12 +52,19 @@ export const SchemaTree = observer<ISchemaTree>(props => {
7352
);
7453

7554
return (
76-
<div className={cn(className, 'flex flex-col h-full w-full')} {...rest}>
55+
<div className={cn(className, 'flex flex-col h-full w-full')}>
7756
{maskedSchema && (
78-
<MaskedSchema onClose={handleMaskedSchemaClose} onSelect={onSelect} selected={selected} schema={maskedSchema} />
57+
<Dialog isOpen onClose={handleMaskedSchemaClose}>
58+
<JsonSchemaViewer selected={selected} schema={maskedSchema} style={{ height: 500 }} />
59+
</Dialog>
7960
)}
8061

81-
{!hideTopBar && <TopBar name={name} height={ROW_HEIGHT} />}
62+
{name &&
63+
!hideTopBar && (
64+
<div className="flex items-center text-sm px-6 font-semibold" style={{ height: ROW_HEIGHT }}>
65+
{name}
66+
</div>
67+
)}
8268

8369
<DetailDialog treeStore={treeStore} />
8470

src/components/TopBar.tsx

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/components/Type.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import { ITreeNodeMeta, JSONSchema4CombinerName } from '../types';
77
export interface IType {
88
type: JSONSchema4TypeName | JSONSchema4CombinerName | 'binary' | '$ref';
99
subtype?: ITreeNodeMeta['subtype'];
10+
className?: string;
1011
}
1112

12-
export const Type: React.FunctionComponent<IType> = ({ children, type, subtype }) => {
13+
export const Type: React.FunctionComponent<IType> = ({ className, children, type, subtype }) => {
1314
return (
14-
<span className={cn(PropertyTypeColors[type])}>
15+
<span className={cn(className, PropertyTypeColors[type])}>
1516
{type === 'array' && subtype && subtype !== 'array' ? `array[${subtype}]` : type}
17+
1618
{children}
1719
</span>
1820
);

src/components/Types.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
1+
import cn from 'classnames';
12
import { JSONSchema4TypeName } from 'json-schema';
23
import * as React from 'react';
34
import { ITreeNodeMeta, JSONSchema4CombinerName } from '../types';
4-
import { MutedText } from './common/MutedText';
55
import { Type } from './Type';
66

77
interface ITypes {
8+
className?: string;
89
type?: JSONSchema4TypeName | JSONSchema4TypeName[] | JSONSchema4CombinerName | '$ref';
910
subtype?: ITreeNodeMeta['subtype'];
1011
}
1112

12-
export const Types: React.FunctionComponent<ITypes> = ({ type, subtype }) => {
13+
export const Types: React.FunctionComponent<ITypes> = ({ className, type, subtype, children }) => {
1314
if (!type) return null;
1415

1516
if (!Array.isArray(type)) {
16-
return <Type type={type} subtype={subtype} />;
17+
return <Type className={className} type={type} subtype={subtype} children={children} />;
1718
}
1819

1920
return (
20-
<div className="truncate">
21+
<div className={cn(className, 'truncate')}>
2122
<>
2223
{type.map((name, i, { length }) => (
2324
<React.Fragment key={i}>
2425
<Type key={i} type={name} subtype={subtype} />
26+
2527
{i < length - 1 && (
2628
<span key={`${i}-sep`} className="text-darken-7">
2729
{' or '}

src/components/index.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
export * from './Divider';
21
export * from './JsonSchemaViewer';
3-
export * from './MaskedSchema';
42
export * from './SchemaRow';
53
export * from './SchemaTree';
6-
export * from './TopBar';
74
export * from './Type';
85
export * from './Types';
96
export * from './DetailDialog';

0 commit comments

Comments
 (0)