A custom AnyOfField or OneOfField reads the selection through useOptionSelection
Detect
- In your code: a component passed as
AnyOfFieldorOneOfFieldinfields=that holds the selected option index inuseState, a classstateor a ref. - On the rendered page: such a field shows one option while the extra keys of the object beside it render for another.
Change
AnyOfField and OneOfField read and write the selection with useOptionSelection(), which @rjsf/core exports, and SchemaField reads it to stub an object's extra keys with the selected option. A custom replacement that keeps the selection in its own state shows one option while the object beside it is stubbed for another. Read and write it through the hook; the options prop stays:
import { useOptionSelection } from '@rjsf/core';
const { selectedOption, selectOption } = useOptionSelection(registry, fieldPath, options, formData, discriminator);
// on a pick: selectOption(index), then onChange(dataForTheNewOption, fieldPath)
The options it receives are already retrieved; see AnyOfField and OneOfField expect their options already retrieved.
Verify
- Your field shows the option the user picked after it unmounts and mounts again, as the built-in field does (see An anyOf or oneOf field keeps the option the user selected).
- The object's extra keys render for the option your field shows.
Also read: