[2.0.0-beta1] EnumPropType: Labelled enumerations

Created on 21 June 2024, over 1 year ago

Problem/Motivation

The current JSON schema for enum prop type is:

{
      "type": ["string", "number", "integer"],
      "enum": {}
}

https://git.drupalcode.org/project/ui_patterns/-/blob/2.0.x/src/Plugin/U...

As already discussed in Introduce component variants to SDC Active , this JSON schema doesn't allow to add label to items

For example, we have only "technical" values here:

props:
  type: object
  properties:
    position:
      type: string
      enum:
        - top
        - bottom

Proposed resolution

anyOF + const

Using a anyOf with const is verbose and complicated:

props:
  type: object
  properties:
    position:
      anyOf:
        - { "const": "top", "title": "Top"}
        - { "const": "bottom", "title": "Secondary"}

But it is JSON schema native and compliant.

meta:enum

props:
  type: object
  properties:
    position:
      type: string
      enum:
        - top
        - bottom
      "meta:enum":
        top: Top
        bottom: Bottom

Not an official standard but supported by some projects:

Other proposals?

Let's investigate

Remaining tasks

If needed, change the EnumPropType schema annotation.
If needed, add the test case

User interface changes

The source plugins for enum prop types can now use the label.

API changes

According to our changes, we may need to also update the JSON Schema Compatibility Checker with:

  • type: string; enum: [] as an equivalent of anyOf/const. We must stay compatible with the prop definition using this shorter version
  • const as an equivalent as enum when there is only one item in the enum
  • anyOf (OR: Must be valid against any of the subschemas) as a superset of oneOf: (XOR: Must be valid against exactly one of the subschemas)

No need for PropTypeAdapter plugins here, because it is pure JSON schema logic.

UI Patterns Legacy changes

The current component converter is not converting the labels which are lost:

      'select' => [
        '$ref' => 'ui-patterns://enum',
        'enum' => \array_keys($setting['options']),
      ],

https://git.drupalcode.org/project/ui_patterns/-/blob/2.0.x/modules/ui_p...

Import then using the notation from EnumPropType schema annotation.

Feature request
Status

Active

Version

2.0

Component

Code

Created by

🇫🇷France pdureau Paris

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024