Skip to content

Allow overriding options in EnumField #1002

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

Merged
merged 1 commit into from
Jun 12, 2018
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
37 changes: 33 additions & 4 deletions packages/core/src/util/field.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/*
The MIT License

Copyright (c) 2018 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
Expand Down Expand Up @@ -47,6 +47,20 @@ export interface StatePropsOfField extends StatePropsOfScopedRenderer {
isValid: boolean;
}

/**
* State props of a field for enum field
*/
export interface StatePropsOfEnumField extends StatePropsOfField {
options: any;
}

/**
* Props of an enum field.
*/
export interface EnumFieldProps extends StatePropsOfEnumField, DispatchPropsOfControl {

}

/**
* Props of a field.
*/
Expand Down Expand Up @@ -102,6 +116,21 @@ export const mapStateToFieldProps = (state, ownProps): StatePropsOfField => {
};
};

/**
* Default mapStateToFieldProps for enum field. Options is used for populating dropdown list
* @param state
* @param ownProps
* @returns {StatePropsOfEnumField}
*/
export const defaultMapStateToEnumFieldProps = (state, ownProps): StatePropsOfEnumField => {
const props = mapStateToFieldProps(state, ownProps);

return {
...props,
options: ownProps.options !== undefined ? ownProps.options : props.scopedSchema.enum
};
};

/**
* Synonym for mapDispatchToControlProps.
*
Expand Down
43 changes: 37 additions & 6 deletions packages/core/test/util/field.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/*
The MIT License

Copyright (c) 2018 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
Expand All @@ -24,7 +24,10 @@
*/
import test from 'ava';
import * as _ from 'lodash';
import { mapStateToFieldProps } from '../../src/util';
import {
defaultMapStateToEnumFieldProps,
mapStateToFieldProps
} from '../../src/util';

