Skip to content

Commit 262af2d

Browse files
author
Ruben van Leeuwen
committed
1714: Render object fields WIP
1 parent a1b1fe8 commit 262af2d

File tree

10 files changed

+35
-83
lines changed

10 files changed

+35
-83
lines changed
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
import React from 'react';
22

3-
// import { usePydanticFormContext } from '@/core';
3+
import { usePydanticFormContext } from '@/core';
4+
import { componentMatcher } from '@/core';
45
import { PydanticFormElementProps } from '@/types';
56

6-
// import { RenderFields } from '../render';
7+
import { RenderFields } from '../render';
78

89
export const ObjectField = ({
910
pydanticFormField,
1011
}: PydanticFormElementProps) => {
12+
const { config } = usePydanticFormContext();
13+
14+
const components = componentMatcher(
15+
pydanticFormField.properties || {},
16+
config?.componentMatcher,
17+
);
18+
console.log('components: ' + pydanticFormField.id, components);
1119
return (
1220
<div>
1321
<h1>{pydanticFormField.title}</h1>
22+
<RenderFields components={components} />
1423
</div>
1524
);
1625
};

frontend/packages/pydantic-forms/src/components/render/FormRenderer.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,14 @@ import type { FormRenderer as Renderer } from '@/types';
66

77
export const FormRenderer: Renderer = ({ pydanticFormComponents }) => {
88
const formSections = getFieldBySection(pydanticFormComponents);
9-
9+
console.log(formSections);
1010
const sections = formSections.map((section) => (
1111
<RenderSections
1212
section={section}
1313
key={section.id}
1414
components={pydanticFormComponents}
1515
>
16-
{({ components }) => (
17-
<div>
18-
<RenderFields components={components} />
19-
</div>
20-
)}
16+
{({ components }) => <RenderFields components={components} />}
2117
</RenderSections>
2218
));
2319

frontend/packages/pydantic-forms/src/components/render/RenderFields.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export function RenderFields({ components }: RenderFieldsProps) {
1919
const field: PydanticFormField = component.pydanticFormField;
2020

2121
if (!Element) {
22-
return <></>;
22+
return undefined;
2323
}
2424

2525
if (isControlledElement) {

frontend/packages/pydantic-forms/src/components/render/RenderForm.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
*/
88
import React from 'react';
99

10+
import { componentMatcher } from '@/components/componentMatcher';
1011
import Footer from '@/components/form/Footer';
1112
import RenderFormErrors from '@/components/render/RenderFormErrors';
12-
import { componentMatcher } from '@/core/componentMatcher';
1313
import { PydanticFormComponents, PydanticFormContextProps } from '@/types';
1414

1515
import { FormRenderer } from './FormRenderer';
@@ -57,7 +57,7 @@ const RenderForm = (contextProps: PydanticFormContextProps) => {
5757
// Map schema to get fields
5858

5959
const pydanticFormComponents: PydanticFormComponents = componentMatcher(
60-
pydanticFormSchema,
60+
pydanticFormSchema.properties,
6161
customComponentMatcher,
6262
);
6363

frontend/packages/pydantic-forms/src/core/componentMatcher.tsx

Lines changed: 0 additions & 36 deletions
This file was deleted.

frontend/packages/pydantic-forms/src/core/helper.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { ControllerRenderProps, FieldValues, useForm } from 'react-hook-form';
88
import defaultComponentMatchers from '@/components/defaultComponentMatchers';
99
import {
1010
Properties,
11-
PydanticComponentMatcher,
1211
PydanticFormApiResponse,
1312
PydanticFormComponents,
1413
PydanticFormField,
@@ -179,7 +178,6 @@ export const getFlatFieldMap = (
179178
properties: Properties,
180179
fieldMap: FieldMap = new Map(),
181180
) => {
182-
console.log('props', properties);
183181
if (properties) {
184182
Object.entries(properties ?? {}).forEach(([id, field]) => {
185183
if (field.properties) {

frontend/packages/pydantic-forms/src/core/hooks/useGetZodValidator.tsx

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { useForm } from 'react-hook-form';
1212

1313
import { z } from 'zod';
1414

15-
import { getMatcher } from '@/core/helper';
15+
import { getFlatFieldMap, getMatcher } from '@/core/helper';
1616
import {
1717
CustomValidationRule,
1818
PydanticFormField,
@@ -22,20 +22,6 @@ import {
2222

2323
type PropertyMap = Map<string, PydanticFormField>;
2424

25-
const getFlatPropertyMap = (schema: PydanticFormSchema): PropertyMap => {
26-
const propertyMap: PropertyMap = new Map();
27-
28-
if (schema) {
29-
Object.entries(schema.properties ?? {}).forEach(
30-
([id, pydanticFormPropertySchema]) => {
31-
propertyMap.set(id, pydanticFormPropertySchema);
32-
},
33-
);
34-
}
35-
36-
return propertyMap;
37-
};
38-
3925
const getClientSideValidationRule = (
4026
field: PydanticFormField,
4127
rhf?: ReturnType<typeof useForm>,
@@ -69,10 +55,10 @@ export const useGetZodValidator = (
6955
return z.object({});
7056
}
7157
// Get all fields ids including the nested ones to generate the correct validation schema
72-
const flatPropertyMap = getFlatPropertyMap(pydanticFormSchema);
58+
const flatFieldMap = getFlatFieldMap(pydanticFormSchema.properties);
7359

7460
return z.object(
75-
[...flatPropertyMap].reduce(
61+
[...flatFieldMap].reduce(
7662
(validationObject, [propertyId, pydanticFormField]) => {
7763
const fieldRules =
7864
customValidationRule?.(pydanticFormField, rhf) ??

frontend/packages/pydantic-forms/src/core/hooks/usePydanticFormParser.tsx

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { useCallback, useMemo, useState } from 'react';
2-
3-
import $RefParser from '@apidevtools/json-schema-ref-parser';
1+
import { useMemo } from 'react';
42

53
import {
64
getFieldAllOfAnyOfEntry,
@@ -17,7 +15,9 @@ import {
1715
*
1816
*/
1917
import type {
18+
Properties,
2019
PydanticFormField,
20+
PydanticFormPropertySchemaParsed,
2121
PydanticFormSchema,
2222
PydanticFormSchemaParsed,
2323
PydanticFormSchemaRawJson,
@@ -38,23 +38,16 @@ const translateLabel = (
3838
};
3939

4040
const parseProperties = (
41-
parsedSchema: PydanticFormSchemaParsed,
41+
parsedSchema: PydanticFormSchemaParsed | PydanticFormPropertySchemaParsed,
4242
formLabels?: Record<string, string>,
4343
fieldDetailProvider?: PydanticFormsContextConfig['fieldDetailProvider'],
4444
prefix: string = '',
4545
) => {
46-
if (!parsedSchema) return {};
47-
48-
const schemaProperties = parsedSchema.properties;
49-
50-
if (!schemaProperties) return {};
46+
if (!parsedSchema || !parsedSchema.properties) return {};
5147

52-
const properties = Object.entries(schemaProperties);
53-
const parsedProperties = properties.reduce(
54-
(
55-
propertiesObject: PydanticFormSchema['properties'],
56-
[propertyId, propertySchema],
57-
) => {
48+
const p = Object.entries(parsedSchema.properties);
49+
const parsedProperties = p.reduce(
50+
(propertiesObject: Properties, [propertyId, propertySchema]) => {
5851
const options = getFieldOptions(propertySchema);
5952
const fieldOptionsEntry = getFieldAllOfAnyOfEntry(propertySchema);
6053
const id = `${prefix && prefix + '.'}${propertyId}`;
@@ -85,6 +78,12 @@ const parseProperties = (
8578
schemaProperty: propertySchema,
8679
validations: getFieldValidation(propertySchema),
8780
columns: 6, // TODO: Is this still relevant?
81+
properties: parseProperties(
82+
propertySchema || {},
83+
formLabels,
84+
fieldDetailProvider,
85+
id,
86+
),
8887
...fieldDetailProvider?.[propertyId],
8988
};
9089

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export * from './PydanticFormContextProvider';
22
export * from './hooks';
3-
export * from './componentMatcher';
3+
export * from '../components/componentMatcher';

frontend/packages/pydantic-forms/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ export interface PydanticFormPropertySchemaParsed
419419
password: boolean;
420420
};
421421

422-
properties?: RawJsonProperties;
422+
properties?: ParsedProperties;
423423
}
424424

425425
export interface PydanticFormFieldAnyOfDefParsed

0 commit comments

Comments
 (0)