@rjsf/utils
Advanced tools
Comparing version 5.19.4 to 5.20.0
@@ -1,10 +0,13 @@ | ||
import { RJSFSchema, EnumOptionsType, StrictRJSFSchema } from './types'; | ||
/** Gets the list of options from the schema. If the schema has an enum list, then those enum values are returned. The | ||
import { RJSFSchema, EnumOptionsType, StrictRJSFSchema, FormContextType, UiSchema } from './types'; | ||
/** Gets the list of options from the `schema`. If the schema has an enum list, then those enum values are returned. The | ||
* labels for the options will be extracted from the non-standard, RJSF-deprecated `enumNames` if it exists, otherwise | ||
* the label will be the same as the `value`. If the schema has a `oneOf` or `anyOf`, then the value is the list of | ||
* `const` values from the schema and the label is either the `schema.title` or the value. | ||
* `const` values from the schema and the label is either the `schema.title` or the value. If a `uiSchema` is provided | ||
* and it has the `ui:enumNames` matched with `enum` or it has an associated `oneOf` or `anyOf` with a list of objects | ||
* containing `ui:title` then the UI schema values will replace the values from the schema. | ||
* | ||
* @param schema - The schema from which to extract the options list | ||
* @param [uiSchema] - The optional uiSchema from which to get alternate labels for the options | ||
* @returns - The list of options from the schema | ||
*/ | ||
export default function optionsList<S extends StrictRJSFSchema = RJSFSchema>(schema: S): EnumOptionsType<S>[] | undefined; | ||
export default function optionsList<S extends StrictRJSFSchema = RJSFSchema, T = any, F extends FormContextType = any>(schema: S, uiSchema?: UiSchema<T, S, F>): EnumOptionsType<S>[] | undefined; |
import toConstant from './toConstant'; | ||
/** Gets the list of options from the schema. If the schema has an enum list, then those enum values are returned. The | ||
import getUiOptions from './getUiOptions'; | ||
/** Gets the list of options from the `schema`. If the schema has an enum list, then those enum values are returned. The | ||
* labels for the options will be extracted from the non-standard, RJSF-deprecated `enumNames` if it exists, otherwise | ||
* the label will be the same as the `value`. If the schema has a `oneOf` or `anyOf`, then the value is the list of | ||
* `const` values from the schema and the label is either the `schema.title` or the value. | ||
* `const` values from the schema and the label is either the `schema.title` or the value. If a `uiSchema` is provided | ||
* and it has the `ui:enumNames` matched with `enum` or it has an associated `oneOf` or `anyOf` with a list of objects | ||
* containing `ui:title` then the UI schema values will replace the values from the schema. | ||
* | ||
* @param schema - The schema from which to extract the options list | ||
* @param [uiSchema] - The optional uiSchema from which to get alternate labels for the options | ||
* @returns - The list of options from the schema | ||
*/ | ||
export default function optionsList(schema) { | ||
// enumNames was deprecated in v5 and is intentionally omitted from the RJSFSchema type. | ||
// Cast the type to include enumNames so the feature still works. | ||
export default function optionsList(schema, uiSchema) { | ||
// TODO flip generics to move T first in v6 | ||
const schemaWithEnumNames = schema; | ||
if (schemaWithEnumNames.enumNames && process.env.NODE_ENV !== 'production') { | ||
console.warn('The enumNames property is deprecated and may be removed in a future major release.'); | ||
} | ||
if (schema.enum) { | ||
let enumNames; | ||
if (uiSchema) { | ||
const { enumNames: uiEnumNames } = getUiOptions(uiSchema); | ||
enumNames = uiEnumNames; | ||
} | ||
if (!enumNames && schemaWithEnumNames.enumNames) { | ||
// enumNames was deprecated in v5 and is intentionally omitted from the RJSFSchema type. | ||
// Cast the type to include enumNames so the feature still works. | ||
if (process.env.NODE_ENV !== 'production') { | ||
console.warn('The "enumNames" property in the schema is deprecated and will be removed in a future major release. Use the "ui:enumNames" property in the uiSchema instead.'); | ||
} | ||
enumNames = schemaWithEnumNames.enumNames; | ||
} | ||
return schema.enum.map((value, i) => { | ||
const label = (schemaWithEnumNames.enumNames && schemaWithEnumNames.enumNames[i]) || String(value); | ||
const label = (enumNames === null || enumNames === void 0 ? void 0 : enumNames[i]) || String(value); | ||
return { label, value }; | ||
}); | ||
} | ||
const altSchemas = schema.oneOf || schema.anyOf; | ||
let altSchemas = undefined; | ||
let altUiSchemas = undefined; | ||
if (schema.anyOf) { | ||
altSchemas = schema.anyOf; | ||
altUiSchemas = uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema.anyOf; | ||
} | ||
else if (schema.oneOf) { | ||
altSchemas = schema.oneOf; | ||
altUiSchemas = uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema.oneOf; | ||
} | ||
return (altSchemas && | ||
altSchemas.map((aSchemaDef) => { | ||
altSchemas.map((aSchemaDef, index) => { | ||
const { title } = getUiOptions(altUiSchemas === null || altUiSchemas === void 0 ? void 0 : altUiSchemas[index]); | ||
const aSchema = aSchemaDef; | ||
const value = toConstant(aSchema); | ||
const label = aSchema.title || String(value); | ||
const label = title || aSchema.title || String(value); | ||
return { | ||
@@ -30,0 +53,0 @@ schema: aSchema, |
@@ -645,3 +645,3 @@ import type { ButtonHTMLAttributes, ChangeEvent, ComponentType, HTMLAttributes, ReactElement, ReactNode, StyleHTMLAttributes } from 'react'; | ||
/** The value change event handler; call it with the new value every time it changes */ | ||
onChange: (value: any) => void; | ||
onChange: (value: any, es?: ErrorSchema<T>, id?: string) => void; | ||
/** The input focus event handler; call it with the widget id and value */ | ||
@@ -765,2 +765,4 @@ onFocus: (id: string, value: any) => void; | ||
widget?: Widget<T, S, F> | string; | ||
/** Allows a user to provide a list of labels for enum values in the schema */ | ||
enumNames?: string[]; | ||
}; | ||
@@ -767,0 +769,0 @@ /** The type that represents the Options potentially provided by `ui:options` */ |
{ | ||
"name": "@rjsf/utils", | ||
"version": "5.19.4", | ||
"version": "5.20.0", | ||
"main": "dist/index.js", | ||
@@ -88,3 +88,3 @@ "module": "lib/index.js", | ||
"license": "Apache-2.0", | ||
"gitHead": "e2c3b063b0d1186fe3a8ced0e9effe29138e4440" | ||
"gitHead": "5522ddae3fa01ef70744f9a5f0a1e4073b203a01" | ||
} |
import toConstant from './toConstant'; | ||
import { RJSFSchema, EnumOptionsType, StrictRJSFSchema } from './types'; | ||
import { RJSFSchema, EnumOptionsType, StrictRJSFSchema, FormContextType, UiSchema } from './types'; | ||
import getUiOptions from './getUiOptions'; | ||
/** Gets the list of options from the schema. If the schema has an enum list, then those enum values are returned. The | ||
/** Gets the list of options from the `schema`. If the schema has an enum list, then those enum values are returned. The | ||
* labels for the options will be extracted from the non-standard, RJSF-deprecated `enumNames` if it exists, otherwise | ||
* the label will be the same as the `value`. If the schema has a `oneOf` or `anyOf`, then the value is the list of | ||
* `const` values from the schema and the label is either the `schema.title` or the value. | ||
* `const` values from the schema and the label is either the `schema.title` or the value. If a `uiSchema` is provided | ||
* and it has the `ui:enumNames` matched with `enum` or it has an associated `oneOf` or `anyOf` with a list of objects | ||
* containing `ui:title` then the UI schema values will replace the values from the schema. | ||
* | ||
* @param schema - The schema from which to extract the options list | ||
* @param [uiSchema] - The optional uiSchema from which to get alternate labels for the options | ||
* @returns - The list of options from the schema | ||
*/ | ||
export default function optionsList<S extends StrictRJSFSchema = RJSFSchema>( | ||
schema: S | ||
export default function optionsList<S extends StrictRJSFSchema = RJSFSchema, T = any, F extends FormContextType = any>( | ||
schema: S, | ||
uiSchema?: UiSchema<T, S, F> | ||
): EnumOptionsType<S>[] | undefined { | ||
// enumNames was deprecated in v5 and is intentionally omitted from the RJSFSchema type. | ||
// Cast the type to include enumNames so the feature still works. | ||
// TODO flip generics to move T first in v6 | ||
const schemaWithEnumNames = schema as S & { enumNames?: string[] }; | ||
if (schemaWithEnumNames.enumNames && process.env.NODE_ENV !== 'production') { | ||
console.warn('The enumNames property is deprecated and may be removed in a future major release.'); | ||
} | ||
if (schema.enum) { | ||
let enumNames: string[] | undefined; | ||
if (uiSchema) { | ||
const { enumNames: uiEnumNames } = getUiOptions<T, S, F>(uiSchema); | ||
enumNames = uiEnumNames; | ||
} | ||
if (!enumNames && schemaWithEnumNames.enumNames) { | ||
// enumNames was deprecated in v5 and is intentionally omitted from the RJSFSchema type. | ||
// Cast the type to include enumNames so the feature still works. | ||
if (process.env.NODE_ENV !== 'production') { | ||
console.warn( | ||
'The "enumNames" property in the schema is deprecated and will be removed in a future major release. Use the "ui:enumNames" property in the uiSchema instead.' | ||
); | ||
} | ||
enumNames = schemaWithEnumNames.enumNames; | ||
} | ||
return schema.enum.map((value, i) => { | ||
const label = (schemaWithEnumNames.enumNames && schemaWithEnumNames.enumNames[i]) || String(value); | ||
const label = enumNames?.[i] || String(value); | ||
return { label, value }; | ||
}); | ||
} | ||
const altSchemas = schema.oneOf || schema.anyOf; | ||
let altSchemas: S['anyOf'] | S['oneOf'] = undefined; | ||
let altUiSchemas: UiSchema<T, S, F> | undefined = undefined; | ||
if (schema.anyOf) { | ||
altSchemas = schema.anyOf; | ||
altUiSchemas = uiSchema?.anyOf; | ||
} else if (schema.oneOf) { | ||
altSchemas = schema.oneOf; | ||
altUiSchemas = uiSchema?.oneOf; | ||
} | ||
return ( | ||
altSchemas && | ||
altSchemas.map((aSchemaDef) => { | ||
altSchemas.map((aSchemaDef, index) => { | ||
const { title } = getUiOptions<T, S, F>(altUiSchemas?.[index]); | ||
const aSchema = aSchemaDef as S; | ||
const value = toConstant(aSchema); | ||
const label = aSchema.title || String(value); | ||
const label = title || aSchema.title || String(value); | ||
return { | ||
@@ -35,0 +60,0 @@ schema: aSchema, |
@@ -753,3 +753,3 @@ import type { | ||
/** The value change event handler; call it with the new value every time it changes */ | ||
onChange: (value: any) => void; | ||
onChange: (value: any, es?: ErrorSchema<T>, id?: string) => void; | ||
/** The input focus event handler; call it with the widget id and value */ | ||
@@ -894,2 +894,4 @@ onFocus: (id: string, value: any) => void; | ||
widget?: Widget<T, S, F> | string; | ||
/** Allows a user to provide a list of labels for enum values in the schema */ | ||
enumNames?: string[]; | ||
}; | ||
@@ -896,0 +898,0 @@ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1648000
19792