-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Control, types, regsitry entry, stories * Revert change to pnpm-lock * Type error * Reduce rank for due to conflict with oneOf/anyOf * Add enumValueToLabelMap * Format * Add tests * Add tests * Comment * Fix test * Format * Fix test * Cast on cast crime * Fix * Format' * Narrow any type * Fix * Format * Fix * Format
- Loading branch information
1 parent
0f47e6d
commit 6da9a19
Showing
8 changed files
with
400 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { test, expect, vi } from "vitest" | ||
import { screen, waitFor } from "@testing-library/react" | ||
import { userEvent } from "@testing-library/user-event" | ||
import { render } from "../common/test-render" | ||
import { | ||
enumPSISchema, | ||
enumPSIUISchema, | ||
enumProfessionSchema, | ||
enumProfessionUISchema, | ||
enumSnakeCaseSchema, | ||
enumSnakeCaseUISchema, | ||
} from "../testSchemas/enumSchema" | ||
|
||
test("renders the enum component as radio optionType", () => { | ||
render({ | ||
schema: enumPSISchema, | ||
uischema: enumPSIUISchema, | ||
}) | ||
|
||
const radioButtons = screen.getAllByRole("radio") | ||
expect(radioButtons).toHaveLength(3) | ||
}) | ||
|
||
test("renders the enum component as dropdown optionType", () => { | ||
render({ | ||
schema: enumProfessionSchema, | ||
uischema: enumProfessionUISchema, | ||
}) | ||
|
||
screen.getByRole("combobox") | ||
}) | ||
|
||
test("renders the enum component with custom titles", async () => { | ||
render({ | ||
schema: enumSnakeCaseSchema, | ||
uischema: enumSnakeCaseUISchema, | ||
}) | ||
|
||
await userEvent.click(screen.getByRole("combobox")) | ||
screen.getByTitle("Option 1") | ||
screen.getByTitle("Option 2") | ||
screen.getByTitle("Option 3") | ||
}) | ||
|
||
test("handles onChange event correctly", async () => { | ||
const updateData = vi.fn() | ||
render({ | ||
schema: enumProfessionSchema, | ||
data: { profession: "Bob Ross Impersonator" }, | ||
onChange: (result) => { | ||
updateData(result) | ||
}, | ||
}) | ||
|
||
screen.getByTitle("Bob Ross Impersonator") | ||
const combobox = screen.getByRole("combobox") | ||
|
||
await userEvent.click(combobox) | ||
await userEvent.click(screen.getByTitle("Footballer")) | ||
await waitFor(() => | ||
expect(updateData).toHaveBeenLastCalledWith({ | ||
data: { profession: "Footballer" }, | ||
errors: [], | ||
}), | ||
) | ||
|
||
await userEvent.click(combobox) | ||
await userEvent.click(screen.getByTitle("Software Engineer")) | ||
await waitFor(() => | ||
expect(updateData).toHaveBeenLastCalledWith({ | ||
data: { profession: "Software Engineer" }, | ||
errors: [], | ||
}), | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import type { ControlProps as JSFControlProps } from "@jsonforms/core" | ||
import { Col, Form, Select, Segmented, Radio } from "antd" | ||
import type { Rule } from "antd/es/form" | ||
import { EnumControlOptions, ControlUISchema } from "../ui-schema" | ||
import { withJsonFormsControlProps } from "@jsonforms/react" | ||
|
||
type ControlProps = Omit<JSFControlProps, "uischema"> & { | ||
uischema: ControlUISchema<unknown> | JSFControlProps["uischema"] | ||
} | ||
|
||
const isStringOrNumberArray = (arr: unknown[]): boolean => { | ||
return arr.every( | ||
(value) => typeof value === "string" || typeof value === "number", | ||
) | ||
} | ||
|
||
export const EnumControl = (props: ControlProps) => { | ||
if (!props.visible) return null | ||
|
||
const rules: Rule[] = [ | ||
{ required: props.required, message: `${props.label} is required` }, | ||
] | ||
|
||
const formItemProps = | ||
"formItemProps" in props.uischema ? props.uischema.formItemProps : {} | ||
|
||
const defaultValue = | ||
(props.data as unknown) ?? (props.schema.default as unknown) | ||
|
||
const appliedUiSchemaOptions = props.uischema.options as EnumControlOptions | ||
|
||
const enumValue = props.schema.enum | ||
const enumValueToLabelMap = appliedUiSchemaOptions?.enumValueToLabelMap | ||
const options = | ||
enumValue && isStringOrNumberArray(enumValue) | ||
? enumValue.map((value: string | number) => ({ | ||
label: enumValueToLabelMap ? enumValueToLabelMap[value] : value, | ||
value: value, | ||
})) | ||
: [] | ||
|
||
let selector | ||
switch (appliedUiSchemaOptions?.optionType) { | ||
case "radio": | ||
selector = ( | ||
<Radio.Group | ||
defaultValue={defaultValue} | ||
options={options} | ||
onChange={(e) => { | ||
props.handleChange(props.path, e.target.value) | ||
}} | ||
optionType="button" | ||
buttonStyle="solid" | ||
/> | ||
) | ||
break | ||
case "segmented": | ||
selector = ( | ||
<Segmented | ||
defaultValue={defaultValue} | ||
options={options} | ||
onChange={(value) => { | ||
props.handleChange(props.path, value) | ||
}} | ||
/> | ||
) | ||
break | ||
case "dropdown": | ||
default: | ||
selector = ( | ||
<Select | ||
showSearch | ||
defaultValue={defaultValue} | ||
options={options} | ||
onChange={(option) => { | ||
props.handleChange(props.path, option) | ||
}} | ||
filterOption={(inputValue, option) => { | ||
const optionValue = option?.value?.toString() || "" | ||
return ( | ||
optionValue.toUpperCase().indexOf(inputValue.toUpperCase()) !== -1 | ||
) | ||
}} | ||
/> | ||
) | ||
break | ||
} | ||
|
||
return ( | ||
<Form.Item | ||
label={props.label} | ||
id={props.id} | ||
name={props.path} | ||
required={props.required} | ||
initialValue={defaultValue} | ||
rules={rules} | ||
validateTrigger={["onBlur"]} | ||
{...formItemProps} | ||
> | ||
<Col span={18}>{selector}</Col> | ||
</Form.Item> | ||
) | ||
} | ||
|
||
export const EnumRenderer = withJsonFormsControlProps(EnumControl) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.