Skip to content

Commit a1b1fe8

Browse files
author
Ruben van Leeuwen
committed
1714: Fixes getLabelById function and error components
1 parent 7489a1f commit a1b1fe8

File tree

4 files changed

+61
-32
lines changed

4 files changed

+61
-32
lines changed

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
import React from 'react';
77

88
import { usePydanticFormContext } from '@/core';
9+
import { getFieldLabelById } from '@/core/helper';
910

1011
export default function RenderFormErrors() {
11-
const { errorDetails } = usePydanticFormContext();
12+
const { errorDetails, pydanticFormSchema } = usePydanticFormContext();
1213

1314
if (!errorDetails) {
1415
return <></>;
@@ -20,10 +21,6 @@ export default function RenderFormErrors() {
2021
.shift();
2122
const otherErrors = errors.filter((err) => !err.loc.includes('__root__'));
2223

23-
const getFieldLabel = (fieldId: string) => {
24-
return fieldId;
25-
};
26-
2724
return (
2825
<div>
2926
{!!rootError && <div>{rootError.msg}</div>}
@@ -32,8 +29,15 @@ export default function RenderFormErrors() {
3229
<ul>
3330
{otherErrors.map((error) => (
3431
<li key={JSON.stringify(error)}>
35-
{error.loc.map(getFieldLabel).join(', ')}:{' '}
36-
{error.msg}
32+
{error.loc
33+
.map((value) =>
34+
getFieldLabelById(
35+
value,
36+
pydanticFormSchema,
37+
),
38+
)
39+
.join(', ')}
40+
: {error.msg}
3741
</li>
3842
))}
3943
</ul>

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { usePydanticFormContext } from '@/core';
99
import { getFieldLabelById } from '@/core/helper';
1010

1111
export default function RenderReactHookFormErrors() {
12-
const { rhf } = usePydanticFormContext();
12+
const { rhf, pydanticFormSchema } = usePydanticFormContext();
1313
const [showDetails, setShowDetails] = useState(false);
1414

1515
const toggleDetails = useCallback(() => {
@@ -46,17 +46,21 @@ export default function RenderReactHookFormErrors() {
4646
<ul className="error-list mb-2">
4747
{Object.keys(rhf.formState.errors).map(
4848
(fieldKey) => {
49-
const field =
49+
const fieldError =
5050
rhf.formState?.errors[fieldKey];
5151

52-
const fieldName =
53-
'TO: Get field name by id function';
52+
const fieldName = getFieldLabelById(
53+
fieldKey,
54+
pydanticFormSchema,
55+
);
56+
5457
return (
5558
<li key={fieldKey}>
5659
<strong className="mr-2">
5760
{fieldName}:{' '}
5861
</strong>
59-
{(field?.message as string) ?? ''}
62+
{(fieldError?.message as string) ??
63+
''}
6064
</li>
6165
);
6266
},

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { ControllerRenderProps, FieldValues, useForm } from 'react-hook-form';
77

88
import defaultComponentMatchers from '@/components/defaultComponentMatchers';
99
import {
10+
Properties,
1011
PydanticComponentMatcher,
1112
PydanticFormApiResponse,
1213
PydanticFormComponents,
@@ -166,9 +167,29 @@ export const optionsToOption = (
166167

167168
export const getFieldLabelById = (
168169
fieldId: string,
169-
formScheam: PydanticFormSchema,
170+
formSchema?: PydanticFormSchema,
170171
) => {
171-
return fieldId;
172+
const fieldMap = getFlatFieldMap(formSchema?.properties ?? {});
173+
console.log('fieldMap', fieldMap);
174+
return fieldMap.has(fieldId) ? fieldMap.get(fieldId)?.title : fieldId;
175+
};
176+
177+
type FieldMap = Map<string, PydanticFormField>;
178+
export const getFlatFieldMap = (
179+
properties: Properties,
180+
fieldMap: FieldMap = new Map(),
181+
) => {
182+
console.log('props', properties);
183+
if (properties) {
184+
Object.entries(properties ?? {}).forEach(([id, field]) => {
185+
if (field.properties) {
186+
getFlatFieldMap(field.properties, fieldMap);
187+
}
188+
fieldMap.set(id, field);
189+
});
190+
}
191+
192+
return fieldMap;
172193
};
173194

174195
/**

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,7 @@ export interface PydanticFormField {
107107
password: boolean;
108108
};
109109

110-
properties?: {
111-
[propId: string]: PydanticFormField;
112-
};
110+
properties?: Properties;
113111
}
114112

115113
export interface PydanticFormFieldSection {
@@ -384,23 +382,17 @@ export interface PydanticFormBaseSchema {
384382

385383
export interface PydanticFormSchema extends PydanticFormBaseSchema {
386384
type: PydanticFormFieldType.OBJECT;
387-
properties: {
388-
[propId: string]: PydanticFormField;
389-
};
385+
properties: Properties;
390386
}
391387

392388
export interface PydanticFormSchemaRawJson extends PydanticFormBaseSchema {
393389
type: PydanticFormFieldType.OBJECT;
394-
properties: {
395-
[propId: string]: PydanticFormPropertySchemaRawJson;
396-
};
390+
properties: RawJsonProperties;
397391
}
398392

399393
export interface PydanticFormSchemaParsed extends PydanticFormBaseSchema {
400394
type: 'object';
401-
properties: {
402-
[propId: string]: PydanticFormPropertySchemaParsed;
403-
};
395+
properties: ParsedProperties;
404396
}
405397

406398
export interface PydanticFormPropertySchemaParsed
@@ -427,9 +419,7 @@ export interface PydanticFormPropertySchemaParsed
427419
password: boolean;
428420
};
429421

430-
properties?: {
431-
[propId: string]: PydanticFormPropertySchemaRawJson;
432-
};
422+
properties?: RawJsonProperties;
433423
}
434424

435425
export interface PydanticFormFieldAnyOfDefParsed
@@ -472,11 +462,21 @@ export interface PydanticFormPropertySchemaRawJson
472462
password: boolean;
473463
};
474464

475-
properties?: {
476-
[propId: string]: PydanticFormPropertySchemaRawJson;
477-
};
465+
properties?: RawJsonProperties;
478466
}
479467

468+
export type RawJsonProperties = {
469+
[propId: string]: PydanticFormPropertySchemaRawJson;
470+
};
471+
472+
export type Properties = {
473+
[propId: string]: PydanticFormField;
474+
};
475+
476+
export type ParsedProperties = {
477+
[propId: string]: PydanticFormPropertySchemaParsed;
478+
};
479+
480480
export interface PydanticFormFieldAnyOfDef {
481481
items?: JsonSchemaRef;
482482
type: 'null' | 'array';

0 commit comments

Comments
 (0)