Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove duplicate dropdown custom renderer #312

Merged
merged 7 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { CombinatorRendererProps, RankedTester } from "@jsonforms/core"
import { useState } from "react"
import { useEffect, useState } from "react"
import { Box, FormControl } from "@chakra-ui/react"
import {
createCombinatorRenderInfos,
Expand All @@ -25,6 +25,8 @@ export function JsonFormsAnyOfControl({
uischema,
uischemas,
label,
handleChange,
indexOfFittingSchema,
}: CombinatorRendererProps) {
const anyOfRenderInfos = createCombinatorRenderInfos(
schema.anyOf ?? [],
Expand All @@ -35,25 +37,43 @@ export function JsonFormsAnyOfControl({
uischemas,
)

const variants = anyOfRenderInfos.map((anyOfRenderInfo) => ({
label: anyOfRenderInfo.label,
value: anyOfRenderInfo.label,
}))
const options = anyOfRenderInfos.map((anyOfRenderInfo) => {
const option = String(anyOfRenderInfo.schema.const || anyOfRenderInfo.label)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why choose schema.const over label?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The const resembles the actual value that the user will select in the case of a string anyOf, so it would be more appropriate to display that to the user. The label is only used for non-string anyOf (which will not have a const).


const [variant, setVariant] = useState(anyOfRenderInfos[0]?.label || "")
return {
label: option.charAt(0).toUpperCase() + option.slice(1),
value: option,
}
})

const [variant, setVariant] = useState("")

const onChange = (value: string) => {
setVariant(value)
handleChange(path, value)
}

useEffect(() => {
if (indexOfFittingSchema !== undefined) {
setVariant(options[indexOfFittingSchema]?.label || "")
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])

return (
<Box py={2}>
<FormControl isRequired>
<FormLabel>Variant</FormLabel>
<SingleSelect
value={variant}
name={label}
items={variants}
isClearable={false}
onChange={setVariant}
/>
</FormControl>
<>
<Box py="0.5rem">
<FormControl isRequired>
<FormLabel>{label || "Variant"}</FormLabel>
<SingleSelect
value={variant}
name={label}
items={options}
isClearable={false}
onChange={onChange}
/>
</FormControl>
</Box>

{anyOfRenderInfos.map(
(anyOfRenderInfo) =>
Expand All @@ -68,7 +88,7 @@ export function JsonFormsAnyOfControl({
/>
),
)}
</Box>
</>
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {
OwnPropsOfEnum,
RankedTester,
} from "@jsonforms/core"
import { useState } from "react"
import { useEffect } from "react"
import { Box, FormControl } from "@chakra-ui/react"
import { isEnumControl, rankWith } from "@jsonforms/core"
import { withJsonFormsEnumProps } from "@jsonforms/react"
Expand All @@ -26,9 +26,16 @@ export function JsonFormsDropdownControl({
options,
schema,
}: ControlProps & OwnPropsOfEnum) {
const [dropdownValue, setDropdownValue] = useState(data || "")
// Use the default value if it exists
useEffect(() => {
if (data !== undefined || data !== "") {
return
}
const value = schema.default || schema.const
handleChange(path, value)
}, [path, schema.default, schema.const, handleChange, data])

if (!options || (options.length === 1 && !!schema.default)) {
if (!options || (options.length === 1 && !!schema.const)) {
return null
}

Expand All @@ -42,12 +49,11 @@ export function JsonFormsDropdownControl({
<FormControl isRequired={required}>
<FormLabel description={description}>{label}</FormLabel>
<SingleSelect
value={dropdownValue}
value={data}
name={label}
items={items}
isClearable={false}
onChange={(value) => {
setDropdownValue(value)
handleChange(path, value)
}}
/>
Expand Down
Loading