Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check if index of category is out of range #2121

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 176 additions & 0 deletions packages/examples/src/examples/nestedCategorization.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/*
The MIT License

Copyright (c) 2023 EclipseSource Munich
https://github.com/eclipsesource/jsonforms

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the 'Software'), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import { registerExamples } from '../register';

export const schema = {
type: 'object',
properties: {
cat1: {
type: 'object',
properties: {
subcat11: {
type: 'string'
}
}
},
cat2: {
type: 'object',
properties: {
subcat21: {
type: 'string'
},
subcat22: {
type: 'string'
}
}
},
cat3: {
type: 'object',
properties: {
subcat31: {
type: 'string'
},
subcat32: {
type: 'string'
},
subcat33: {
type: 'string'
}
}
}
}
}

export const uischema = {
type: 'Categorization',
elements: [
{
type: 'Category',
label: 'Cat1',
elements: [
{
type: 'Categorization',
elements: [
{
type: 'Category',
label: 'SubCat1-1',
elements: [
{
type: 'Control',
scope: '#/properties/cat1/properties/subcat11'
}
]
}
]
}
]
},
{
type: 'Category',
label: 'Cat2',
elements: [
{
type: 'Categorization',
elements: [
{
type: 'Category',
label: 'SubCat2-1',
elements: [
{
type: 'Control',
scope: '#/properties/cat2/properties/subcat21'
}
]
},
{
type: 'Category',
label: 'SubCat2-2',
elements: [
{
type: 'Control',
scope: '#/properties/cat2/properties/subcat22'
}
]
}
]
}
]
},
,
{
type: 'Category',
label: 'Cat3',
elements: [
{
type: 'Categorization',
elements: [
{
type: 'Category',
label: 'SubCat3-1',
elements: [
{
type: 'Control',
scope: '#/properties/cat3/properties/subcat31'
}
]
},
{
type: 'Category',
label: 'SubCat3-2',
elements: [
{
type: 'Control',
scope: '#/properties/cat3/properties/subcat32'
}
]
},
{
type: 'Category',
label: 'SubCat3-3',
elements: [
{
type: 'Control',
scope: '#/properties/cat3/properties/subcat33'
}
]
}
]
}
]
}
]
};

export const data = {};

registerExamples([
{
name: 'nestedCategorization',
label: 'Nested Categorization',
data,
schema,
uischema
}
]);
2 changes: 2 additions & 0 deletions packages/examples/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import * as anyOfOneOfAllOfResolve from './examples/anyOf-oneOf-allOf-resolve';
import * as array from './examples/arrays';
import * as arrayI18n from './examples/arraysI18n';
import * as nestedArray from './examples/nestedArrays';
import * as nestedCategorization from './examples/nestedCategorization';
import * as arrayWithDetail from './examples/arrays-with-detail';
import * as arrayWithDetailAndRule from './examples/arrays-with-detail-and-rule';
import * as arrayWithCustomChildLabel from './examples/arrays-with-custom-element-label';
Expand Down Expand Up @@ -100,6 +101,7 @@ export {
array,
arrayI18n,
nestedArray,
nestedCategorization,
arrayWithDetail,
arrayWithDetailAndRule,
arrayWithCustomChildLabel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import React, {useState, useMemo} from 'react';
import React, { useState, useMemo } from 'react';
import { AppBar, Hidden, Tab, Tabs } from '@mui/material';
import {
and,
Expand Down Expand Up @@ -92,12 +92,21 @@ export const MaterialCategorizationLayoutRenderer = (props: MaterialCategorizati
t
} = props;
const categorization = uischema as Categorization;
const [activeCategory, setActiveCategory]= useState<number|undefined>(selected??0);
const [previousCategorization, setPreviousCategorization] = useState<Categorization>(uischema as Categorization);
const [activeCategory, setActiveCategory] = useState<number>(selected ?? 0);
const categories = useMemo(() => categorization.elements.filter((category: Category) =>
isVisible(category, data, undefined, ajv)
),[categorization, data, ajv]);
), [categorization, data, ajv]);

if (categorization !== previousCategorization) {
setActiveCategory(0);
setPreviousCategorization(categorization);
}

const safeCategory = activeCategory >= categorization.elements.length ? 0 : activeCategory;
lucas-koehler marked this conversation as resolved.
Show resolved Hide resolved

const childProps: MaterialLayoutRendererProps = {
elements: categories[activeCategory].elements,
elements: categories[safeCategory] ? categories[safeCategory].elements : [],
schema,
path,
direction: 'column',
Expand All @@ -108,31 +117,32 @@ export const MaterialCategorizationLayoutRenderer = (props: MaterialCategorizati
};
const onTabChange = (_event: any, value: any) => {
if (onChange) {
onChange(value, activeCategory);
onChange(value, safeCategory);
}
setActiveCategory(value);
};

const tabLabels = useMemo(() => {
return categories.map((e: Category) =>
return categories.map((e: Category) =>
deriveLabelForUISchemaElement(e, t)
)
}, [categories, t])

return (
<Hidden xsUp={!visible}>
<AppBar position='static'>
<Tabs value={activeCategory} onChange={onTabChange} textColor='inherit' indicatorColor='secondary' variant='scrollable'>
<Tabs value={safeCategory} onChange={onTabChange} textColor='inherit' indicatorColor='secondary' variant='scrollable'>
{categories.map((_, idx: number) => (
<Tab key={idx} label={tabLabels[idx]} />
))}
</Tabs>
</AppBar>
<div style={{ marginTop: '0.5em' }}>
<MaterialLayoutRenderer {...childProps} />
<MaterialLayoutRenderer {...childProps} key={safeCategory} />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason for adding this key? I don't think we need it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it always makes sense to set a new key and reset the state of all components, as we are working on different data once we change the tap. Also, we could run into similar issues with e.g. the detail list or expandable list, where the old state might not fit to the new data and the selected element is out of range.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel it is not strictly necessary because the childProps should change anyway. But we can leave it in as it shouldn't hurt either :D

</div>
</Hidden>
);
};

export default withAjvProps(withTranslateProps(withJsonFormsLayoutProps(MaterialCategorizationLayoutRenderer)));