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

feat: improve support for minItems and maxItems for array layout and control #2387

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions packages/core/src/mappers/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,7 @@ export interface ControlWithDetailProps
*/
export interface StatePropsOfArrayControl
extends StatePropsOfControlWithDetail {
arraySchema: JsonSchema;
childErrors?: ErrorObject[];
}

Expand All @@ -811,6 +812,7 @@ export const mapStateToArrayControlProps = (
path,
uischema,
schema: resolvedSchema,
arraySchema: schema,
childErrors,
renderers: ownProps.renderers || getRenderers(state),
cells: ownProps.cells || getCells(state),
Expand Down Expand Up @@ -1139,6 +1141,10 @@ export const mapStateToOneOfProps = (

export interface StatePropsOfArrayLayout extends StatePropsOfControlWithDetail {
data: number;
arraySchema: JsonSchema;
/**
* @deprecated Use `arraySchema.minItems` instead.
*/
minItems?: number;
disableRemove?: boolean;
disableAdd?: boolean;
Expand Down Expand Up @@ -1179,6 +1185,7 @@ export const mapStateToArrayLayoutProps = (
path,
uischema,
schema: resolvedSchema,
arraySchema: schema,
data: props.data ? props.data.length : 0,
errors: allErrors,
minItems: schema.minItems,
Expand Down
120 changes: 119 additions & 1 deletion packages/core/test/mappers/renderer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import {
mapDispatchToControlProps,
mapDispatchToMultiEnumProps,
mapStateToAnyOfProps,
mapStateToArrayControlProps,
mapStateToArrayLayoutProps,
mapStateToControlProps,
mapStateToEnumControlProps,
Expand Down Expand Up @@ -810,6 +811,84 @@ test('mapStateToLayoutProps - visible via state with path from ownProps ', (t) =
t.true(props.visible);
});

test('mapStateToArrayControlProps - should include minItems in array control props', (t) => {
const schema: JsonSchema = {
type: 'array',
minItems: 42,
items: {
type: 'object',
properties: {
message: {
type: 'string',
default: 'foo',
},
},
},
};

const uischema: ControlElement = {
type: 'Control',
scope: '#',
};

const state = {
jsonforms: {
core: {
schema,
data: {},
uischema,
errors: [] as ErrorObject[],
},
},
};

const ownProps = {
uischema,
};

const props = mapStateToArrayControlProps(state, ownProps);
t.is(props.arraySchema.minItems, 42);
});

test('mapStateToArrayControlProps - should include maxItems in array control props', (t) => {
const schema: JsonSchema = {
type: 'array',
maxItems: 42,
items: {
type: 'object',
properties: {
message: {
type: 'string',
default: 'foo',
},
},
},
};

const uischema: ControlElement = {
type: 'Control',
scope: '#',
};

const state = {
jsonforms: {
core: {
schema,
data: {},
uischema,
errors: [] as ErrorObject[],
},
},
};

const ownProps = {
uischema,
};

const props = mapStateToArrayControlProps(state, ownProps);
t.is(props.arraySchema.maxItems, 42);
});

test('mapStateToArrayLayoutProps - should include minItems in array layout props', (t) => {
const schema: JsonSchema = {
type: 'array',
Expand Down Expand Up @@ -846,7 +925,46 @@ test('mapStateToArrayLayoutProps - should include minItems in array layout props
};

const props = mapStateToArrayLayoutProps(state, ownProps);
t.is(props.minItems, 42);
t.is(props.arraySchema.minItems, 42);
});

test('mapStateToArrayLayoutProps - should include maxItems in array layout props', (t) => {
const schema: JsonSchema = {
type: 'array',
maxItems: 42,
items: {
type: 'object',
properties: {
message: {
type: 'string',
default: 'foo',
},
},
},
};

const uischema: ControlElement = {
type: 'Control',
scope: '#',
};

const state = {
jsonforms: {
core: {
schema,
data: {},
uischema,
errors: [] as ErrorObject[],
},
},
};

const ownProps = {
uischema,
};

const props = mapStateToArrayLayoutProps(state, ownProps);
t.is(props.arraySchema.maxItems, 42);
});

test('mapStateToLayoutProps should return renderers prop via ownProps', (t) => {
Expand Down