Skip to content

Commit

Permalink
fix: run prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
DrewHoo committed Mar 13, 2024
1 parent ecd6740 commit 8748a8d
Show file tree
Hide file tree
Showing 17 changed files with 209 additions and 148 deletions.
1 change: 0 additions & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ jobs:
shell: bash
run: pnpm pack


- name: Run lint
run: pnpm lint

Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
rendererRegistryEntries,
cellRegistryEntries,
} from "jsonforms-antd-renderers"

;<JsonForms
schema={schema}
renderers={rendererRegistryEntries}
Expand Down
70 changes: 46 additions & 24 deletions src/antd/InputNumber.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { ReactElement } from "react"
import { ControlProps, RendererProps } from "@jsonforms/core"
import { InputNumber as AntdInputNumber } from "antd"
import { InputNumberOptions } from "../ui-schema"
import { coerceToInteger, coerceToNumber, decimalToPercentage, percentageStringToDecimal } from "../controls/utils"
import {
coerceToInteger,
coerceToNumber,
decimalToPercentage,
percentageStringToDecimal,
} from "../controls/utils"

type InputNumber = ReactElement<typeof AntdInputNumber>
type AntdInputNumberProps = React.ComponentProps<typeof AntdInputNumber>
Expand All @@ -13,12 +18,24 @@ export const InputNumber = (props: InputNumberProps): InputNumber => {
const ariaLabel = props.label || schema.description || "Value"

const defaultValue = schema.default as number | undefined
const isDataNonNullObject = typeof props.data === "object" && props.data !== undefined && props.data !== null
const isDataEmptyObj = isDataNonNullObject ? Object.keys(props.data as object).length === 0 : false
const value = props.data === undefined || isDataEmptyObj ? defaultValue : props.data as number
const isDataNonNullObject =
typeof props.data === "object" &&
props.data !== undefined &&
props.data !== null
const isDataEmptyObj = isDataNonNullObject
? Object.keys(props.data as object).length === 0
: false
const value =
props.data === undefined || isDataEmptyObj
? defaultValue
: (props.data as number)

const numberType = schema.type
const isInteger = (typeof numberType === "string" && numberType === "integer") || (Array.isArray(numberType) && numberType.length === 1 && numberType.includes("integer"))
const isInteger =
(typeof numberType === "string" && numberType === "integer") ||
(Array.isArray(numberType) &&
numberType.length === 1 &&
numberType.includes("integer"))
const handleChange = (value: number | string | null) => {
if (typeof value === "number") {
if (isInteger) {
Expand All @@ -34,14 +51,17 @@ export const InputNumber = (props: InputNumberProps): InputNumber => {
const options = props.uischema.options as InputNumberOptions
const addonAfter = options?.addonAfter
const addonBefore = options?.addonBefore
const isPercentage = addonAfter && typeof addonAfter === "string" ? addonAfter?.trim() === "%" : false
const isPercentage =
addonAfter && typeof addonAfter === "string"
? addonAfter?.trim() === "%"
: false

const min = schema.minimum
const max = schema.maximum
const marginLeft = min === undefined || max === undefined ? 0 : 16
const style = { marginLeft: marginLeft, width: "100%" }

const formatter = ((value?: string | number): string => {
const formatter = (value?: string | number): string => {
if (value !== "" && value !== undefined) {
if (isPercentage) {
const valueFloat = typeof value === "string" ? parseFloat(value) : value
Expand All @@ -51,8 +71,8 @@ export const InputNumber = (props: InputNumberProps): InputNumber => {
}
}
return ""
})
const parser = ((value?: string): number | undefined => {
}
const parser = (value?: string): number | undefined => {
const isNumeric = value ? !isNaN(Number(value)) : false
if (isNumeric && value !== undefined) {
if (isPercentage) {
Expand All @@ -64,20 +84,22 @@ export const InputNumber = (props: InputNumberProps): InputNumber => {
}
}
return undefined
})
}

return <AntdInputNumber
aria-label={ariaLabel}
defaultValue={defaultValue}
value={value}
onChange={(value) => handleChange(value)}
min={min}
max={max}
addonBefore={addonBefore}
addonAfter={addonAfter}
style={style}
formatter={formatter}
parser={parser as AntdInputNumberProps["parser"]}
controls={false}
/>
return (
<AntdInputNumber
aria-label={ariaLabel}
defaultValue={defaultValue}
value={value}
onChange={(value) => handleChange(value)}
min={min}
max={max}
addonBefore={addonBefore}
addonAfter={addonAfter}
style={style}
formatter={formatter}
parser={parser as AntdInputNumberProps["parser"]}
controls={false}
/>
)
}
77 changes: 56 additions & 21 deletions src/antd/Slider.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
import { ReactElement } from "react"
import { ControlProps, RendererProps } from "@jsonforms/core"
import { Slider as AntdSlider, SliderSingleProps as AntdSliderSingleProps } from "antd"
import { coerceToInteger, coerceToNumber, decimalToPercentage } from "../controls/utils"
import {
Slider as AntdSlider,
SliderSingleProps as AntdSliderSingleProps,
} from "antd"
import {
coerceToInteger,
coerceToNumber,
decimalToPercentage,
} from "../controls/utils"

type SliderProps = AntdSliderSingleProps & RendererProps & ControlProps

export const Slider = (props: SliderProps): ReactElement<typeof AntdSlider> => {
const isDataNonNullObject = typeof props.data === "object" && props.data !== undefined && props.data !== null
const isDataEmptyObj = isDataNonNullObject ? Object.keys(props.data as object).length === 0 : false
const defaultValue = typeof props.schema?.default === "number" ? props.schema.default : props.schema.minimum
const value = props.data === undefined || isDataEmptyObj ? defaultValue : props.data as number
const isDataNonNullObject =
typeof props.data === "object" &&
props.data !== undefined &&
props.data !== null
const isDataEmptyObj = isDataNonNullObject
? Object.keys(props.data as object).length === 0
: false
const defaultValue =
typeof props.schema?.default === "number"
? props.schema.default
: props.schema.minimum
const value =
props.data === undefined || isDataEmptyObj
? defaultValue
: (props.data as number)

const addonAfter = props.uischema.options?.addonAfter as string | undefined
const addonBefore = props.uischema.options?.addonBefore as string | undefined
Expand All @@ -20,32 +38,49 @@ export const Slider = (props: SliderProps): ReactElement<typeof AntdSlider> => {
const step = props.schema.multipleOf

const numberType = props.schema.type
const isInteger = (typeof numberType === "string" && numberType === "integer") || (Array.isArray(numberType) && numberType.includes("integer"))
const isInteger =
(typeof numberType === "string" && numberType === "integer") ||
(Array.isArray(numberType) && numberType.includes("integer"))
const handleChange = (value: number) => {
if ((min !== undefined && value >= min) && (max !== undefined && value <= max)) {
if (
min !== undefined &&
value >= min &&
max !== undefined &&
value <= max
) {
if (isInteger) {
props.handleChange(props.path, value !== null ? coerceToInteger(value) : value)
props.handleChange(
props.path,
value !== null ? coerceToInteger(value) : value,
)
} else {
props.handleChange(props.path, value !== null ? coerceToNumber(value) : value)
props.handleChange(
props.path,
value !== null ? coerceToNumber(value) : value,
)
}
}
}

const tooltip = {
formatter: (value?: number) => {
const tooltipValue = value !== undefined ? value : defaultValue
const formattedTooltipValue = isPercentage ? decimalToPercentage(tooltipValue) : tooltipValue
const formattedTooltipValue = isPercentage
? decimalToPercentage(tooltipValue)
: tooltipValue
return `${addonBefore ? addonBefore : ""}${formattedTooltipValue}${addonAfter ? addonAfter : ""}`
}
},
}

return <AntdSlider
defaultValue={defaultValue}
value={value}
onChange={(value) => handleChange(value)}
min={min}
max={max}
step={step}
tooltip={tooltip}
/>
return (
<AntdSlider
defaultValue={defaultValue}
value={value}
onChange={(value) => handleChange(value)}
min={min}
max={max}
step={step}
tooltip={tooltip}
/>
)
}
2 changes: 1 addition & 1 deletion src/controls/BooleanControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,5 @@ export function BooleanControl({
config={config}
/>
</Form.Item>
);
)
}
58 changes: 30 additions & 28 deletions src/controls/NumericControls/NumericControl.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
numericUSDUISchema,
} from "../../testSchemas/numericSchema/numericSchema"


describe("NumericControl", () => {
it("does not fall back to default if value is empty", () => {
render({
Expand All @@ -27,7 +26,7 @@ describe("NumericControl", () => {
})

it("calls onChange with number values", async () => {
let data = { numericValue: 42.00 }
let data = { numericValue: 42.0 }
render({
schema: numericTheNumberSchema,
data,
Expand All @@ -40,7 +39,7 @@ describe("NumericControl", () => {
await userEvent.type(screen.getByRole("spinbutton"), "42.00")

await waitFor(() => {
expect(data).toBe(42.00)
expect(data).toBe(42.0)
})
})

Expand Down Expand Up @@ -84,16 +83,19 @@ describe("NumericControl", () => {
expect(screen.queryByText("Magnitude")).toBeNull()
})

it.each([[0], [100]])("renders when data of %s is included", (dataVal: number) => {
const data = { numericValue: dataVal}
render({
data: data,
schema: numericTheNumberSchema, // this has a default of 42.42
uischema: numericUISchema,
})
screen.getByText("The Number")
expect(screen.getByRole("spinbutton")).toHaveValue(`${dataVal}`)
})
it.each([[0], [100]])(
"renders when data of %s is included",
(dataVal: number) => {
const data = { numericValue: dataVal }
render({
data: data,
schema: numericTheNumberSchema, // this has a default of 42.42
uischema: numericUISchema,
})
screen.getByText("The Number")
expect(screen.getByRole("spinbutton")).toHaveValue(`${dataVal}`)
},
)

it("renders default value when no data is provided", () => {
render({
Expand Down Expand Up @@ -121,7 +123,7 @@ describe("NumericControl", () => {
})
})

it ("shows error message onBlur when field is required and empty", async () => {
it("shows error message onBlur when field is required and empty", async () => {
render({
schema: numericTheNumberSchema,
uischema: numericUISchema,
Expand All @@ -132,25 +134,25 @@ describe("NumericControl", () => {
await screen.findByText("The Number is required")
})

it ("shows units next to text input if set in UI schema", async () => {
it("shows units next to text input if set in UI schema", async () => {
render({
schema: numericPriceSchema,
uischema: numericUSDUISchema,
})
await screen.findByText("$")
})

it.each([
numericSheepSchema,
numericBeansSchema,
])("is treated as an integer if the schema type is integer or the type is an array with only integer", async (schema: JSONSchema) => {
render({
schema: schema,
uischema: numericUISchema,
})
const input = screen.getByRole("spinbutton")
await userEvent.type(input, "123.45") // try to input a float
await userEvent.tab()
expect(input).toHaveValue("123") // it should be rounded to an integer
})
it.each([numericSheepSchema, numericBeansSchema])(
"is treated as an integer if the schema type is integer or the type is an array with only integer",
async (schema: JSONSchema) => {
render({
schema: schema,
uischema: numericUISchema,
})
const input = screen.getByRole("spinbutton")
await userEvent.type(input, "123.45") // try to input a float
await userEvent.tab()
expect(input).toHaveValue("123") // it should be rounded to an integer
},
)
})
6 changes: 3 additions & 3 deletions src/controls/NumericControls/NumericControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { Col, Form } from "antd"
import { Rule } from "antd/lib/form"
import { InputNumber } from "../../antd/InputNumber"


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

const initialValue = typeof props.schema.default === "number" ? props.schema.default : undefined
const initialValue =
typeof props.schema.default === "number" ? props.schema.default : undefined

const rules: Rule[] = [
{ required: props.required, message: `${props.label} is required` },
Expand All @@ -23,7 +23,7 @@ export const NumericControl = (props: ControlProps & RendererProps) => {
rules={rules}
validateTrigger={["onBlur"]}
>
<Col span={18}>{InputNumber({...props})}</Col>
<Col span={18}>{InputNumber({ ...props })}</Col>
</Form.Item>
)
}
6 changes: 3 additions & 3 deletions src/controls/NumericControls/NumericSliderControl.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe("NumericSliderControl", () => {
})

it("calls onChange with number values", async () => {
let data = { numericRangeValue: 42.00 }
let data = { numericRangeValue: 42.0 }
render({
schema: numericSliderBasisPointsSchema,
data,
Expand All @@ -80,11 +80,11 @@ describe("NumericSliderControl", () => {
await userEvent.type(screen.getByRole("spinbutton"), "42.00")

await waitFor(() => {
expect(data).toEqual({ numericRangeValue: 42.00 })
expect(data).toEqual({ numericRangeValue: 42.0 })
})
})

it ("shows units in tooltip if set in UI schema", async () => {
it("shows units in tooltip if set in UI schema", async () => {
render({
schema: numericSliderFinalGradeSchema,
uischema: numericSliderPercentageUISchema,
Expand Down
Loading

0 comments on commit 8748a8d

Please sign in to comment.