-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added Dynamic UI Schema Generation (#80)
- Loading branch information
1 parent
85eed5e
commit 1681d12
Showing
12 changed files
with
115 additions
and
104 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import Form from '@rjsf/chakra-ui'; | ||
import { RJSFSchema } from '@rjsf/utils'; | ||
import validator from '@rjsf/validator-ajv8'; | ||
|
||
import ObjectFieldTemplate from '@/components/JSONSchemaForm/rjsf/ObjectFieldTemplate'; | ||
import TitleFieldTemplate from '@/components/JSONSchemaForm/rjsf/TitleFieldTemplate'; | ||
import FieldTemplate from '@/components/JSONSchemaForm/rjsf/FieldTemplate'; | ||
import BaseInputTemplate from '@/components/JSONSchemaForm/rjsf/BaseInputTemplate'; | ||
import DescriptionFieldTemplate from '@/components/JSONSchemaForm/rjsf/DescriptionFieldTemplate'; | ||
import { FormProps } from '@rjsf/core'; | ||
|
||
type JSONSchemaFormProps = { | ||
schema: RJSFSchema; | ||
uiSchema: Record<string, string>; | ||
onSubmit: (formData: FormData) => void; | ||
onChange?: (formData: FormData) => void; | ||
children?: JSX.Element; | ||
formData?: unknown; | ||
}; | ||
|
||
const JSONSchemaForm = ({ | ||
schema, | ||
uiSchema, | ||
onSubmit, | ||
onChange, | ||
children, | ||
formData, | ||
}: JSONSchemaFormProps): JSX.Element => { | ||
const templateOverrides: FormProps<any, RJSFSchema, any>['templates'] = { | ||
ObjectFieldTemplate: ObjectFieldTemplate, | ||
TitleFieldTemplate: TitleFieldTemplate, | ||
FieldTemplate: FieldTemplate, | ||
BaseInputTemplate: BaseInputTemplate, | ||
DescriptionFieldTemplate: DescriptionFieldTemplate, | ||
}; | ||
return ( | ||
<Form | ||
// TODO: Removed Hardcoded UI Schema | ||
// uiSchema={ | ||
// connectorSchema.title ? uiSchemas[connectorSchema.title.toLowerCase()] : undefined | ||
// } | ||
uiSchema={uiSchema} | ||
schema={schema} | ||
validator={validator} | ||
templates={templateOverrides} | ||
formData={formData} | ||
onSubmit={({ formData }) => onSubmit(formData)} | ||
onChange={({ formData }) => onChange?.(formData)} | ||
> | ||
{children} | ||
</Form> | ||
); | ||
}; | ||
|
||
export default JSONSchemaForm; |
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 @@ | ||
export { default } from './JSONSchemaForm'; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,26 @@ | ||
export const generateUiSchema = ( | ||
schemaProperties: object, | ||
uiSchema: Record<string, any> = {}, | ||
path: string[] = [], | ||
): Record<string, string> => { | ||
Object.entries(schemaProperties).forEach(([key, value]) => { | ||
if (key === 'properties' && typeof value === 'object' && value !== null) { | ||
generateUiSchema(value, uiSchema, path); | ||
} else if (typeof value === 'object' && value !== null) { | ||
const nestedObject = key === 'properties' ? path : path.concat(key); | ||
generateUiSchema(value, uiSchema, nestedObject); | ||
} else if (key === 'multiwoven_secret' && value === true) { | ||
let current = uiSchema; | ||
path.forEach((schemaPath, index) => { | ||
if (index === path.length - 1) { | ||
current[schemaPath] = { 'ui:widget': 'password' }; | ||
} else { | ||
current[schemaPath] = current[schemaPath] || {}; | ||
} | ||
current = current[schemaPath]; | ||
}); | ||
} | ||
}); | ||
|
||
return uiSchema; | ||
}; |
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
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