Skip to content

Commit c42d77f

Browse files
authored
Merge pull request #82 from headwirecom/issue/any-of-simple
implement any of simple control
2 parents 67d7cd9 + 8211ca9 commit c42d77f

File tree

4 files changed

+101
-2
lines changed

4 files changed

+101
-2
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
The MIT License
3+
4+
Copyright (c) 2020 headwire.com, Inc
5+
https://github.com/headwirecom/jsonforms-react-spectrum-renderers
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
*/
25+
26+
import {
27+
and,
28+
ControlProps,
29+
JsonSchema,
30+
RankedTester,
31+
rankWith,
32+
schemaMatches,
33+
uiTypeIs,
34+
} from '@jsonforms/core';
35+
import { withJsonFormsEnumProps } from '@jsonforms/react';
36+
import React from 'react';
37+
import { InputEnum } from '../spectrum-control';
38+
import { SpectrumInputControl } from './SpectrumInputControl';
39+
40+
const findEnumSchema = (schemas: JsonSchema[]) =>
41+
schemas.find(
42+
(s) => s.enum !== undefined && (s.type === 'string' || s.type === undefined)
43+
);
44+
const findTextSchema = (schemas: JsonSchema[]) =>
45+
schemas.find((s) => s.type === 'string' && s.enum === undefined);
46+
47+
export const SpectrumAnyOfStringOrEnumControl = (props: ControlProps) => {
48+
debugger;
49+
return <SpectrumInputControl {...props} input={InputEnum} />;
50+
};
51+
52+
const hasEnumAndText = (schemas: JsonSchema[]) => {
53+
// idea: map to type,enum and check that all types are string and at least one item is of type enum,
54+
const enumSchema = findEnumSchema(schemas);
55+
const stringSchema = findTextSchema(schemas);
56+
const remainingSchemas = schemas.filter(
57+
(s) => s !== enumSchema || s !== stringSchema
58+
);
59+
const wrongType = remainingSchemas.find((s) => s.type && s.type !== 'string');
60+
return enumSchema && stringSchema && !wrongType;
61+
};
62+
const simpleAnyOf = and(
63+
uiTypeIs('Control'),
64+
schemaMatches(
65+
(schema) => schema.hasOwnProperty('anyOf') && hasEnumAndText(schema.anyOf)
66+
)
67+
);
68+
69+
export const spectrumAnyOfStringOrEnumControlTester: RankedTester = rankWith(
70+
5,
71+
simpleAnyOf
72+
);
73+
74+
export default withJsonFormsEnumProps(SpectrumAnyOfStringOrEnumControl);

packages/spectrum/src/controls/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
*/
2828

2929
import InputControl, { inputControlTester } from './InputControl';
30+
import SpectrumAnyOfStringOrEnumControl, {
31+
spectrumAnyOfStringOrEnumControlTester,
32+
} from './SpectrumAnyOfStringOrEnumControl';
3033
import SpectrumBooleanControl, {
3134
spectrumBooleanControlTester,
3235
} from './SpectrumBooleanControl';
@@ -67,6 +70,8 @@ import SpectrumTimeControl, {
6770
export {
6871
InputControl,
6972
inputControlTester,
73+
SpectrumAnyOfStringOrEnumControl,
74+
spectrumAnyOfStringOrEnumControlTester,
7075
SpectrumBooleanControl,
7176
spectrumBooleanControlTester,
7277
SpectrumDateControl,

packages/spectrum/src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ import {
5353
import {
5454
InputControl,
5555
inputControlTester,
56+
SpectrumAnyOfStringOrEnumControl,
57+
spectrumAnyOfStringOrEnumControlTester,
5658
SpectrumBooleanControl,
5759
spectrumBooleanControlTester,
5860
SpectrumDateControl,
@@ -127,6 +129,10 @@ export const spectrumRenderers: { tester: RankedTester; renderer: any }[] = [
127129
tester: spectrumListWithDetailTester,
128130
renderer: SpectrumListWithDetailRenderer,
129131
},
132+
{
133+
tester: spectrumAnyOfStringOrEnumControlTester,
134+
renderer: SpectrumAnyOfStringOrEnumControl,
135+
},
130136
{ tester: spectrumBooleanControlTester, renderer: SpectrumBooleanControl },
131137
{ tester: spectrumDateControlTester, renderer: SpectrumDateControl },
132138
{ tester: spectrumDateTimeControlTester, renderer: SpectrumDateTimeControl },

packages/spectrum/src/spectrum-control/InputEnum.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
THE SOFTWARE.
2424
*/
2525
import React from 'react';
26-
import { EnumCellProps } from '@jsonforms/core';
26+
import { EnumCellProps, JsonSchema } from '@jsonforms/core';
2727
import { merge } from 'lodash';
2828
import { SpectrumInputProps } from './index';
2929
import { DimensionValue } from '@react-types/shared';
@@ -54,6 +54,20 @@ export class InputEnum extends React.PureComponent<
5454
? undefined
5555
: '100%';
5656

57+
const findEnumSchema = (schemas: JsonSchema[]) =>
58+
schemas.find(
59+
(s) =>
60+
s.enum !== undefined && (s.type === 'string' || s.type === undefined)
61+
);
62+
63+
const tryEnumSchema = (anyOf: JsonSchema[]) => {
64+
const enumSchema = findEnumSchema(anyOf);
65+
return enumSchema.enum.map((v) => {
66+
return { value: v, label: v };
67+
});
68+
};
69+
const items = options ?? tryEnumSchema(this.props.schema.anyOf);
70+
5771
return (
5872
<Picker
5973
key={id}
@@ -62,7 +76,7 @@ export class InputEnum extends React.PureComponent<
6276
isRequired={isRequired}
6377
isDisabled={!enabled}
6478
width={width}
65-
items={options}
79+
items={items}
6680
selectedKey={data}
6781
onSelectionChange={(ev) => handleChange(path, ev)}
6882
>

0 commit comments

Comments
 (0)