An anyOf or oneOf field keeps the option the user selected
Detect
- Your schema has
anyOforoneOf, and your tests select an option, type into it and then assert which option or which inputs show. - Tests that unmount and remount a field, reorder array items or rename extra keys and then check the option shown.
- On the rendered page: a selector that used to switch back to another option while the user typed now stays put.
Change
The option a field shows and which collapsed $ref cycles are expanded are now kept by the form, by field path, instead of by the AnyOfField/OneOfField and CyclicSchemaField components. They survive the field unmounting and mounting again, and follow array items and extra keys that the form moves, renames or removes.
With no selection, a field shows the option getClosestMatchingOption() picks for its data, as before. A selection, the user's or that pick once an edit is made inside the option, holds for the data the form last rendered under it and for whatever the form's own edits inside the option leave at the field. A switch or an edit that transformChange refuses keeps it too.
The field goes back to the option getClosestMatchingOption() picks, with the selection winning a tie, for other data:
- data from
setFieldValue(), the data passed toreset(saved), or atransformChangethat rewrites a switch to data the chosen option doesn't fit; - an edit to a property the object declares beside its options that the options can disagree about: one an option names under
properties(such as aconstthat tells the options apart) orrequired, or any property where an option carries a keyword that reads the whole object, such asminProperties,notoradditionalProperties.
An edit to the discriminator property the object itself declares is the exception. It keeps the option shown where the value it leaves, such as one the user cleared or is partway through typing, names no other option, or where the option shown pins that value too, and it switches where only other options name the value. An option the user picked, or the one shown when an edit began, stays for the value it was picked or shown for wherever it accepts that value, even one only other options name, so an edit elsewhere in the option doesn't switch it. That lasts until the discriminator itself is edited: a value retyped there names its option again, even the one the pick was made for.
An edit to a property every option is silent about, where each carries only type, annotations such as title, and properties and required that leave the property out, keeps the option shown. The pick doesn't replace the selection, and the selection is kept by field path, so it outlives the data it was made for, until a reset(): see reset() shows the anyOf or oneOf option a mount on the seed shows.
A field whose data holds a value for its schema's discriminator.propertyName shows the option that value names, as before: the discriminator is the selector, so an edit to it is a switch. Where several options pin the same value, as the options a merge of lists builds from one option do, the selection among them holds while its option pins the data's value, and with no selection the rest of the data picks between them. A value no option pins, such as one the user cleared or is partway through typing, keeps the option the selection holds, unless that option doesn't match the data and another does, as getFirstMatchingOption() matches them: by their declarations of the property, such as a pattern, a not or a false, or, where an option declares none, by the data as a whole, such as under additionalProperties: false.
Where an option declaring none of the property has a field of its own invalid mid-edit, so that it rejects the data with or without the value, it is asked about the value alone, as matching scores it, without its requireds and dependencies names, so a half-filled field doesn't switch away from it. One with nothing of its own filled in but what the form starts it with, such as on a new form with a list's minItems rows or a default, or an empty value, such as a null or a list row typed into and cleared, switches as before. Once a field it declares is filled in, one still holding a start or an empty value it rejects, such as an untouched minItems row or a cleared one, counts as invalid beside it. One rejecting the value only beside another of its fields, as a dependencies entry or maxProperties can, switches once that field, and any start it rejects beside a filled field, turns valid (#5999).
No code has to change. Update tests to the new behaviour; "before" is what 6.11.0 gives:
- With
o: { anyOf: [{ title: 'A', type: 'object', properties: { a: { type: 'string', default: 'd' } } }, { title: 'B', type: 'object', properties: { a: { type: 'string' }, b: { type: 'string' } } }] }, selectB, clearroot_o_aand typed. Before: the selector went back toA, whosedefaultthe data now matched, androot_o_bdisappeared. After: it stays onB, withroot_o_b. - With
{ type: 'object', properties: { name: { type: 'string' } }, oneOf: [{ title: 'A', properties: { a: { type: 'string' } } }, { title: 'B', properties: { a: { type: 'string', maxLength: 2 }, b: { type: 'string' } }, required: ['b'] }] }, selectB, typezintoroot_b,abcintoroot_aand thennintoroot_name. Before: the selector went toAatabc, whichBrejects, androot_bdisappeared; withomitExtraDataandliveOmit,b: 'z'was dropped there. After: it stays onBthrough both edits, with{ a: 'abc', b: 'z', name: 'n' }. - With
{ type: 'object', properties: { a: { type: 'string' }, b: { type: 'string' } }, oneOf: [{ title: 'A', required: ['a'] }, { title: 'B', required: ['b'] }] }and{}, typexintoroot_b: the selector goes toB, before and after, sinceB'srequirednamesb. So doesanyOf: [{ title: 'Few', maxProperties: 1 }, { title: 'Many', minProperties: 2 }]with{ a: 'x' }whenyis typed intoroot_b:Many, before and after. - A condition that swaps the option list a field offers starts the choice over. With
kind: { enum: ['a', 'b'], default: 'a' },if: { properties: { kind: { const: 'a' } } },then: { properties: { x: { anyOf: [A1, A2] } } }andelse: { properties: { x: { anyOf: [B1, B2] } } }, selectA2, thenbforkind. Before:B2, the index carried over. After:B1, the option the data matches; selectingaagain showsA2before and after. - A field that mounts again shows the option selected before. With the
oabove, selectB, then render the form witho: { 'ui:field': Nothing, 'ui:fieldReplacesAnyOrOneOf': true }and again without it. Before:A. After:B. - Data that
setFieldValue()sets and every option fits shows the selection, where it used to show the option an earlier call matched. With the rootoneOf: [{ title: 'A', type: 'object', properties: { a: { type: 'string' } } }, { title: 'B', type: 'object', properties: { b: { type: 'string' } } }], selectBand typeyintoroot_b.setFieldValue('', { a: 'x' })then showsAbefore and after, andsetFieldValue('', {})showedAand now showsB. - A selection outlives the array item it was made for. With
{ type: 'array', items: ...that oneOf }and[{}, {}], selectBon the second item, then callsetFieldValue('', [{}])and latersetFieldValue('', [{}, {}]). Before: the new second item showsA. After:B, the selection made at that index, which the new item's data fits. An item the form removes or moves takes its selection with it. - A field opens on the pick of
getClosestMatchingOption(), which now returns an option the data matches whenever one does (see getClosestMatchingOption returns a valid option index whenever there are options). So a field whose first option is{ type: 'null', title: 'None' }and whose data several other options match opens on one of those. With five definitionsAtoE, each{ type: 'object', properties: { child: { oneOf: [{ type: 'null', title: 'None' }, ...a $ref to each of the four others] } } }, the rootoneOf: [{ type: 'null', title: 'None' }, ...a $ref to each definition]and theformData{ child: { child: { child: null } } }, the form used to open onNoneand render only the root selector. It now opens onAand renders a selector at each of the four levels.
The selection is also what sanitizing, ui:required and omitExtraData read; see Form keeps or drops a value by comparing the field trees before and after a change and ui:required and omitExtraData follow the anyOf or oneOf option on screen. A custom AnyOfField or OneOfField has to keep it through useOptionSelection(); see A custom AnyOfField or OneOfField reads the selection through useOptionSelection.
Verify
- Your tests that switch options and type into them pass against the option the user selected.
- A form whose data matches a non-null option opens on that option.
Also read:
- A custom AnyOfField or OneOfField reads the selection through useOptionSelection
- ui:required and omitExtraData follow the anyOf or oneOf option on screen
- Form keeps or drops a value by comparing the field trees before and after a change
- getClosestMatchingOption returns a valid option index whenever there are options