From a28cb43db718db1804bbdd1f98d4f03a0b46dd0c Mon Sep 17 00:00:00 2001 From: Ahmet Tanakol Date: Mon, 11 Jun 2018 17:23:31 +0200 Subject: [PATCH] EnumField refactor --- packages/core/src/util/field.ts | 37 +++++- packages/core/test/util/field.test.ts | 43 ++++++- .../material/src/fields/MaterialEnumField.tsx | 14 ++- .../test/renderers/MaterialEnumField.test.tsx | 107 ++++++++++++++++++ packages/vanilla/src/fields/EnumField.tsx | 19 ++-- 5 files changed, 194 insertions(+), 26 deletions(-) create mode 100644 packages/material/test/renderers/MaterialEnumField.test.tsx diff --git a/packages/core/src/util/field.ts b/packages/core/src/util/field.ts index 1eed62a0b..8406bdd85 100644 --- a/packages/core/src/util/field.ts +++ b/packages/core/src/util/field.ts @@ -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 @@ -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. */ @@ -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. * diff --git a/packages/core/test/util/field.test.ts b/packages/core/test/util/field.test.ts index d6a1eac06..d1dd4f823 100644 --- a/packages/core/test/util/field.test.ts +++ b/packages/core/test/util/field.test.ts @@ -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 @@ -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', @@ -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: { @@ -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']); +}); diff --git a/packages/material/src/fields/MaterialEnumField.tsx b/packages/material/src/fields/MaterialEnumField.tsx index 3de68eab0..adef78ebd 100644 --- a/packages/material/src/fields/MaterialEnumField.tsx +++ b/packages/material/src/fields/MaterialEnumField.tsx @@ -24,10 +24,10 @@ */ import * as React from 'react'; import { - FieldProps, + EnumFieldProps, isEnumControl, mapDispatchToFieldProps, - mapStateToFieldProps, + defaultMapStateToEnumFieldProps, RankedTester, rankWith, } from '@jsonforms/core'; @@ -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 ( { export const enumFieldTester: RankedTester = rankWith(2, isEnumControl); export default connectToJsonForms( - mapStateToFieldProps, + defaultMapStateToEnumFieldProps, mapDispatchToFieldProps )(EnumField);