Skip to content

Commit

Permalink
feat: add args transform parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
lmestel committed Feb 26, 2024
1 parent ce4f5e0 commit 0e73ae3
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 24 deletions.
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,18 @@ export default {
component: Button,
parameters: {
jsonschema: {
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://my-components/button.schema.json",
"type": "object",
"properties": {
"primary": {
"type": "boolean",
"default": false,
},
"label": {
"type": "string"
schema: {
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://my-components/button.schema.json",
"type": "object",
"properties": {
"primary": {
"type": "boolean",
"default": false,
},
"label": {
"type": "string"
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"dependencies": {},
"devDependencies": {
"@kickstartds/core": "^2.2.2",
"@kickstartds/json-schema-viewer": "^2.0.0",
"@kickstartds/json-schema-viewer": "^2.0.1",
"@monaco-editor/react": "^4.5.1",
"@storybook/addon-essentials": "^7.6.17",
"@storybook/components": "^7.6.17",
Expand Down
12 changes: 9 additions & 3 deletions src/components/SchemaEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import React, { useEffect, useRef, useMemo } from "react";
import type { IRange, editor } from "monaco-editor";
import type { Args } from "@storybook/types";
import Editor, { useMonaco, OnMount, OnValidate } from "@monaco-editor/react";
import { JsonSchema } from "@kickstartds/json-schema-viewer";
import { pack, unpack } from "@kickstartds/core/lib/storybook";
import { useArgs } from "@storybook/manager-api";
import decomment from "decomment";

const identity: <T>(v: T) => T = (v) => v;

type SchemaEditorProps = {
schema: JsonSchema;
setValidationResults: (marker: editor.IMarker[]) => void;
selectedValidationRange?: IRange;
fromArgs?: (args: Args) => Record<string, any>;
toArgs?: (obj: Record<string, any>) => Args;
};

const editorPreamble = `
Expand All @@ -22,20 +26,22 @@ export const SchemaEditor: React.FC<SchemaEditorProps> = ({
schema,
setValidationResults,
selectedValidationRange,
fromArgs = identity,
toArgs = identity,
}) => {
const editorRef = useRef<editor.IStandaloneCodeEditor>(null);
const monaco = useMonaco();
const [storybookArgs = {}, updateArgs] = useArgs();

const initialContent = useMemo(() => unpack(storybookArgs), [schema]);
const initialContent = useMemo(() => fromArgs(storybookArgs), [schema]);

const handleEditorDidMount: OnMount = (editor, monaco) => {
editorRef.current = editor;
};

const handleChange = (value: string) => {
try {
updateArgs(pack(JSON.parse(decomment(value))));
updateArgs(toArgs(JSON.parse(decomment(value))));
} catch (e) {}
};

Expand Down
12 changes: 8 additions & 4 deletions src/components/SchemaView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { useParameter } from "@storybook/manager-api";
import styled from "styled-components";
import type { editor, IRange } from "monaco-editor";

import { forSize, JsonSchema } from "@kickstartds/json-schema-viewer";
import { forSize } from "@kickstartds/json-schema-viewer";

import { PARAM_KEY } from "../constants";
import { PARAM_KEY, JsonSchemaParameter } from "../constants";
import { SchemaDoc } from "./SchemaDoc";
import { SchemaEditor } from "./SchemaEditor";

Expand Down Expand Up @@ -43,12 +43,14 @@ const SchemaEditorContainerHeading = styled.h3`
`;

export const SchemaView: React.FC = () => {
const schema = useParameter<JsonSchema>(PARAM_KEY, {});
const { schema, fromArgs, toArgs } = useParameter<JsonSchemaParameter>(
PARAM_KEY,
{ schema: {} }
);
const [validationResults, setValidationResults] = useState<editor.IMarker[]>(
[]
);
const [selectedValidation, setSelectedValidation] = useState<IRange>();

return (
<SchemaContainer>
<SchemaDocContainer>
Expand All @@ -63,6 +65,8 @@ export const SchemaView: React.FC = () => {
schema={schema}
setValidationResults={setValidationResults}
selectedValidationRange={selectedValidation}
fromArgs={fromArgs}
toArgs={toArgs}
/>
<SchemaEditorContainerHeading>
Editor and Validator
Expand Down
8 changes: 8 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import type { Args } from "@storybook/types";
import type { JsonSchema } from "@kickstartds/json-schema-viewer";

export const ADDON_ID = "storybook/jsonschema";
export const PANEL_ID = `${ADDON_ID}/panel`;
export const PARAM_KEY = "jsonschema";
export type JsonSchemaParameter = {
schema: JsonSchema;
fromArgs?: (args: Args) => Record<string, any>;
toArgs?: (obj: Record<string, any>) => Args;
} | undefined;
2 changes: 1 addition & 1 deletion stories/button/Button.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default {
component: Button,
parameters: {
layout: "centered",
jsonschema: schema,
jsonschema: { schema },
},
args: {
label: "Button",
Expand Down
3 changes: 2 additions & 1 deletion stories/header/Header.stories.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
pack,
unpack,
unpackDecorator,
getArgsShared,
} from "@kickstartds/core/lib/storybook";
Expand All @@ -15,7 +16,7 @@ export default {
argTypes,
parameters: {
layout: "fullscreen",
jsonschema: schema,
jsonschema: { schema, toArgs: pack, fromArgs: unpack },
},
decorators: [unpackDecorator],
};
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1677,10 +1677,10 @@
pubsub-js "^1.9.4"
sass "^1.62.1"

"@kickstartds/json-schema-viewer@^2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@kickstartds/json-schema-viewer/-/json-schema-viewer-2.0.0.tgz#0b58a3ddcb53a8efb7df5f5203120b9ccf06d062"
integrity sha512-6lf7Ezp5byL6BIit/2CWWVoiJany2PDqoR9inR7CK8dQw+z3j9z5nKF0Ac8rTjiFofIWqlch2Z6rD9Bx5Vn1Qg==
"@kickstartds/json-schema-viewer@^2.0.1":
version "2.0.1"
resolved "https://registry.yarnpkg.com/@kickstartds/json-schema-viewer/-/json-schema-viewer-2.0.1.tgz#7d73a45bc4ef9c3904c64277d6d613db2ee16221"
integrity sha512-EfatoDe4Gr8qleW+xpbvq/aU2RlyDJb/mQWuG1VjSmwc/1dn86C+DCnsdQ4oYi8e3num8n2aHn87RY8838izZg==

"@mdx-js/react@^2.1.5":
version "2.3.0"
Expand Down

0 comments on commit 0e73ae3

Please sign in to comment.