Skip to content

Commit 6051bdb

Browse files
authored
Merge pull request #187 from workfloworchestrator/2175-floris-fixes
2175 Fixes for bugs found in QA
2 parents eb0cc23 + b5cf834 commit 6051bdb

File tree

11 files changed

+68
-27
lines changed

11 files changed

+68
-27
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'pydantic-forms': patch
3+
---
4+
5+
Fixes resetting formdata with forms that have no properties

frontend/packages/pydantic-forms/src/components/defaultComponentMatchers.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
*
44
* We will search for the first field that returns a positive match
55
*/
6+
import _ from 'lodash';
7+
68
import {
79
ArrayField,
810
CheckboxField,
@@ -101,6 +103,7 @@ const defaultComponentMatchers: PydanticComponentMatcher[] = [
101103
// We are looking for a single value from a set list of options. With less than 4 options, use radio buttons.
102104
return (
103105
field.type === PydanticFormFieldType.STRING &&
106+
_.isArray(field.options) &&
104107
field.options?.length > 0 &&
105108
field.options?.length <= 3
106109
);
@@ -116,6 +119,7 @@ const defaultComponentMatchers: PydanticComponentMatcher[] = [
116119
// We are looking for a single value from a set list of options. With more than 3 options, use a dropdown.
117120
return (
118121
field.type === PydanticFormFieldType.STRING &&
122+
_.isArray(field.options) &&
119123
field.options?.length >= 4
120124
);
121125
},
@@ -139,6 +143,7 @@ const defaultComponentMatchers: PydanticComponentMatcher[] = [
139143
matcher(field) {
140144
return (
141145
field.type === PydanticFormFieldType.ARRAY &&
146+
_.isArray(field.options) &&
142147
field.options?.length > 0 &&
143148
field.options?.length <= 5
144149
);
@@ -153,6 +158,7 @@ const defaultComponentMatchers: PydanticComponentMatcher[] = [
153158
},
154159
matcher(field) {
155160
return (
161+
_.isArray(field.options) &&
156162
field.options?.length > 0 &&
157163
field.type === PydanticFormFieldType.ARRAY
158164
);

frontend/packages/pydantic-forms/src/components/fields/IntegerField.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import React from 'react';
22

3+
import _ from 'lodash';
4+
35
import type { PydanticFormControlledElementProps } from '@/types';
6+
import { getFormFieldIdWithPath } from '@/utils';
47

58
export const IntegerField = ({
69
value,
@@ -9,6 +12,14 @@ export const IntegerField = ({
912
disabled,
1013
pydanticFormField,
1114
}: PydanticFormControlledElementProps) => {
15+
// If the field is part of an array the value is passed in as an object with the field name as key
16+
// this is imposed by react-hook-form. We try to detect this and extract the actual value
17+
const fieldName = getFormFieldIdWithPath(pydanticFormField.id);
18+
const fieldValue =
19+
_.isObject(value) && _.has(value, fieldName)
20+
? _.get(value, fieldName)
21+
: value;
22+
1223
return (
1324
<input
1425
data-testid={pydanticFormField.id}
@@ -18,7 +29,7 @@ export const IntegerField = ({
1829
onChange(value);
1930
}}
2031
disabled={disabled}
21-
value={value}
32+
value={fieldValue}
2233
type="number"
2334
style={{
2435
padding: '8px',

frontend/packages/pydantic-forms/src/components/fields/MultiCheckboxField.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const MultiCheckboxField = ({
2828

2929
return (
3030
<div>
31-
{options.map((option: PydanticFormFieldOption) => {
31+
{options?.map((option: PydanticFormFieldOption) => {
3232
// Extract the unique ID for this option
3333
const optionId = `${id}-${option.value}`;
3434

frontend/packages/pydantic-forms/src/components/fields/MultiSelectField.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export const MultiSelectField = ({
3838
}}
3939
multiple
4040
>
41-
{pydanticFormField.options.map(
41+
{pydanticFormField.options?.map(
4242
(option: PydanticFormFieldOption) => (
4343
<option key={option.value} value={option.value}>
4444
{option.label}

frontend/packages/pydantic-forms/src/components/fields/RadioField.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const RadioField = ({
1616

1717
return (
1818
<div>
19-
{options.map((option, key) => (
19+
{options?.map((option, key) => (
2020
<div key={key}>
2121
<input
2222
data-testid={`${id}-${option.value}`}

frontend/packages/pydantic-forms/src/components/fields/TextField.tsx

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,40 @@
55
*/
66
import React from 'react';
77

8+
import _ from 'lodash';
9+
810
import { PydanticFormControlledElementProps } from '@/types';
11+
import { getFormFieldIdWithPath } from '@/utils';
912

1013
export const TextField = ({
1114
value,
1215
onChange,
1316
onBlur,
1417
disabled,
1518
pydanticFormField,
16-
}: PydanticFormControlledElementProps) => (
17-
<input
18-
data-testid={pydanticFormField.id}
19-
onBlur={onBlur}
20-
onChange={(t) => {
21-
onChange(t.currentTarget.value);
22-
}}
23-
disabled={disabled}
24-
value={value}
25-
type="text"
26-
style={{
27-
padding: '8px',
28-
margin: '8px 0',
29-
}}
30-
/>
31-
);
19+
}: PydanticFormControlledElementProps) => {
20+
// If the field is part of an array the value is passed in as an object with the field name as key
21+
// this is imposed by react-hook-form. We try to detect this and extract the actual value
22+
const fieldName = getFormFieldIdWithPath(pydanticFormField.id);
23+
const fieldValue =
24+
_.isObject(value) && _.has(value, fieldName)
25+
? _.get(value, fieldName)
26+
: value;
27+
28+
return (
29+
<input
30+
data-testid={pydanticFormField.id}
31+
onBlur={onBlur}
32+
onChange={(t) => {
33+
onChange(t.currentTarget.value);
34+
}}
35+
disabled={disabled}
36+
value={fieldValue}
37+
type="text"
38+
style={{
39+
padding: '8px',
40+
margin: '8px 0',
41+
}}
42+
/>
43+
);
44+
};

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ export function useApiProvider(
5555
}
5656
if (
5757
request.status &&
58-
(request.status === 200 || request.status === 201)
58+
request.status >= 200 &&
59+
request.status < 300
5960
) {
6061
return {
6162
type: PydanticFormApiResponseType.SUCCESS,

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,6 @@ const getPydanticFormField = (
7474
)
7575
: '';
7676

77-
const addConstValue =
78-
typeof flatSchema.const === 'undefined' ? false : true;
79-
8077
const arrayItem = flatSchema.items
8178
? getPydanticFormField(
8279
flatSchema.items,
@@ -87,6 +84,14 @@ const getPydanticFormField = (
8784
)
8885
: undefined;
8986

87+
const addConstValue =
88+
typeof flatSchema.const === 'undefined' ? false : true;
89+
90+
// Don't add options to array items if they have an arrayItem where they live
91+
const addOptions = !(
92+
flatSchema.type === PydanticFormFieldType.ARRAY && arrayItem
93+
);
94+
9095
//TODO: I think object properties should never be required only their properties are or aren't. Should we fix this in the backend?
9196
const required =
9297
flatSchema.type === PydanticFormFieldType.OBJECT
@@ -100,7 +105,7 @@ const getPydanticFormField = (
100105
arrayItem,
101106
format: flatSchema.format || PydanticFormFieldFormat.DEFAULT,
102107
type: flatSchema.type || PydanticFormFieldType.STRING,
103-
options: options,
108+
...toOptionalObjectProperty({ options }, addOptions),
104109
default: flatSchema.default,
105110
attributes: attributes,
106111
schema: propertySchema,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export interface PydanticFormField {
4646
description?: string;
4747
type: PydanticFormFieldType;
4848
format: PydanticFormFieldFormat;
49-
options: PydanticFormFieldOption[];
49+
options?: PydanticFormFieldOption[];
5050
disabledOptions?: string[];
5151
default?: PydanticFormFieldValue;
5252
required: boolean;

0 commit comments

Comments
 (0)