react-jsonschema-form
react-jsonschema-form copied to clipboard
Enum enumNames are not applied to null values
Prerequisites
- [X] I have searched the existing issues
- [X] I understand that providing a SSCCE example is tremendously useful to the maintainers.
- [X] I have read the documentation
- [X] Ideally, I'm providing a sample JSFiddle or a shared playground link demonstrating the issue.
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
- Create a schema with a property with
type: ["number", "null"]
- Give the property some
enum
values. Leave one of themnull
. - Give the property some
enumNames
. You could name thenull
value for example"Empty"
. - Select the
null
value (with the name"Empty"
). - 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
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
)
The issue with semantic-ui and material-ui themes is that the field is emptied when the null
value is selected:
Even though it should say "None"
, because that is the given enumName
@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).
@f4irline I'm sorry to say but
enumNames
support is being dropped in version 5 as there are options that work withoneOf
(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
Any updates on this? I also have this problem with null
transformed to string, and can't solve it with oneOf
@f4irline I'm sorry to say but
enumNames
support is being dropped in version 5 as there are options that work withoneOf
(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.