Skip to content

implement any of simple control #82

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
Dec 21, 2020
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
The MIT License

Copyright (c) 2020 headwire.com, Inc
https://github.com/headwirecom/jsonforms-react-spectrum-renderers

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 {
and,
ControlProps,
JsonSchema,
RankedTester,
rankWith,
schemaMatches,
uiTypeIs,
} from '@jsonforms/core';
import { withJsonFormsEnumProps } from '@jsonforms/react';
import React from 'react';
import { InputEnum } from '../spectrum-control';
import { SpectrumInputControl } from './SpectrumInputControl';

const findEnumSchema = (schemas: JsonSchema[]) =>
schemas.find(
(s) => s.enum !== undefined && (s.type === 'string' || s.type === undefined)
);
const findTextSchema = (schemas: JsonSchema[]) =>
schemas.find((s) => s.type === 'string' && s.enum === undefined);

export const SpectrumAnyOfStringOrEnumControl = (props: ControlProps) => {
debugger;
return <SpectrumInputControl {...props} input={InputEnum} />;
};

const hasEnumAndText = (schemas: JsonSchema[]) => {
// idea: map to type,enum and check that all types are string and at least one item is of type enum,
const enumSchema = findEnumSchema(schemas);
const stringSchema = findTextSchema(schemas);
const remainingSchemas = schemas.filter(
(s) => s !== enumSchema || s !== stringSchema
);
const wrongType = remainingSchemas.find((s) => s.type && s.type !== 'string');
return enumSchema && stringSchema && !wrongType;
};
const simpleAnyOf = and(
uiTypeIs('Control'),
schemaMatches(
(schema) => schema.hasOwnProperty('anyOf') && hasEnumAndText(schema.anyOf)
)
);

export const spectrumAnyOfStringOrEnumControlTester: RankedTester = rankWith(
5,
simpleAnyOf
);

export default withJsonFormsEnumProps(SpectrumAnyOfStringOrEnumControl);
5 changes: 5 additions & 0 deletions packages/spectrum/src/controls/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
*/

import InputControl, { inputControlTester } from './InputControl';
import SpectrumAnyOfStringOrEnumControl, {
spectrumAnyOfStringOrEnumControlTester,
} from './SpectrumAnyOfStringOrEnumControl';
import SpectrumBooleanControl, {
spectrumBooleanControlTester,
} from './SpectrumBooleanControl';
Expand Down Expand Up @@ -67,6 +70,8 @@ import SpectrumTimeControl, {
export {
InputControl,
inputControlTester,
SpectrumAnyOfStringOrEnumControl,
spectrumAnyOfStringOrEnumControlTester,
SpectrumBooleanControl,
spectrumBooleanControlTester,
SpectrumDateControl,
Expand Down
6 changes: 6 additions & 0 deletions packages/spectrum/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ import {
import {
InputControl,
inputControlTester,
SpectrumAnyOfStringOrEnumControl,
spectrumAnyOfStringOrEnumControlTester,
SpectrumBooleanControl,
spectrumBooleanControlTester,
SpectrumDateControl,
Expand Down Expand Up @@ -127,6 +129,10 @@ export const spectrumRenderers: { tester: RankedTester; renderer: any }[] = [
tester: spectrumListWithDetailTester,
renderer: SpectrumListWithDetailRenderer,
},
{
tester: spectrumAnyOfStringOrEnumControlTester,
renderer: SpectrumAnyOfStringOrEnumControl,
},
{ tester: spectrumBooleanControlTester, renderer: SpectrumBooleanControl },
{ tester: spectrumDateControlTester, renderer: SpectrumDateControl },
{ tester: spectrumDateTimeControlTester, renderer: SpectrumDateTimeControl },
Expand Down
18 changes: 16 additions & 2 deletions packages/spectrum/src/spectrum-control/InputEnum.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
THE SOFTWARE.
*/
import React from 'react';
import { EnumCellProps } from '@jsonforms/core';
import { EnumCellProps, JsonSchema } from '@jsonforms/core';
import { merge } from 'lodash';
import { SpectrumInputProps } from './index';
import { DimensionValue } from '@react-types/shared';
Expand Down Expand Up @@ -54,6 +54,20 @@ export class InputEnum extends React.PureComponent<
? undefined
: '100%';

const findEnumSchema = (schemas: JsonSchema[]) =>
schemas.find(
(s) =>
s.enum !== undefined && (s.type === 'string' || s.type === undefined)
);

const tryEnumSchema = (anyOf: JsonSchema[]) => {
const enumSchema = findEnumSchema(anyOf);
return enumSchema.enum.map((v) => {
return { value: v, label: v };
});
};
const items = options ?? tryEnumSchema(this.props.schema.anyOf);

return (
<Picker
key={id}
Expand All @@ -62,7 +76,7 @@ export class InputEnum extends React.PureComponent<
isRequired={isRequired}
isDisabled={!enabled}
width={width}
items={options}
items={items}
selectedKey={data}
onSelectionChange={(ev) => handleChange(path, ev)}
>
Expand Down