|
| 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); |
0 commit comments