Skip to content

Commit

Permalink
MaterialRadioGroupControl not used
Browse files Browse the repository at this point in the history
- increase ranking of tester (above that of
MaterialAutocompleteEnumControl)
- use radio group only when the 'radio' format is present in the UI
schema
- add radio group example
- add tests
  • Loading branch information
AlexandraBuzila authored and sdirix committed Oct 7, 2020
1 parent fac1ef6 commit bdd934f
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 15 deletions.
4 changes: 3 additions & 1 deletion packages/examples/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import * as huge from './huge';
import * as defaultExample from './default';
import * as onChange from './onChange';
import * as enumExample from './enum';
import * as radioGroupExample from './radioGroup';
export * from './register';
export * from './example';

Expand Down Expand Up @@ -120,5 +121,6 @@ export {
huge,
ifThenElse,
onChange,
enumExample
enumExample,
radioGroupExample
};
56 changes: 56 additions & 0 deletions packages/examples/src/radioGroup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
The MIT License
Copyright (c) 2017-2019 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';

const data = {
};

const schema = {
type: 'object',
properties: {
exampleRadioEnum: {
type: 'string',
enum: ['One', 'Two', 'Three']
}
}
};

const uischema = {
type: 'Control',
scope: '#/properties/exampleRadioEnum',
options: {
format: 'radio'
}
};

registerExamples([
{
name: 'radio-group',
label: 'Radio Group',
data,
schema,
uischema
}
]);
6 changes: 3 additions & 3 deletions packages/material/src/controls/MaterialRadioGroupControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
isPlainLabel,
rankWith,
RankedTester,
optionIs
optionIs, and, isEnumControl
} from '@jsonforms/core';
import { Control, withJsonFormsControlProps } from '@jsonforms/react';
import Radio from '@material-ui/core/Radio';
Expand Down Expand Up @@ -118,7 +118,7 @@ export class MaterialRadioGroupControl extends Control<
}

export const materialRadioGroupControlTester: RankedTester = rankWith(
2,
optionIs('format', 'radio')
20,
and(isEnumControl, optionIs('format', 'radio'))
);
export default withJsonFormsControlProps(MaterialRadioGroupControl);
34 changes: 23 additions & 11 deletions packages/material/test/renderers/MaterialRadioGroupControl.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@ import * as React from 'react';
import {
Actions,
ControlElement,
isEnumControl,
jsonformsReducer,
JsonFormsState,
JsonSchema,
rankWith,
NOT_APPLICABLE,
UISchemaElement,
update
} from '@jsonforms/core';
import MaterialRadioGroupControl from '../../src/controls/MaterialRadioGroupControl';
import MaterialRadioGroupControl, { materialRadioGroupControlTester } from '../../src/controls/MaterialRadioGroupControl';
import { Provider } from 'react-redux';
import { materialRenderers } from '../../src';
import { AnyAction, combineReducers, createStore, Reducer, Store } from 'redux';
Expand All @@ -56,7 +55,10 @@ const schema = {
};
const uischema: ControlElement = {
type: 'Control',
scope: '#/properties/foo'
scope: '#/properties/foo',
options: {
format: 'radio'
}
};

const initJsonFormsStore = (
Expand All @@ -66,13 +68,7 @@ const initJsonFormsStore = (
): Store<JsonFormsState> => {
const s: JsonFormsState = {
jsonforms: {
renderers: [
...materialRenderers,
{
tester: rankWith(10, isEnumControl),
renderer: MaterialRadioGroupControl
}
]
renderers: materialRenderers
}
};
const reducer: Reducer<JsonFormsState, AnyAction> = combineReducers({
Expand All @@ -83,6 +79,22 @@ const initJsonFormsStore = (
return store;
};

describe('Material radio group tester', () => {
it('should return valid rank for enums with radio format', () => {
const rank = materialRadioGroupControlTester(uischema, schema);
expect(rank).not.toBe(NOT_APPLICABLE);
});

it('should return NOT_APPLICABLE for enums without radio format', () => {
const uiSchemaNoRadio = {
type: 'Control',
scope: '#/properties/foo'
};
const rank = materialRadioGroupControlTester(uiSchemaNoRadio, schema);
expect(rank).toBe(NOT_APPLICABLE);
});
});

describe('Material radio group control', () => {
let wrapper: ReactWrapper;

Expand Down

0 comments on commit bdd934f

Please sign in to comment.