react-jsonschema-form icon indicating copy to clipboard operation
react-jsonschema-form copied to clipboard

Enum enumNames are not applied to null values

Open f4irline opened this issue 2 years ago • 4 comments

Prerequisites

What theme are you using?

core

Version

4.2.0

Current Behavior

I have a field with number enums, which should be nullable. I've specified the type to be ["number", "null"]. I'm also using enumNames property to give the values different titles. My schema is this:

{
  "title": "A registration form",
  "description": "A simple form example.",
  "type": "object",
  "required": [
    "favoriteColor"
  ],
  "properties": {
    "favoriteColor": {
      "type": ["number", "null"],
      "title": "Favorite Color",
      "enum": [
        0,
        1,
        2,
        null
      ],
      "enumNames": [
        "Red",
        "Green",
        "Blue",
        "None"
      ]
    },
    "default": null
  }
}

But when I choose the null ("None") value, the data doesn't give the field value null. Instead it gives it value "None". For other values it works, for example if I choose the value "Red", the value is 0 as it should. I'm seeing this behavior with type: ["string", "null"] as well.

I've also tried providing the following UISchema with no luck:

{
  "favoriteColor": {
    "ui:emptyValue": null
  }
}

Expected Behavior

enumNames should also map names for null values.

Steps To Reproduce

  1. Create a schema with a property with type: ["number", "null"]
  2. Give the property some enum values. Leave one of them null.
  3. Give the property some enumNames. You could name the null value for example "Empty".
  4. Select the null value (with the name "Empty").
  5. You should see that the value is not null as expected, but "Empty".

Environment

- OS: MacOS
- Node: 14.19.1
- npm: 6.14.6

Anything else?

Link to the RJSF playground: here

f4irline avatar Jun 30 '22 08:06 f4irline

This actually seems to work as expected with the semantic-ui and both material-ui themes (4 and 5). With other themes either the value is given the enumName, or "null" (which is also incorrect, because it should be null)

f4irline avatar Jun 30 '22 09:06 f4irline

The issue with semantic-ui and material-ui themes is that the field is emptied when the null value is selected: image

Even though it should say "None", because that is the given enumName

f4irline avatar Jun 30 '22 09:06 f4irline

@f4irline I'm sorry to say but enumNames support is being dropped in version 5 as there are options that work with oneOf (see https://github.com/rjsf-team/react-jsonschema-form/issues/532 for how to do it).

heath-freenome avatar Aug 22 '22 22:08 heath-freenome

@f4irline I'm sorry to say but enumNames support is being dropped in version 5 as there are options that work with oneOf (see #532 for how to do it).

That's alright. But I'm seeing the same behaviour with oneOf as well, see RJSF playground here

f4irline avatar Aug 23 '22 09:08 f4irline

Any updates on this? I also have this problem with null transformed to string, and can't solve it with oneOf

Sinled avatar Dec 02 '22 11:12 Sinled

@f4irline I'm sorry to say but enumNames support is being dropped in version 5 as there are options that work with oneOf (see #532 for how to do it).

That's alright. But I'm seeing the same behaviour with oneOf as well, see RJSF playground here

The issue in the above playground seems to go away if the default entry is the schema is removed, so that may be the source of one of the bugs.

nickgros avatar Apr 14 '23 15:04 nickgros