A oneOf or anyOf of constants renders a select, with or without a type
Detect
- In your schema: a
oneOf/anyOfwhose options are allconstor single-valueenum, with notype. - In your schema: object or array constants, in a
oneOf/anyOfor as anenumof atype: 'object'/'array', such as{ "type": "object", "enum": [{ "a": 1 }, { "a": 2 }] }. - In your schema: an
enumor constantoneOf/anyOfwhosetypelist names more than one non-nulltype. - On the rendered page: a select where the form showed an unsupported field, an
ObjectFieldwith no control, or anArrayFieldlist editor.
Change
SchemaField now infers the type from the constant values, so the select these schemas describe renders (#4666):
{
"oneOf": [
{ "const": "gpl", "title": "GPL" },
{ "const": "mit", "title": "MIT" }
]
}
Object and array constants render through a select whose options are labelled with their title, or with their JSON when they have none, whether the schema omits type or declares object/array (#5317). They are given the type object/array, and a declared type is kept, so custom fields, templates and widgets see the type of the value they choose. StringField renders the select, as it renders every other select. A typed object or array with an empty oneOf: []/anyOf: [] is still edited through ObjectField/ArrayField.
The data changes with it:
getDefaultFormState()fills such a select as a whole, the way it fills a primitive select. A requiredoneOf/anyOforenumselect defaults to its owndefault, and is otherwise left unfilled, unless it is aoneOf/anyOfof one constant. A picked value is kept. An ancestor'sdefaultnaming one of its constants picks that one, andui:initialValue/ui:emptyValueapply as they do to a primitive select. Both used to get an empty{}/[]that no option allows, and a picked object could be blended with the select'sdefault.omitExtraData(),sanitizeDataForNewSchema()and theui:requiredcheck treat the value as one constant.omitExtraData/liveOmitkeep the picked object or array whole instead of pruning it to nothing, and switching to a schema whose select doesn't offer it clears it. Picking one sanitizes the data a condition on it controls, as picking a primitive option does.- An array select offers the
radioalias as well asselectandhidden, and no longercheckboxesorfiles. A multi-select array still has noradio. - A select whose
typelist names more than one non-nulltype renders throughStringField, which keeps each option's value. It used to render the field for the first listed type, which wasNullFieldfor a list starting withnull, and a list starting withnumbercast every option to a number, so atrueoption wrote1.
If a widget alias stops applying, switch the field to select, radio or hidden:
const uiSchema = {
- tags: { 'ui:widget': 'checkboxes' },
+ tags: { 'ui:widget': 'radio' },
};
Verify
- Each such field renders a select, labelled by its options' titles or JSON.
- The submitted data holds the picked constant whole, and a required select starts on its
default, or empty where it has none, unless it is aoneOf/anyOfof one constant. - Three narrower shapes have entries of their own: a schema with both anyOf and oneOf, a boolean with labelled constant options and a typeless falsy const with constAsDefaults never.
Also read:
- A schema with both anyOf and oneOf reads anyOf first everywhere
- A boolean with labelled constant options renders a select instead of a checkbox
- A typeless falsy const writes its value with constAsDefaults never
- The rjsf-field type class on a type list names the field rendered
- A list of choices with no default starts empty