Skip to content

Commit

Permalink
feat: don't set the first item as default for anyOf
Browse files Browse the repository at this point in the history
  • Loading branch information
dcshzj committed Jul 18, 2024
1 parent 3457c9c commit e1b9ae8
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 5 deletions.
1 change: 1 addition & 0 deletions apps/studio/src/constants/formBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
export const JSON_FORMS_RANKING = {
ArrayControl: 4,
BooleanControl: 2,
ConstControl: 3,
DropdownControl: 2,
IntegerControl: 4,
TextControl: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
jsonFormsArrayControlTester,
JsonFormsBooleanControl,
jsonFormsBooleanControlTester,
JsonFormsConstControl,
jsonFormsConstControlTester,
JsonFormsDropdownControl,
jsonFormsDropdownControlTester,
jsonFormsGroupLayoutRenderer,
Expand All @@ -36,6 +38,7 @@ const renderers: JsonFormsRendererRegistryEntry[] = [
{ tester: jsonFormsObjectControlTester, renderer: JsonFormsObjectControl },
{ tester: jsonFormsArrayControlTester, renderer: JsonFormsArrayControl },
{ tester: jsonFormsBooleanControlTester, renderer: JsonFormsBooleanControl },
{ tester: jsonFormsConstControlTester, renderer: JsonFormsConstControl },
{
tester: jsonFormsDropdownControlTester,
renderer: JsonFormsDropdownControl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export function JsonFormsAnyOfControl({
uischema,
uischemas,
label,
handleChange,
}: CombinatorRendererProps) {
const anyOfRenderInfos = createCombinatorRenderInfos(
schema.anyOf ?? [],
Expand All @@ -35,7 +36,7 @@ export function JsonFormsAnyOfControl({
uischemas,
)

const variants = anyOfRenderInfos.map((anyOfRenderInfo) => {
const options = anyOfRenderInfos.map((anyOfRenderInfo) => {
const option = String(anyOfRenderInfo.schema.const || anyOfRenderInfo.label)

return {
Expand All @@ -44,7 +45,12 @@ export function JsonFormsAnyOfControl({
}
})

const [variant, setVariant] = useState(anyOfRenderInfos[0]?.label || "")
const [variant, setVariant] = useState("")

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

return (
<>
Expand All @@ -54,9 +60,9 @@ export function JsonFormsAnyOfControl({
<SingleSelect
value={variant}
name={label}
items={variants}
items={options}
isClearable={false}
onChange={setVariant}
onChange={onChange}
/>
</FormControl>
</Box>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { ControlProps, RankedTester } from "@jsonforms/core"
import { useEffect } from "react"
import { and, isStringControl, rankWith, schemaMatches } from "@jsonforms/core"
import { withJsonFormsControlProps } from "@jsonforms/react"

import { JSON_FORMS_RANKING } from "~/constants/formBuilder"

export const jsonFormsConstControlTester: RankedTester = rankWith(
JSON_FORMS_RANKING.ConstControl,
and(
isStringControl,
schemaMatches((schema) => schema.const !== undefined),
),
)

export function JsonFormsConstControl({
handleChange,
path,
schema,
}: ControlProps) {
// TODO: Make the default value persist inside JSONForms
useEffect(() => {
handleChange(path, schema.const)
}, [handleChange, path, schema])

return null
}

export default withJsonFormsControlProps(JsonFormsConstControl)
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, useState } from "react"
import { Box, FormControl } from "@chakra-ui/react"
import { isEnumControl, rankWith } from "@jsonforms/core"
import { withJsonFormsEnumProps } from "@jsonforms/react"
Expand All @@ -28,6 +28,11 @@ export function JsonFormsDropdownControl({
}: ControlProps & OwnPropsOfEnum) {
const [dropdownValue, setDropdownValue] = useState(data || "")

// Use the default value if it exists
useEffect(() => {
handleChange(path, schema.default || schema.const)
}, [path, schema.default, schema.const, handleChange])

if (!options || (options.length === 1 && !!schema.const)) {
return null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export {
default as JsonFormsBooleanControl,
jsonFormsBooleanControlTester,
} from "./JsonFormsBooleanControl"
export {
default as JsonFormsConstControl,
jsonFormsConstControlTester,
} from "./JsonFormsConstControl"
export {
default as JsonFormsDropdownControl,
jsonFormsDropdownControlTester,
Expand Down

0 comments on commit e1b9ae8

Please sign in to comment.