-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use JSONForms for complex block editing
- Loading branch information
Showing
18 changed files
with
602 additions
and
764 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
12 changes: 12 additions & 0 deletions
12
apps/studio/src/features/editing-experience/components/ComplexEditorStateDrawer.tsx
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,12 @@ | ||
import React from 'react' | ||
import { Box } from '@chakra-ui/react' | ||
import FormBuilder from './form-builder/FormBuilder' | ||
|
||
export default function ComplexEditorStateDrawer(): JSX.Element { | ||
return ( | ||
<Box p={4}> | ||
<h1>Complex Editor State Drawer</h1> | ||
<FormBuilder component="infocards" /> | ||
</Box> | ||
) | ||
} |
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
166 changes: 0 additions & 166 deletions
166
apps/studio/src/features/editing-experience/components/FormBuilder.tsx
This file was deleted.
Oops, something went wrong.
66 changes: 66 additions & 0 deletions
66
apps/studio/src/features/editing-experience/components/form-builder/FormBuilder.tsx
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,66 @@ | ||
import { type JsonFormsRendererRegistryEntry } from '@jsonforms/core' | ||
import { JsonForms } from '@jsonforms/react' | ||
|
||
import { useState } from 'react' | ||
import IsomerSchema from '../../data/0.1.0.json' | ||
|
||
import { | ||
JsonFormsBooleanControl, | ||
jsonFormsBooleanControlTester, | ||
JsonFormsDropdownControl, | ||
jsonFormsDropdownControlTester, | ||
JsonFormsIntegerControl, | ||
jsonFormsIntegerControlTester, | ||
JsonFormsRadioControl, | ||
jsonFormsRadioControlTester, | ||
JsonFormsTextControl, | ||
jsonFormsTextControlTester, | ||
jsonFormsVerticalLayoutRenderer, | ||
jsonFormsVerticalLayoutTester, | ||
} from './renderers' | ||
|
||
const renderers: JsonFormsRendererRegistryEntry[] = [ | ||
{ tester: jsonFormsBooleanControlTester, renderer: JsonFormsBooleanControl }, | ||
{ | ||
tester: jsonFormsDropdownControlTester, | ||
renderer: JsonFormsDropdownControl, | ||
}, | ||
{ tester: jsonFormsIntegerControlTester, renderer: JsonFormsIntegerControl }, | ||
{ tester: jsonFormsTextControlTester, renderer: JsonFormsTextControl }, | ||
{ tester: jsonFormsRadioControlTester, renderer: JsonFormsRadioControl }, | ||
{ | ||
tester: jsonFormsVerticalLayoutTester, | ||
renderer: jsonFormsVerticalLayoutRenderer, | ||
}, | ||
] | ||
|
||
export interface FormBuilderProps { | ||
component: keyof typeof IsomerSchema.components.complex | ||
} | ||
|
||
export default function FormBuilder({ | ||
component, | ||
}: FormBuilderProps): JSX.Element { | ||
const { properties, ...rest } = IsomerSchema.components.complex[component] | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const { type, ...props } = properties | ||
const newSchema = { | ||
...rest, | ||
properties: props, | ||
components: IsomerSchema.components, | ||
} | ||
|
||
const [formData, setFormData] = useState({}) | ||
|
||
return ( | ||
<JsonForms | ||
schema={newSchema} | ||
data={formData} | ||
renderers={renderers} | ||
onChange={({ data }) => { | ||
console.log(data) | ||
setFormData(data) | ||
}} | ||
/> | ||
) | ||
} |
48 changes: 48 additions & 0 deletions
48
...editing-experience/components/form-builder/renderers/controls/JsonFormsBooleanControl.tsx
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,48 @@ | ||
import { Box, FormControl } from '@chakra-ui/react' | ||
import { | ||
isBooleanControl, | ||
rankWith, | ||
type ControlProps, | ||
type RankedTester, | ||
} from '@jsonforms/core' | ||
import { withJsonFormsControlProps } from '@jsonforms/react' | ||
import { | ||
FormErrorMessage, | ||
FormLabel, | ||
Switch, | ||
} from '@opengovsg/design-system-react' | ||
|
||
export const jsonFormsBooleanControlTester: RankedTester = rankWith( | ||
2, | ||
isBooleanControl, | ||
) | ||
|
||
export function JsonFormsBooleanControl({ | ||
data, | ||
label, | ||
id, | ||
enabled, | ||
handleChange, | ||
errors, | ||
path, | ||
description, | ||
}: ControlProps) { | ||
return ( | ||
<Box py={2}> | ||
<FormControl> | ||
<FormLabel description={description} htmlFor={id}> | ||
{label} | ||
</FormLabel> | ||
<Switch | ||
id={id} | ||
isDisabled={!enabled} | ||
checked={data || false} | ||
onChange={(e) => handleChange(path, e.target.checked)} | ||
/> | ||
<FormErrorMessage>{errors}</FormErrorMessage> | ||
</FormControl> | ||
</Box> | ||
) | ||
} | ||
|
||
export default withJsonFormsControlProps(JsonFormsBooleanControl) |
57 changes: 57 additions & 0 deletions
57
...diting-experience/components/form-builder/renderers/controls/JsonFormsDropdownControl.tsx
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,57 @@ | ||
import { Box, FormControl } from '@chakra-ui/react' | ||
import { | ||
isEnumControl, | ||
rankWith, | ||
type ControlProps, | ||
type OwnPropsOfEnum, | ||
type RankedTester, | ||
} from '@jsonforms/core' | ||
import { withJsonFormsEnumProps } from '@jsonforms/react' | ||
import { FormLabel, SingleSelect } from '@opengovsg/design-system-react' | ||
import { useState } from 'react' | ||
|
||
export const jsonFormsDropdownControlTester: RankedTester = rankWith( | ||
2, | ||
isEnumControl, | ||
) | ||
|
||
export function JsonFormsDropdownControl({ | ||
data, | ||
label, | ||
handleChange, | ||
path, | ||
description, | ||
required, | ||
options, | ||
}: ControlProps & OwnPropsOfEnum) { | ||
const [dropdownValue, setDropdownValue] = useState(data || '') | ||
|
||
if (!options) { | ||
return null | ||
} | ||
|
||
const items = options.map((option) => ({ | ||
label: option.label.charAt(0).toUpperCase() + option.label.slice(1), | ||
value: option.value, | ||
})) | ||
|
||
return ( | ||
<Box py={2}> | ||
<FormControl isRequired={required}> | ||
<FormLabel description={description}>{label}</FormLabel> | ||
<SingleSelect | ||
value={dropdownValue} | ||
name={label} | ||
items={items} | ||
isClearable={false} | ||
onChange={(value) => { | ||
setDropdownValue(value) | ||
handleChange(path, value) | ||
}} | ||
/> | ||
</FormControl> | ||
</Box> | ||
) | ||
} | ||
|
||
export default withJsonFormsEnumProps(JsonFormsDropdownControl) |
Oops, something went wrong.