[2.0.0-alpha3] EnumPropType: Labelled enumerations

Created on 21 June 2024, 5 months ago
Updated 3 July 2024, 5 months ago

Problem/Motivation

The current JSON schema for EnumPropType is:

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

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", "machine" values here:

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

We have found 2 solutions to add labels to enums:

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 popular projects:

anyOF + const

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

A bit more verbose and complicated, but JSON schema native and compliant.

Proposed resolution

meta:enum

Let's promote it as the recommended way. So, there is nothing to change on EnumPropType annotations.

We just need to:

  • update ::getSummary() method
  • update the source plugins for EnumPropType (so, only SelectWidget for now) which can now use the label.

anyOF + const

We need to cover this one too, let's do it in SchemaManager/Canonicalizer

If anyOfof oneOfis found, and every value is a const or an enum with only one item

, we transform it to:

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

UI Patterns Legacy changes

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

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

Let's add "meta:enum" property with the expected value.

Feature request
Status

Fixed

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