UNPKG

@rjsf/utils

Version:
47 lines 2.19 kB
import isObject from 'lodash-es/isObject.js'; import uniq from 'lodash-es/uniq.js'; import getSchemaType from './getSchemaType.js'; import getUiOptions from './getUiOptions.js'; import isRootSchema from './isRootSchema.js'; import { ANY_OF_KEY, ONE_OF_KEY } from './constants.js'; /** Returns the unique list of schema types for all of the options in a anyOf/oneOf * * @param schemas - The list of schemas representing the XxxOf options * @returns - All of the unique types contained within the oneOf list */ export function getSchemaTypesForXxxOf(schemas) { const allTypes = uniq(schemas .map((s) => (isObject(s) ? getSchemaType(s) : undefined)) .flat() .filter((t) => t !== undefined)); return allTypes.length === 1 ? allTypes[0] : allTypes; } /** Determines whether the field information from the combination of `schema` and `required` along with the * `enableOptionalDataFieldForType` settings from the global UI options in the `registry` all indicate that this field * should be rendered with the Optional Data Controls UI. * * @param registry - The `registry` object * @param schema - The schema for the field * @param required - Flag indicating whether the field is required * @param [uiSchema] - The uiSchema for the field * @return - True if the field should be rendered with the optional field UI, otherwise false */ export default function shouldRenderOptionalField(registry, schema, required, uiSchema) { const { enableOptionalDataFieldForType = [] } = getUiOptions(uiSchema, registry.globalUiOptions); let schemaType; if (ANY_OF_KEY in schema && Array.isArray(schema[ANY_OF_KEY])) { schemaType = getSchemaTypesForXxxOf(schema[ANY_OF_KEY]); } else if (ONE_OF_KEY in schema && Array.isArray(schema[ONE_OF_KEY])) { schemaType = getSchemaTypesForXxxOf(schema[ONE_OF_KEY]); } else { schemaType = getSchemaType(schema); } return (!isRootSchema(registry, schema) && !required && !!schemaType && !Array.isArray(schemaType) && !!enableOptionalDataFieldForType.find((val) => val === schemaType)); } //# sourceMappingURL=shouldRenderOptionalField.js.map