const hideRule = {
effect: 'HIDE',
Expand Down Expand Up @@ -56,7 +59,11 @@ const createState = uischema => ({
type: 'object',
properties: {
firstName: { type: 'string' },
lastName: { type: 'string' }
lastName: { type: 'string' },
nationality: {
type: 'string',
enum: ['DE', 'IT', 'JP', 'US', 'RU', 'Other']
}
}
},
data: {
Expand Down Expand Up @@ -204,3 +211,27 @@ test('mapStateToFieldProps - id', t => {
const props = mapStateToFieldProps(createState(coreUISchema), ownProps);
t.is(props.id, '#/properties/firstName');
});

test('mapStateToEnumFieldProps - set default options for dropdown list', t => {
const uiSchema = {
type: 'Control',
scope: '#/properties/nationality',
};
const ownProps = {
schema: {
type: 'object',
properties: {
firstName: { type: 'string' },
lastName: { type: 'string' },
nationality: {
type: 'string',
enum: ['DE', 'IT', 'JP', 'US', 'RU', 'Other']
}
}
},
uischema: uiSchema
};

const props = defaultMapStateToEnumFieldProps(createState(uiSchema), ownProps);
t.deepEqual(props.options, ['DE', 'IT', 'JP', 'US', 'RU', 'Other']);
});
14 changes: 8 additions & 6 deletions packages/material/src/fields/MaterialEnumField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
*/
import * as React from 'react';
import {
FieldProps,
EnumFieldProps,
isEnumControl,
mapDispatchToFieldProps,
mapStateToFieldProps,
defaultMapStateToEnumFieldProps,
RankedTester,
rankWith,
} from '@jsonforms/core';
Expand All @@ -36,9 +36,8 @@ import { connectToJsonForms } from '@jsonforms/react';
import Select from '@material-ui/core/Select';
import { MenuItem } from '@material-ui/core';

export const MaterialEnumField = (props: FieldProps) => {
const { data, className, id, enabled, uischema, path, handleChange, scopedSchema } = props;
const options = scopedSchema.enum;
export const MaterialEnumField = (props: EnumFieldProps) => {
const { data, className, id, enabled, uischema, path, handleChange, options } = props;

return (
<Select
Expand Down Expand Up @@ -69,4 +68,7 @@ export const MaterialEnumField = (props: FieldProps) => {
* @type {RankedTester}
*/
export const materialEnumFieldTester: RankedTester = rankWith(2, isEnumControl);
export default connectToJsonForms(mapStateToFieldProps, mapDispatchToFieldProps)(MaterialEnumField);
export default connectToJsonForms(
defaultMapStateToEnumFieldProps,
mapDispatchToFieldProps)
(MaterialEnumField);
107 changes: 107 additions & 0 deletions packages/material/test/renderers/MaterialEnumField.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
The MIT License

Copyright (c) 2018 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 * as React from 'react';
import {
Actions,
ControlElement,
getData,
jsonformsReducer,
JsonFormsState,
update
} from '@jsonforms/core';
import MaterialEnumField, { materialEnumFieldTester } from '../../src/fields/MaterialEnumField';
import { Provider } from 'react-redux';
import * as TestUtils from 'react-dom/test-utils';
import { materialFields, materialRenderers } from '../../src';
import { combineReducers, createStore, Store } from 'redux';

const data = { nationality: 'JP'};
const schema = {
type: 'object',
properties: {
nationality: {
type: 'string',
enum: ['DE', 'IT', 'JP', 'US', 'RU', 'Other']
}
}
};
const uischema = {
type: 'Control',
scope: '#/properties/nationality'
};

const initJsonFormsStore = (testData, testSchema, testUiSchema): Store<JsonFormsState> => {
const store: Store<JsonFormsState> = createStore(
combineReducers({ jsonforms: jsonformsReducer() }),
{
jsonforms: {
renderers: materialRenderers,
fields: materialFields,
}
}
);

store.dispatch(Actions.init(testData, testSchema, testUiSchema));
return store;
};

describe('Material enum field tester', () => {

it('should succeed with matching prop type', () => {
const control: ControlElement = {
type: 'Control',
scope: '#/properties/nationality'
};
expect(
materialEnumFieldTester(
control,
{
type: 'object',
properties: {
nationality: {
type: 'string',
enum: ['DE', 'IT', 'JP', 'US', 'RU', 'Other']
}
}
}
)
).toBe(2);
});
});

describe('Material enum field', () => {
it('should select an item from dropdown list', () => {
const store = initJsonFormsStore(data, schema, uischema);
const tree = TestUtils.renderIntoDocument(
<Provider store={store}>
<MaterialEnumField schema={schema} uischema={uischema}/>
</Provider>
);

const input = TestUtils.findRenderedDOMComponentWithTag(tree, 'input') as HTMLInputElement;
store.dispatch(update('nationality', () => 'DE'));
expect(input.value).toBe('DE');
});
});
19 changes: 9 additions & 10 deletions packages/vanilla/src/fields/EnumField.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/*
The MIT License

Copyright (c) 2018 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
Expand All @@ -24,19 +24,18 @@
*/
import * as React from 'react';
import {
FieldProps,
EnumFieldProps,
isEnumControl,
mapDispatchToFieldProps,
mapStateToFieldProps,
defaultMapStateToEnumFieldProps,
RankedTester,
rankWith,
} from '@jsonforms/core';
import { connectToJsonForms } from '@jsonforms/react';
import { SyntheticEvent } from 'react';

const EnumField = (props: FieldProps) => {
const { data, className, id, enabled, uischema, scopedSchema, path, handleChange } = props;
const options = scopedSchema.enum;
const EnumField = (props: EnumFieldProps) => {
const { data, className, id, enabled, uischema, path, handleChange, options } = props;

return (
<select
Expand Down Expand Up @@ -70,6 +69,6 @@ const EnumField = (props: FieldProps) => {
export const enumFieldTester: RankedTester = rankWith(2, isEnumControl);

export default connectToJsonForms(
mapStateToFieldProps,
defaultMapStateToEnumFieldProps,
mapDispatchToFieldProps
)(EnumField);