Skip to content

Commit 97a7dc7

Browse files
committed
Address review comments
1 parent b789cf3 commit 97a7dc7

File tree

5 files changed

+28
-51
lines changed

5 files changed

+28
-51
lines changed

packages/core/src/actions/index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,13 @@ export const registerUISchema = (
128128
export interface RemoveUISchemaAction {
129129
type: 'jsonforms/REMOVE_UI_SCHEMA';
130130
tester: UISchemaTester;
131-
uischema: UISchemaElement;
132131
}
133132

134133
export const unregisterUISchema = (
135-
tester: UISchemaTester,
136-
uischema: UISchemaElement
134+
tester: UISchemaTester
137135
): RemoveUISchemaAction => {
138136
return {
139137
type: REMOVE_UI_SCHEMA,
140138
tester,
141-
uischema
142139
};
143140
};

packages/core/src/reducers/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import {
3636
} from './core';
3737
import { JsonFormsState } from '../store';
3838
import { findMatchingUISchema, uischemaRegistryReducer } from './uischemas';
39-
import { JsonSchema, UISchemaElement } from '..';
39+
import { Generate, JsonSchema, UISchemaElement } from '..';
4040

4141
export {
4242
rendererReducer,
@@ -62,7 +62,7 @@ export const findUISchema = state =>
6262
(schema: JsonSchema, schemaPath: string, path: string): UISchemaElement => {
6363
const uiSchema = findMatchingUISchema(state.jsonforms.uischemas)(schema, schemaPath, path);
6464
if (uiSchema === undefined) {
65-
return getUiSchema(state);
65+
return Generate.uiSchema(schema);
6666
}
6767
return uiSchema;
6868
};
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as _ from 'lodash';
22
import { ADD_UI_SCHEMA, REMOVE_UI_SCHEMA } from '../actions';
3-
import { JsonSchema, UISchemaElement } from '..';
3+
import { JsonSchema, NOT_APPLICABLE, UISchemaElement } from '..';
44

55
export type UISchemaTester = (schema: JsonSchema, schemaPath: string, path: string) => number;
66

@@ -14,7 +14,7 @@ export const uischemaRegistryReducer = (
1414
const copy = state.slice();
1515
_.remove(
1616
copy,
17-
entry => entry.tester === action.tester && _.eq(entry.uischema, action.uischema)
17+
entry => entry.tester === action.tester
1818
);
1919
return copy;
2020
default:
@@ -23,11 +23,11 @@ export const uischemaRegistryReducer = (
2323
};
2424

2525
export const findMatchingUISchema =
26-
state =>
27-
(jsonSchema: JsonSchema, schemaPath: string, path: string): UISchemaElement => {
28-
const match = _.find(state, entry => entry.tester(jsonSchema, schemaPath, path));
29-
if (match) {
30-
return match.uischema;
31-
}
32-
return undefined;
33-
};
26+
(state: { tester: UISchemaTester, uischema: UISchemaElement }[]) =>
27+
(jsonSchema: JsonSchema, schemaPath: string, path: string): UISchemaElement => {
28+
const match = _.maxBy(state, entry => entry.tester(jsonSchema, schemaPath, path));
29+
if (match !== undefined && match.tester(jsonSchema, schemaPath, path) !== NOT_APPLICABLE) {
30+
return match.uischema;
31+
}
32+
return undefined;
33+
};

packages/core/test/reducers/uischemas.test.ts

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
UISchemaTester
88
} from '../../src/reducers/uischemas';
99
import { registerUISchema, unregisterUISchema } from '../../src/actions';
10-
import { findUISchema } from '../../src/reducers';
10+
import { findUISchema, getSchema } from '../../src/reducers';
11+
import Generate from '../../src/generators';
1112

1213
test('init state empty', t => {
1314
t.deepEqual(uischemaRegistryReducer(undefined, { type: 'whatever' }), []);
@@ -39,7 +40,7 @@ test('remove ui schema', t => {
3940
uischema: control
4041
}
4142
],
42-
unregisterUISchema(tester, control)
43+
unregisterUISchema(tester)
4344
);
4445
t.is(after.length, 0);
4546
});
@@ -75,47 +76,27 @@ test('findMatchingUISchema', t => {
7576
);
7677
});
7778

78-
test('findUISchema returns default if no matching UI schema has been found', t => {
79-
80-
const testerA: UISchemaTester = (_schema, schemaPath) => _.endsWith(schemaPath, 'foo') ? 1 : 0;
81-
const testerB: UISchemaTester = (_schema, schemaPath) => _.endsWith(schemaPath, 'bar') ? 1 : 0;
82-
const controlA = {
83-
type: 'Control',
84-
scope: '#/definitions/foo'
85-
};
86-
const controlB = {
87-
type: 'Control',
88-
scope: '#/definitions/bar'
89-
};
90-
const before = [
91-
{
92-
tester: testerA,
93-
uischema: controlA
94-
},
95-
{
96-
tester: testerB,
97-
uischema: controlB
98-
}
99-
];
100-
const uischema = {
101-
type: 'Control',
102-
scope: '#/properties/any'
103-
};
104-
79+
test('findUISchema returns generated UI schema if no match has been found', t => {
10580
const middlewares = [];
10681
const mockStore = configureStore(middlewares);
10782
const store = mockStore({
10883
jsonforms: {
10984
core: {
110-
uischema
85+
schema: {
86+
definitions: {
87+
baz: {
88+
type: 'number'
89+
}
90+
}
91+
}
11192
},
112-
uischemas: before
93+
uischemas: []
11394
}
11495
});
11596

11697
t.deepEqual(
117-
findUISchema(store.getState())(undefined, '#/defintions/baz', undefined),
118-
uischema
98+
findUISchema(store.getState())(getSchema(store.getState()), '#/definitions/baz', undefined),
99+
Generate.uiSchema(getSchema(store.getState()))
119100
);
120101
});
121102

packages/example/src/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ store.dispatch(Actions.registerRenderer(ratingControlTester, RatingControl));
4545

4646
store.dispatch(Actions.registerUISchema(
4747
(jsonSchema, schemaPath) => {
48-
49-
return _.has(jsonSchema, 'properties.objectarrayofstrings') ? 9 : NOT_APPLICABLE
48+
return schemaPath === '#/properties/firstarray' ? 2 : NOT_APPLICABLE;
5049
},
5150
{
5251
type: 'VerticalLayout',

0 commit comments

Comments
 (0)