Skip to content

Commit

Permalink
Feature/improve modal performance (#10)
Browse files Browse the repository at this point in the history
* make modal single instance

* bump version

* remove unused

* update docs
  • Loading branch information
Ryan Sites committed Jun 3, 2021
1 parent a074508 commit cb1dd7c
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 81 deletions.
4 changes: 2 additions & 2 deletions docs/asset-manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"files": {
"0.97dc7c88.iframe.bundle.js": "./0.97dc7c88.iframe.bundle.js",
"main.js": "./main.def69772.iframe.bundle.js",
"main.js": "./main.c7f46bdf.iframe.bundle.js",
"runtime~main.js": "./runtime~main.806b4e7e.iframe.bundle.js",
"vendors~main.js": "./vendors~main.2b0e6e48.iframe.bundle.js",
"vendors~main.js.map": "./vendors~main.2b0e6e48.iframe.bundle.js.map",
Expand All @@ -19,6 +19,6 @@
"entrypoints": [
"runtime~main.806b4e7e.iframe.bundle.js",
"vendors~main.2b0e6e48.iframe.bundle.js",
"main.def69772.iframe.bundle.js"
"main.c7f46bdf.iframe.bundle.js"
]
}
2 changes: 1 addition & 1 deletion docs/iframe.html
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,4 @@



window['FRAMEWORK_OPTIONS'] = {};</script><script src="runtime~main.806b4e7e.iframe.bundle.js"></script><script src="vendors~main.2b0e6e48.iframe.bundle.js"></script><script src="main.def69772.iframe.bundle.js"></script></body></html>
window['FRAMEWORK_OPTIONS'] = {};</script><script src="runtime~main.806b4e7e.iframe.bundle.js"></script><script src="vendors~main.2b0e6e48.iframe.bundle.js"></script><script src="main.c7f46bdf.iframe.bundle.js"></script></body></html>

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@optum/json-schema-editor",
"version": "2.0.3",
"version": "2.1.0",
"description": "JsonSchema Editor React Control",
"repository": "https://github.com/optum/jsonschema-editor-react",
"license": "Apache 2.0",
Expand All @@ -24,7 +24,7 @@
"build": "microbundle --jsx React.createElement",
"test": "react-scripts test",
"eject": "react-scripts eject",
"storybook": "start-storybook -p 6006 -s docs",
"storybook": "rimraf docs && start-storybook -p 6006 -s docs",
"build-storybook": "rimraf docs && build-storybook --docs -o docs"
},
"eslintConfig": {
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ yarn add @optum/json-schema-editor
| readOnly | boolean | make editor read only | false |
| onSchemaChange | callback (results: string) => void | callback method to capture changes to schema | required (no default) |

## Example
## Usage

```js
import JsonSchemaEditor from "@optum/json-schema-editor";
Expand Down
1 change: 1 addition & 0 deletions src/JsonSchemaEditor/advanced-string/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export const AdvancedString: React.FunctionComponent<AdvancedItemStateProps> = (
<FormLabel mr={2}>Max Length: </FormLabel>
<NumberInput
size="sm"
defaultValue={Number(itemState.maxLength.value)}
onChange={(value: number | string) => {
itemState.maxLength.set(Number(value));
}}
Expand Down
4 changes: 2 additions & 2 deletions src/JsonSchemaEditor/schema-item/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export interface SchemaItemProps extends FlexProps {
parentStateProp: State<JSONSchema7>;
name: string;
isReadOnly: State<boolean>;
showadvanced: (item: State<JSONSchema7>) => void;
showadvanced: (item: string) => void;
}

export const SchemaItem: React.FunctionComponent<SchemaItemProps> = (
Expand Down Expand Up @@ -214,7 +214,7 @@ export const SchemaItem: React.FunctionComponent<SchemaItemProps> = (
icon={<FiSettings />}
aria-label="Advanced Settings"
onClick={() => {
showadvanced(itemState);
showadvanced(name);
}}
/>
</Tooltip>
Expand Down
92 changes: 46 additions & 46 deletions src/JsonSchemaEditor/schema-object/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@ export const SchemaObject: React.FunctionComponent<SchemaObjectProps> = (
localState.isAdvancedOpen.set(false);
};

const showadvanced = (item: State<JSONSchema7>): void => {
const showadvanced = (item: string): void => {
localState.isAdvancedOpen.set(true);
localState.item.set(item);
};

const focusRef = React.createRef<HTMLElement>();

const localState = useState({
isAdvancedOpen: false,
item: "",
});

if (!propertiesOrNull) {
Expand All @@ -56,55 +58,53 @@ export const SchemaObject: React.FunctionComponent<SchemaObjectProps> = (
<div className="object-style">
{propertiesOrNull?.keys?.map((name) => {
return (
<>
<SchemaItem
key={String(name)}
<SchemaItem
key={String(name)}
itemStateProp={
propertiesOrNull.nested(name as string) as State<JSONSchema7>
}
parentStateProp={schema}
name={name as string}
showadvanced={showadvanced}
required={schema.required.value as string[]}
isReadOnly={isReadOnlyState}
/>
);
})}
<Modal
isOpen={localState.isAdvancedOpen.get()}
finalFocusRef={focusRef}
size="lg"
onClose={onCloseAdvanced}
>
<ModalOverlay />
<ModalContent>
<ModalHeader textAlign="center">
Advanced Schema Settings
</ModalHeader>

<ModalBody>
<AdvancedSettings
itemStateProp={
propertiesOrNull.nested(name as string) as State<JSONSchema7>
propertiesOrNull.nested(
localState.item.value as string
) as State<JSONSchema7>
}
parentStateProp={schema}
name={name as string}
showadvanced={showadvanced}
required={schema.required.value as string[]}
isReadOnly={isReadOnlyState}
/>
<Modal
isOpen={localState.isAdvancedOpen.get()}
finalFocusRef={focusRef}
size="lg"
onClose={onCloseAdvanced}
>
<ModalOverlay />
<ModalContent>
<ModalHeader textAlign="center">
Advanced Schema Settings
</ModalHeader>
</ModalBody>

<ModalBody>
<AdvancedSettings
itemStateProp={
propertiesOrNull.nested(
name as string
) as State<JSONSchema7>
}
/>
</ModalBody>

<ModalFooter>
<Button
colorScheme="blue"
variant="ghost"
mr={3}
onClick={onCloseAdvanced}
>
Close
</Button>
</ModalFooter>
</ModalContent>
</Modal>
</>
);
})}
<ModalFooter>
<Button
colorScheme="blue"
variant="ghost"
mr={3}
onClick={onCloseAdvanced}
>
Close
</Button>
</ModalFooter>
</ModalContent>
</Modal>
</div>
);
}
Expand Down
14 changes: 0 additions & 14 deletions src/JsonSchemaEditor/state/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,6 @@ export const defaultSchema = (): JSONSchema7 => {
};
};

const countKeys = (object: any): number => {
if (typeof object !== "object" || object === null) {
return 0;
}

const keys = Object.keys(object);
let sum = keys.length;
for (const key of keys) {
sum += countKeys(object[key]);
}

return sum;
};

const isValidSchema = (schema: JSONSchema7): boolean => {
const isValid = ajv.validateSchema(schema);
return isValid;
Expand Down
24 changes: 12 additions & 12 deletions src/stories/JsonSchemaEditor.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@ import React from "react";

import { Story, Meta } from "@storybook/react";

import JsonSchemaEditor from "../JsonSchemaEditor";
import { SchemaEditorProps } from "../JsonSchemaEditor/JsonSchemaEditor.types";
import JsonSchemaEditor from "..";
import { SchemaEditorProps } from "../JsonSchemaEditor.types";
import { readOnlyData, printIt } from "./helper";

export default {
title: "Example/SchemaEditor",
component: JsonSchemaEditor,
title: "Example/SchemaEditor",
component: JsonSchemaEditor,
} as Meta;

const Template: Story<SchemaEditorProps> = (args) => (
<JsonSchemaEditor {...args} />
<JsonSchemaEditor {...args} />
);

export const NewJsonSchema = Template.bind({});
NewJsonSchema.args = {
onSchemaChange: (r) => {
console.log(r);
},
onSchemaChange: (r) => {
console.log(r);
},
};

export const WithData = Template.bind({});
WithData.args = {
data: readOnlyData,
onSchemaChange: (r) => {
printIt(r);
},
data: readOnlyData,
onSchemaChange: (r) => {
printIt(r);
},
};

0 comments on commit cb1dd7c

Please sign in to comment.