Skip to content

Commit

Permalink
Narrow any type
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFarmer committed Jun 28, 2024
1 parent 491edd8 commit f743e32
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
28 changes: 17 additions & 11 deletions src/controls/EnumControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ type ControlProps = Omit<JSFControlProps, "uischema"> & {
uischema: ControlUISchema<unknown> | JSFControlProps["uischema"]
}

const isStringOrNumber = (value: unknown): boolean => {
return typeof value === "string" || typeof value === "number"
}

export const EnumControl = (props: ControlProps) => {
if (!props.visible) return null

Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/ui-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export type EnumOption = (typeof EnumOptions)[number]

export type EnumControlOptions = {
optionType?: EnumOption
enumValueToLabelMap?: Record<string, string>
enumValueToLabelMap?: Record<string | number, string>
}

export type TextControlType = "multiline" | "password" | "singleline"
Expand Down

0 comments on commit f743e32

Please sign in to comment.