Skip to content

Commit

Permalink
Narrow typing
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFarmer committed May 20, 2024
1 parent 54be81e commit 5b5f29b
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/controls/BooleanControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ export function BooleanControl({

const showTooltip =
!showDescription && !isDescriptionHidden(visible, description, true, true)
const uiSchema = uischema as ControlUISchema<typeof uischema> & ControlElement
const formItemProps = uiSchema.formItemProps ?? {}
const uiSchema = uischema as ControlUISchema<unknown> | ControlElement
const formItemProps = "formItemProps" in uiSchema ? uiSchema.formItemProps : {}
const tooltip = showTooltip && description ? description : undefined

return (
Expand Down
5 changes: 2 additions & 3 deletions src/controls/NumericControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ export const NumericControl = (props: ControlProps & RendererProps) => {
{ required: props.required, message: `${props.label} is required` },
]

const uiSchema = props.uischema as ControlUISchema<typeof props.uischema> &
ControlElement
const formItemProps = uiSchema.formItemProps ?? {}
const uiSchema = props.uischema as ControlUISchema<unknown> | ControlElement
const formItemProps = "formItemProps" in uiSchema ? uiSchema.formItemProps : {}

return (
<Form.Item
Expand Down
5 changes: 2 additions & 3 deletions src/controls/NumericSliderControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ export const NumericSliderControl = (props: ControlProps & RendererProps) => {
{ required: props.required, message: `${props.label} is required` },
]

const uiSchema = props.uischema as ControlUISchema<typeof props.uischema> &
ControlElement
const formItemProps = uiSchema.formItemProps ?? {}
const uiSchema = props.uischema as ControlUISchema<unknown> | ControlElement
const formItemProps = "formItemProps" in uiSchema ? uiSchema.formItemProps : {}

return (
<Form.Item
Expand Down
7 changes: 6 additions & 1 deletion src/controls/ObjectArrayControl.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
ControlElement,
Helpers,
ArrayLayoutProps,
composePaths,
Expand All @@ -12,7 +13,7 @@ import {
import { Flex, Form, List, Button } from "antd"
import range from "lodash.range"
import { useEffect, useMemo } from "react"
import { ArrayControlOptions } from "../ui-schema"
import { ArrayControlOptions, ControlUISchema } from "../ui-schema"
import { usePreviousValue } from "../common/usePreviousValue"
import React from "react"

Expand Down Expand Up @@ -58,6 +59,9 @@ export function ObjectArrayControl({
}
})

const uiSchema = uischema as ControlUISchema<unknown> | ControlElement
const formItemProps = "formItemProps" in uiSchema ? uiSchema.formItemProps : {}

const labelDescription = Helpers.createLabelDescriptionFrom(uischema, schema)
const label = labelDescription.show ? labelDescription.text : ""

Expand Down Expand Up @@ -86,6 +90,7 @@ export function ObjectArrayControl({
required={required}
rules={[{ required: required, message: `${label} is required` }]}
validateTrigger={["onBlur"]}
{...formItemProps}
>
<>{label}</>
<List<unknown>
Expand Down
4 changes: 2 additions & 2 deletions src/controls/PrimitiveArrayControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ export function PrimitiveArrayControl({
const labelDescription = Helpers.createLabelDescriptionFrom(uischema, schema)
const label = labelDescription.text || props.label // nullish coalescing doesn't work here because labelDescription.text can be an empty string =(

const uiSchema = uischema as ControlUISchema<typeof uischema> & ControlElement
const uiSchema = uischema as ControlUISchema<unknown> | ControlElement
const options: ArrayControlOptions = uiSchema.options ?? {}
const formItemProps = uiSchema.formItemProps ?? {}
const formItemProps = "formItemProps" in uiSchema ? uiSchema.formItemProps : {}

return (
<Form.Item
Expand Down
6 changes: 3 additions & 3 deletions src/controls/TextControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function TextControl({
id,
uischema,
}: TextControlProps) {
const uiSchema = uischema as ControlUISchema<typeof uischema> & ControlElement
const uiSchema = uischema as ControlUISchema<unknown> | ControlElement
const setInitialValue = useCallback(
(value: unknown) => {
if (typeof value !== "number") return value
Expand All @@ -38,8 +38,8 @@ export function TextControl({
const ariaLabel = label || schema.description
const options: TextControlOptions =
(uiSchema.options as TextControlOptions) ?? {}
const formItemProps = uiSchema.formItemProps ?? {}
const tooltip = options.tooltip ? options.tooltip : formItemProps.tooltip
const formItemProps = "formItemProps" in uiSchema ? uiSchema.formItemProps : {}
const tooltip = options.tooltip ? options.tooltip : formItemProps?.tooltip
const placeholderText = options.placeholderText
const form = Form.useFormInstance()
const rules: Rule[] = [
Expand Down
4 changes: 2 additions & 2 deletions src/controls/combinators/AnyOfControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ export function AnyOfControl({

const labelDescription = Helpers.createLabelDescriptionFrom(uischema, schema)

const uiSchema = uischema as ControlUISchema<typeof uischema> & ControlElement
const formItemProps = uiSchema.formItemProps ?? {}
const uiSchema = uischema as ControlUISchema<unknown> | ControlElement
const formItemProps = "formItemProps" in uiSchema ? uiSchema.formItemProps : {}

return (
<Space direction="vertical" style={{ width: "100%" }} size="middle">
Expand Down
4 changes: 2 additions & 2 deletions src/controls/combinators/OneOfControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export function OneOfControl({
uischemas,
)

const uiSchema = uischema as ControlUISchema<typeof uischema> & ControlElement
const formItemProps = uiSchema.formItemProps ?? {}
const uiSchema = uischema as ControlUISchema<unknown> | ControlElement
const formItemProps = "formItemProps" in uiSchema ? uiSchema.formItemProps : {}

return (
<Form.Item
Expand Down

0 comments on commit 5b5f29b

Please sign in to comment.