Skip to content

Commit 2370d15

Browse files
author
Ruben van Leeuwen
committed
2102: Disables descendants of disabled fields
1 parent bb71784 commit 2370d15

File tree

2 files changed

+52
-32
lines changed

2 files changed

+52
-32
lines changed

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

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import { useFieldArray } from 'react-hook-form';
44
import { usePydanticFormContext } from '@/core';
55
import { fieldToComponentMatcher } from '@/core/helper';
66
import { PydanticFormElementProps } from '@/types';
7-
import { itemizeArrayItem } from '@/utils';
7+
import { disableField, itemizeArrayItem } from '@/utils';
88

99
import { RenderFields } from '../render';
1010

1111
export const ArrayField = ({ pydanticFormField }: PydanticFormElementProps) => {
1212
const { rhf, config } = usePydanticFormContext();
1313

14+
const disabled = pydanticFormField.attributes?.disabled || false;
1415
const { control } = rhf;
1516
const { id: arrayName, arrayItem } = pydanticFormField;
1617
const { fields, append, remove } = useFieldArray({
@@ -28,7 +29,12 @@ export const ArrayField = ({ pydanticFormField }: PydanticFormElementProps) => {
2829
);
2930

3031
const renderField = (field: Record<'id', string>, index: number) => {
31-
const arrayItemField = itemizeArrayItem(index, arrayItem, arrayName);
32+
const itemizedField = itemizeArrayItem(index, arrayItem, arrayName);
33+
// We have decided - for now - on the convention that all descendants of disabled fields will be disabled as well
34+
// so we will not displaying any interactive elements inside a disabled element
35+
const arrayItemField = disabled
36+
? disableField(itemizedField)
37+
: itemizedField;
3238

3339
return (
3440
<div
@@ -49,15 +55,16 @@ export const ArrayField = ({ pydanticFormField }: PydanticFormElementProps) => {
4955
]}
5056
extraTriggerFields={[arrayName]}
5157
/>
52-
{(!minItems || (minItems && fields.length > minItems)) && (
53-
<span
54-
onClick={() => {
55-
remove(index);
56-
}}
57-
>
58-
-
59-
</span>
60-
)}
58+
{(!minItems || (minItems && fields.length > minItems)) &&
59+
!disabled && (
60+
<span
61+
onClick={() => {
62+
remove(index);
63+
}}
64+
>
65+
-
66+
</span>
67+
)}
6168
</div>
6269
);
6370
};
@@ -66,7 +73,7 @@ export const ArrayField = ({ pydanticFormField }: PydanticFormElementProps) => {
6673
<div
6774
data-testid={arrayName}
6875
style={{
69-
border: 'thin solid green',
76+
border: '1px solid #ccc',
7077
padding: '1rem',
7178
marginTop: '16px',
7279
display: 'flex',
@@ -75,26 +82,27 @@ export const ArrayField = ({ pydanticFormField }: PydanticFormElementProps) => {
7582
}}
7683
>
7784
{fields.map(renderField)}
78-
{(!maxItems || (maxItems && fields.length < maxItems)) && (
79-
<div
80-
onClick={() => {
81-
append({
82-
[arrayName]: arrayItem.default,
83-
});
84-
}}
85-
style={{
86-
cursor: 'pointer',
87-
fontSize: '32px',
88-
display: 'flex',
89-
justifyContent: 'end',
90-
marginTop: '8px',
91-
marginBottom: '8px',
92-
padding: '16px',
93-
}}
94-
>
95-
+
96-
</div>
97-
)}
85+
{(!maxItems || (maxItems && fields.length < maxItems)) &&
86+
!disabled && (
87+
<div
88+
onClick={() => {
89+
append({
90+
[arrayName]: arrayItem.default,
91+
});
92+
}}
93+
style={{
94+
cursor: 'pointer',
95+
fontSize: '32px',
96+
display: 'flex',
97+
justifyContent: 'end',
98+
marginTop: '8px',
99+
marginBottom: '8px',
100+
padding: '16px',
101+
}}
102+
>
103+
+
104+
</div>
105+
)}
98106
</div>
99107
);
100108
};

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,30 @@ import React from 'react';
33
import { usePydanticFormContext } from '@/core';
44
import { getPydanticFormComponents } from '@/core/helper';
55
import { PydanticFormElementProps } from '@/types';
6+
import { disableField } from '@/utils';
67

78
import { RenderFields } from '../render';
89

910
export const ObjectField = ({
1011
pydanticFormField,
1112
}: PydanticFormElementProps) => {
1213
const { config } = usePydanticFormContext();
14+
const disabled = pydanticFormField.attributes?.disabled || false;
1315
const components = getPydanticFormComponents(
1416
pydanticFormField.properties || {},
1517
config?.componentMatcherExtender,
1618
);
1719

20+
// We have decided - for now - on the convention that all descendants of disabled fields will be disabled as well
21+
// so we will not displaying any interactive elements inside a disabled element
22+
if (disabled) {
23+
components.forEach((component) => {
24+
component.pydanticFormField = disableField(
25+
component.pydanticFormField,
26+
);
27+
});
28+
}
29+
1830
return (
1931
<div
2032
data-testid={pydanticFormField.id}

0 commit comments

Comments
 (0)