Skip to content

Commit 364a7d0

Browse files
eneufeldedgarmueller
authored andcommitted
Review Fixes
1 parent f9dd21f commit 364a7d0

File tree

4 files changed

+35
-39
lines changed

4 files changed

+35
-39
lines changed

packages/material/src/controls/MaterialSimpleAnyOfControl.tsx renamed to packages/material/src/controls/MaterialAnyOfStringOrEnumControl.tsx

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ import React from 'react';
4444
import { connect } from 'react-redux';
4545
import { MaterialInputControl } from './MaterialInputControl';
4646

47+
const findEnumSchema = (schemas: JsonSchema[]) =>
48+
schemas.find(
49+
s => s.enum !== undefined && (s.type === 'string' || s.type === undefined)
50+
);
51+
const findTextSchema = (schemas: JsonSchema[]) =>
52+
schemas.find(s => s.type === 'string' && s.enum === undefined);
53+
4754
const MuiAutocompleteInputText = (props: EnumCellProps & WithClassname) => {
4855
const {
4956
data,
@@ -57,16 +64,9 @@ const MuiAutocompleteInputText = (props: EnumCellProps & WithClassname) => {
5764
handleChange,
5865
schema
5966
} = props;
60-
61-
const findEnumSchema = (schemas: JsonSchema[]) =>
62-
schemas.find(
63-
s => s.enum !== undefined && (s.type === 'string' || s.type === undefined)
64-
);
65-
const findTextSchema = (schemas: JsonSchema[]) =>
66-
schemas.find(s => s.type === 'string' && s.enum === undefined);
6767
const enumSchema = findEnumSchema(schema.anyOf);
68-
const textSchema = findTextSchema(schema.anyOf);
69-
const maxLength = textSchema.maxLength;
68+
const stringSchema = findTextSchema(schema.anyOf);
69+
const maxLength = stringSchema.maxLength;
7070
const mergedConfig = merge({}, config, uischema.options);
7171
let inputProps: InputBaseComponentProps = {};
7272
if (mergedConfig.restrict) {
@@ -102,7 +102,7 @@ const MuiAutocompleteInputText = (props: EnumCellProps & WithClassname) => {
102102
);
103103
};
104104

105-
export class MaterialSimpleAnyOfControl extends Control<ControlProps, ControlState> {
105+
export class MaterialAnyOfStringOrEnumControl extends Control<ControlProps, ControlState> {
106106
render() {
107107
return (
108108
<MaterialInputControl {...this.props} input={MuiAutocompleteInputText} />
@@ -111,29 +111,25 @@ export class MaterialSimpleAnyOfControl extends Control<ControlProps, ControlSta
111111
}
112112
const hasEnumAndText = (schemas: JsonSchema[]) => {
113113
// idea: map to type,enum and check that all types are string and at least one item is of type enum,
114-
const enumSchema = schemas.find(
115-
s => s.enum !== undefined && (s.type === 'string' || s.type === undefined)
116-
);
117-
const textSchema = schemas.find(
118-
s => s.type === 'string' && s.enum === undefined
119-
);
114+
const enumSchema = findEnumSchema(schemas);
115+
const stringSchema = findTextSchema(schemas);
120116
const remainingSchemas = schemas.filter(
121-
s => s !== enumSchema || s !== textSchema
117+
s => s !== enumSchema || s !== stringSchema
122118
);
123119
const wrongType = remainingSchemas.find(s => s.type && s.type !== 'string');
124-
return enumSchema && textSchema && !wrongType;
120+
return enumSchema && stringSchema && !wrongType;
125121
};
126122
const simpleAnyOf = and(
127123
uiTypeIs('Control'),
128124
schemaMatches(
129125
schema => schema.hasOwnProperty('anyOf') && hasEnumAndText(schema.anyOf)
130126
)
131127
);
132-
export const materialSimpleAnyOfControlTester: RankedTester = rankWith(
128+
export const materialAnyOfStringOrEnumControlTester: RankedTester = rankWith(
133129
5,
134130
simpleAnyOf
135131
);
136132
export default connect(
137133
mapStateToControlProps,
138134
mapDispatchToControlProps
139-
)(MaterialSimpleAnyOfControl);
135+
)(MaterialAnyOfStringOrEnumControl);

packages/material/src/controls/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ import MaterialTextControl, {
5353
materialTextControlTester
5454
} from './MaterialTextControl';
5555

56-
import MaterialSimpleAnyOfControl, {
57-
materialSimpleAnyOfControlTester
58-
} from './MaterialSimpleAnyOfControl';
56+
import MaterialAnyOfStringOrEnumControl, {
57+
materialAnyOfStringOrEnumControlTester
58+
} from './MaterialAnyOfStringOrEnumControl';
5959

6060
export {
6161
MaterialBooleanControl,
@@ -78,6 +78,6 @@ export {
7878
materialNumberControlTester,
7979
MaterialTextControl,
8080
materialTextControlTester,
81-
MaterialSimpleAnyOfControl,
82-
materialSimpleAnyOfControlTester
81+
MaterialAnyOfStringOrEnumControl,
82+
materialAnyOfStringOrEnumControlTester
8383
};

packages/material/src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ import {
4545
materialListWithDetailTester
4646
} from './additional';
4747
import {
48+
MaterialAnyOfStringOrEnumControl,
49+
materialAnyOfStringOrEnumControlTester,
4850
MaterialBooleanControl,
4951
materialBooleanControlTester,
5052
MaterialDateControl,
@@ -61,8 +63,6 @@ import {
6163
materialNumberControlTester,
6264
MaterialRadioGroupControl,
6365
materialRadioGroupControlTester,
64-
MaterialSimpleAnyOfControl,
65-
materialSimpleAnyOfControlTester,
6666
MaterialSliderControl,
6767
materialSliderControlTester,
6868
MaterialTextControl,
@@ -155,8 +155,8 @@ export const materialRenderers: JsonFormsRendererRegistryEntry[] = [
155155
renderer: MaterialListWithDetailRenderer
156156
},
157157
{
158-
tester: materialSimpleAnyOfControlTester,
159-
renderer: MaterialSimpleAnyOfControl
158+
tester: materialAnyOfStringOrEnumControlTester,
159+
renderer: MaterialAnyOfStringOrEnumControl
160160
}
161161
];
162162

packages/material/test/renderers/MaterialSimpleAnyOfRenderer.test.tsx renamed to packages/material/test/renderers/MaterialAnyOfStringOrEnumControl.test.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import {
3636
UISchemaElement
3737
} from '@jsonforms/core';
3838
import {
39-
materialRenderers, MaterialSimpleAnyOfControl, materialSimpleAnyOfControlTester
39+
MaterialAnyOfStringOrEnumControl, materialAnyOfStringOrEnumControlTester, materialRenderers
4040
} from '../../src';
4141
import { combineReducers, createStore, Store } from 'redux';
4242

@@ -53,8 +53,8 @@ const uischema: ControlElement = {
5353

5454
describe('Material simple any of control tester', () => {
5555
it('should only be applicable for simple any of cases', () => {
56-
expect(materialSimpleAnyOfControlTester({ type: 'Foo' }, schema)).toBe(-1);
57-
expect(materialSimpleAnyOfControlTester(uischema, schema)).toBe(5);
56+
expect(materialAnyOfStringOrEnumControlTester({ type: 'Foo' }, schema)).toBe(-1);
57+
expect(materialAnyOfStringOrEnumControlTester(uischema, schema)).toBe(5);
5858

5959
const nestedSchema: JsonSchema = {
6060
properties: {
@@ -65,7 +65,7 @@ describe('Material simple any of control tester', () => {
6565
type: 'Control',
6666
scope: '#/properties/foo'
6767
};
68-
expect(materialSimpleAnyOfControlTester(nestedUischema, nestedSchema)).toBe(
68+
expect(materialAnyOfStringOrEnumControlTester(nestedUischema, nestedSchema)).toBe(
6969
5
7070
);
7171
const schemaNoEnum: JsonSchema = {
@@ -80,14 +80,14 @@ describe('Material simple any of control tester', () => {
8080
const schemaNoString: JsonSchema = {
8181
anyOf: [{ type: 'integer' }, { enum: [1, 2] }]
8282
};
83-
expect(materialSimpleAnyOfControlTester(uischema, schemaNoEnum)).toBe(-1);
83+
expect(materialAnyOfStringOrEnumControlTester(uischema, schemaNoEnum)).toBe(-1);
8484
expect(
85-
materialSimpleAnyOfControlTester(uischema, schemaConflictTypes)
85+
materialAnyOfStringOrEnumControlTester(uischema, schemaConflictTypes)
8686
).toBe(-1);
8787
expect(
88-
materialSimpleAnyOfControlTester(uischema, schemaAdditionalProps)
88+
materialAnyOfStringOrEnumControlTester(uischema, schemaAdditionalProps)
8989
).toBe(5);
90-
expect(materialSimpleAnyOfControlTester(uischema, schemaNoString)).toBe(-1);
90+
expect(materialAnyOfStringOrEnumControlTester(uischema, schemaNoString)).toBe(-1);
9191
});
9292
});
9393

@@ -109,7 +109,7 @@ const initJsonFormsStore = (
109109
return store;
110110
};
111111

112-
describe('Material input control', () => {
112+
describe('Material any of string or enum control', () => {
113113
let wrapper: ReactWrapper;
114114

115115
afterEach(() => {
@@ -120,7 +120,7 @@ describe('Material input control', () => {
120120
const store = initJsonFormsStore('foo', schema, uischema);
121121
wrapper = mount(
122122
<Provider store={store}>
123-
<MaterialSimpleAnyOfControl schema={schema} uischema={uischema} />
123+
<MaterialAnyOfStringOrEnumControl schema={schema} uischema={uischema} />
124124
</Provider>
125125
);
126126
const inputs = wrapper.find('input');

0 commit comments

Comments
 (0)