Skip to content

Commit c79ba32

Browse files
author
Ruben van Leeuwen
committed
2175: Make options optional and dont add them to array fields with arrayItems
1 parent cc63c09 commit c79ba32

File tree

6 files changed

+19
-8
lines changed

6 files changed

+19
-8
lines changed

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/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/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)