diff --git a/library/src/components/Schema.tsx b/library/src/components/Schema.tsx index e677374a1..2de7cbe82 100644 --- a/library/src/components/Schema.tsx +++ b/library/src/components/Schema.tsx @@ -14,6 +14,7 @@ interface Props { dependentRequired?: string[]; expanded?: boolean; onlyTitle?: boolean; + isArray?: boolean; } const SchemaContext = React.createContext({ @@ -31,17 +32,22 @@ export const Schema: React.FunctionComponent = ({ dependentRequired, expanded: propExpanded = false, onlyTitle = false, + isArray = false, }) => { const { reverse, deepExpanded } = useContext(SchemaContext); - const [expanded, setExpanded] = useState(propExpanded); + const [expanded, setExpanded] = useState(propExpanded || isArray); const [deepExpand, setDeepExpand] = useState(false); useEffect(() => { - setDeepExpand(deepExpanded); + if (!isArray) { + setDeepExpand(deepExpanded); + } }, [deepExpanded, setDeepExpand]); useEffect(() => { - setExpanded(deepExpand); + if (!isArray) { + setExpanded(deepExpand); + } }, [deepExpand, setExpanded]); if ( @@ -88,7 +94,7 @@ export const Schema: React.FunctionComponent = ({
- {isExpandable && !isCircular ? ( + {isExpandable && !isCircular && !isArray ? ( <> setExpanded(prev => !prev)} @@ -270,7 +276,7 @@ export const Schema: React.FunctionComponent = ({ reverse ? 'bg-gray-200' : '' } ${expanded ? 'block' : 'hidden'}`} > - + {schema.oneOf() && @@ -368,10 +374,12 @@ export const Schema: React.FunctionComponent = ({ interface SchemaPropertiesProps { schema: SchemaInterface; + isArray: boolean; } const SchemaProperties: React.FunctionComponent = ({ schema, + isArray, }) => { const properties = schema.properties(); if (properties === undefined || !Object.keys(properties)) { @@ -471,17 +479,22 @@ const SchemaItems: React.FunctionComponent = ({ schema }) => { !Array.isArray(items) && Object.keys(items.properties() || {}).length ) { - return ; + return ; } else if (Array.isArray(items)) { return ( <> {items.map((item, idx) => ( - + ))} ); } - return ; + return ; }; interface AdditionalItemsProps { diff --git a/library/src/components/__tests__/Schema.test.tsx b/library/src/components/__tests__/Schema.test.tsx index b903d46ce..2c840fd9b 100644 --- a/library/src/components/__tests__/Schema.test.tsx +++ b/library/src/components/__tests__/Schema.test.tsx @@ -93,4 +93,60 @@ describe('Schema component', () => { expect(screen.getByText('false')).toBeDefined(); }); }); + + describe('should render arrays', () => { + test('which includes oneOf', async () => { + const schemaModel = new SchemaModel({ + title: 'Sets', + type: 'array', + items: { + title: 'Set', + type: 'object', + properties: { + pets: { + title: 'Pets', + type: 'array', + items: { + title: 'Pet', + type: 'object', + discriminator: 'type', + properties: { + type: { + title: 'Pet.Type', + type: 'string', + enum: ['CAT', 'DOG'], + }, + }, + oneOf: [ + { + title: 'Cat', + type: 'object', + properties: { + type: { + const: 'CAT', + }, + }, + }, + { + title: 'Dog', + type: 'object', + properties: { + type: { + const: 'DOG', + }, + }, + }, + ], + }, + }, + }, + }, + }); + + render(); + + expect(screen.getByText('Adheres to Cat:')).toBeDefined(); + expect(screen.getByText('Or to Dog:')).toBeDefined(); + }); + }); });