Skip to content
This repository was archived by the owner on Jun 28, 2024. It is now read-only.

Commit 90e2c9d

Browse files
authored
feat: use new IDiagnostic type [SL-2125] (#45)
BREAKING CHANGES: The diagnostics interface has changed and the `show` now shows fields instead of hides them. * feat: use new IDiagnostic type * chore: storybook v5 * fix: summary should be consumed * chore: remove @emotion/core * feat: less TypeScript-ish * fix: show means show, not hide
1 parent 56f3bd4 commit 90e2c9d

File tree

10 files changed

+2624
-1047
lines changed

10 files changed

+2624
-1047
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,13 @@
4141
},
4242
"dependencies": {
4343
"@stoplight/memoize-one": "4.0.x",
44+
"@stoplight/types": "^4.1.0",
4445
"expression-eval": "1.4.0"
4546
},
4647
"devDependencies": {
4748
"@fortawesome/free-solid-svg-icons": "^5.6.1",
4849
"@stoplight/scripts": "4.0.0",
49-
"@stoplight/storybook-config": "1.1.1",
50+
"@stoplight/storybook-config": "1.4.0",
5051
"@stoplight/ui-kit": "^1.46.0",
5152
"@types/react": "16.8.x",
5253
"@types/react-dom": "16.8.x",

src/@emotion__core.d.ts

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

src/__stories__/decorators.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react';
22

3-
import { boolean, number, select, text } from '@storybook/addon-knobs/react';
3+
import { boolean, select, text } from '@storybook/addon-knobs/react';
44

55
import { ThemeZone } from '../theme';
66

@@ -13,9 +13,9 @@ export const Tooltips = (storyFn: Function) => {
1313
if (path.length > 0) return [];
1414
return [
1515
{
16-
summary: text('message', ''),
17-
severity: number('severity', 40),
18-
severityLabel: select('severityLabel', ['warn', 'anything-other-than-warn'], 'warn'),
16+
message: text('message', ''),
17+
// Will wanted it to be less TypeScript-ish hence numbers are listed
18+
severity: select('severity', [0, 1, 2, 3], 1),
1919
},
2020
];
2121
};
@@ -27,9 +27,9 @@ export const PathTooltips = (storyFn: Function) => {
2727
return boolean('show path tooltips', false)
2828
? [
2929
{
30-
summary: path.join(' > '),
31-
severity: number('severity', 0),
32-
severityLabel: select('severityLabel', ['warn', 'anything-other-than-warn'], 'anything-other-than-warn'),
30+
message: path.join(' > '),
31+
// Will wanted it to be less TypeScript-ish hence numbers are listed
32+
severity: select('severity', [0, 1, 2, 3], 1),
3333
},
3434
]
3535
: [];
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1+
import { JsonPath } from '@stoplight/types';
12
import * as React from 'react';
3+
import { IFormtronDiagnostic } from '../types';
24

3-
export interface IDiagnosticMessage {
4-
severity: number;
5-
severityLabel: string;
6-
summary?: string;
7-
message?: string;
8-
}
9-
10-
export type IDiagnosticMessagesProvider = (path: string[]) => IDiagnosticMessage[];
5+
export type IDiagnosticMessagesProvider = (path: JsonPath) => IFormtronDiagnostic[];
116

127
export const DiagnosticMessagesContext = React.createContext<IDiagnosticMessagesProvider>(() => []);

src/components/Form.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export const Form: React.FunctionComponent<IFormtronControl> = ({
6262
const { show, evalOptions, type } = propSchema;
6363

6464
// if evalutating show is false skip area
65-
if (show && evaluate(show, value, name, true)) {
65+
if (show && !evaluate(show, value, name, true)) {
6666
return;
6767
}
6868

src/components/Messages.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import * as React from 'react';
22

3+
import { JsonPath } from '@stoplight/types';
34
import { Box, Popup } from '@stoplight/ui-kit';
45
import { Tooltip } from '@stoplight/ui-kit/Tooltip';
56

67
import { useDiagnostics } from './hooks';
78
import { Variant } from './types';
89

910
interface IMessages {
10-
path: string[];
11+
path: JsonPath;
1112
}
1213

1314
export const Messages: React.FunctionComponent<IMessages> = ({ path, children }) => {

src/components/__tests__/hooks.spec.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { DiagnosticSeverity } from '@stoplight/types';
12
import * as React from 'react';
23
import { useDiagnostics } from '../hooks/useDiagnostics';
34
import { Variant } from '../types';
@@ -7,14 +8,12 @@ jest.mock('../DiagnosticMessagesContext');
78

89
const getMessages = jest.fn(() => [
910
{
10-
severity: 10,
11-
severityLabel: 'warn',
12-
summary: 'An error happened',
11+
severity: DiagnosticSeverity.Error,
1312
message: 'Cannot foobar undefines',
1413
},
1514
]);
16-
// @ts-ignore
17-
React.useContext.mockImplementation(() => getMessages);
15+
16+
(React.useContext as jest.Mock).mockImplementation(() => getMessages);
1817

1918
describe('useDiagnostics', () => {
2019
it('should determine variant', () => {
@@ -23,9 +22,7 @@ describe('useDiagnostics', () => {
2322
expect(variant).toBe(Variant.invalid);
2423
expect(messages).toEqual([
2524
{
26-
severity: 10,
27-
severityLabel: 'warn',
28-
summary: 'An error happened',
25+
severity: DiagnosticSeverity.Error,
2926
message: 'Cannot foobar undefines',
3027
},
3128
]);

src/components/hooks/useDiagnostics.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
1+
import { DiagnosticSeverity, JsonPath } from '@stoplight/types';
12
import { useContext } from 'react';
23

3-
import { DiagnosticMessagesContext } from '../DiagnosticMessagesContext';
4+
import { IFormtronDiagnostic } from '../../types';
5+
import { DiagnosticMessagesContext, IDiagnosticMessagesProvider } from '../DiagnosticMessagesContext';
46
import { Variant } from '../types';
57

6-
export const useDiagnostics = (path: string[]) => {
7-
const getMessages = useContext(DiagnosticMessagesContext);
8+
export type UseDiagnostics = (
9+
path: JsonPath
10+
) => {
11+
variant: Variant;
12+
messages: IFormtronDiagnostic[];
13+
};
14+
15+
export const useDiagnostics: UseDiagnostics = path => {
16+
const getMessages = useContext<IDiagnosticMessagesProvider>(DiagnosticMessagesContext);
817
const messages = getMessages(path);
9-
let severity = -1;
10-
let severityLabel = '';
11-
for (const message of messages) {
12-
if (message.severity > severity) {
13-
severity = message.severity;
14-
severityLabel = message.severityLabel;
15-
}
16-
}
18+
const severity = Math.min(...messages.map(({ severity }) => severity));
19+
1720
let variant: Variant = Variant.normal;
18-
if (severityLabel === 'warn') {
21+
if (severity === DiagnosticSeverity.Error || severity === DiagnosticSeverity.Warning) {
1922
variant = Variant.invalid;
2023
}
2124
return { variant, messages };

src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { IDiagnostic, IRange, Omit } from '@stoplight/types';
12
import * as React from 'react';
23
import { Dictionary } from 'ts-essentials';
34
import { themeTypes } from './theme';
@@ -23,6 +24,11 @@ export interface IFormtron extends IFormtronCommon {
2324
themeName?: themeTypes;
2425
}
2526

27+
export interface IFormtronDiagnostic extends Omit<IDiagnostic, 'range'> {
28+
summary?: string;
29+
range?: IRange;
30+
}
31+
2632
export interface IAddOperation {
2733
op: 'add';
2834
path: string;

0 commit comments

Comments
 (0)