From f743e320bc083dd6a298543b339d7c953559f2ae Mon Sep 17 00:00:00 2001 From: Farmer Date: Fri, 28 Jun 2024 12:12:38 -0400 Subject: [PATCH] Narrow any type --- src/controls/EnumControl.tsx | 28 +++++++++++++++++----------- src/ui-schema.ts | 2 +- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/controls/EnumControl.tsx b/src/controls/EnumControl.tsx index 933a056..41bf242 100644 --- a/src/controls/EnumControl.tsx +++ b/src/controls/EnumControl.tsx @@ -8,6 +8,10 @@ type ControlProps = Omit & { uischema: ControlUISchema | JSFControlProps["uischema"] } +const isStringOrNumber = (value: unknown): boolean => { + return typeof value === "string" || typeof value === "number" +} + export const EnumControl = (props: ControlProps) => { if (!props.visible) return null @@ -23,18 +27,20 @@ export const EnumControl = (props: ControlProps) => { const appliedUiSchemaOptions = props.uischema.options as EnumControlOptions + const enumValue = props.schema.enum const enumValueToLabelMap = appliedUiSchemaOptions?.enumValueToLabelMap - const options = props.schema.enum - ? props.schema.enum?.map((value) => ({ - label: - enumValueToLabelMap && - typeof value === "string" && - !!enumValueToLabelMap[value] - ? enumValueToLabelMap[value] - : (value as string), - value: value as string, - })) - : [] + const options = + enumValue && isStringOrNumber(enumValue) + ? enumValue.map((value) => ({ + label: + enumValueToLabelMap && + typeof value === "string" && + !!enumValueToLabelMap[value] + ? enumValueToLabelMap[value] + : (value as string), + value: value as string, + })) + : [] let selector switch (appliedUiSchemaOptions?.optionType) { diff --git a/src/ui-schema.ts b/src/ui-schema.ts index c15656d..a370dce 100644 --- a/src/ui-schema.ts +++ b/src/ui-schema.ts @@ -158,7 +158,7 @@ export type EnumOption = (typeof EnumOptions)[number] export type EnumControlOptions = { optionType?: EnumOption - enumValueToLabelMap?: Record + enumValueToLabelMap?: Record } export type TextControlType = "multiline" | "password" | "singleline"