Skip to content

Commit 58c96a1

Browse files
authored
Allow to use global form config in field functions (#79)
* Allow to use global form config in field functions * Prepare components release 4.2.0 * Fix lint errors
1 parent 4d0ac09 commit 58c96a1

File tree

7 files changed

+289
-117
lines changed

7 files changed

+289
-117
lines changed

Diff for: packages/components/CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Change Log
22

3+
## 4.2.0 (2025-02-20)
4+
5+
### Features / Enhancements
6+
7+
- Allow to use global form config in field functions (#79)
8+
39
## 4.1.0 (2025-01-17)
410

511
### Features / Enhancements

Diff for: packages/components/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,5 @@
8989
"typecheck": "tsc --emitDeclarationOnly false --noEmit"
9090
},
9191
"types": "dist/index.d.ts",
92-
"version": "4.1.0"
92+
"version": "4.2.0"
9393
}

Diff for: packages/components/src/components/Form/Form.test.tsx

+18-1
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,9 @@ describe('Form', () => {
753753
getComponent<{
754754
field: string;
755755
field2: string;
756+
group: {
757+
field: string;
758+
};
756759
}>({
757760
getForm: (builder) =>
758761
builder
@@ -764,14 +767,27 @@ describe('Form', () => {
764767
path: 'field2',
765768
defaultValue: '',
766769
showIf: (config) => config.field !== '',
767-
}),
770+
})
771+
.addGroup(
772+
{
773+
path: 'group',
774+
label: '',
775+
},
776+
(builder) =>
777+
builder.addInput({
778+
path: 'field',
779+
defaultValue: '',
780+
showIf: (config, formConfig) => formConfig.field !== '',
781+
})
782+
),
768783
})
769784
);
770785

771786
/**
772787
* Should be hidden
773788
*/
774789
expect(selectors.fieldInput(true, 'field2')).not.toBeInTheDocument();
790+
expect(selectors.fieldInput(true, 'group.field')).not.toBeInTheDocument();
775791

776792
/**
777793
* Update dependent field
@@ -782,6 +798,7 @@ describe('Form', () => {
782798
* Should be visible now
783799
*/
784800
expect(selectors.fieldInput(false, 'field2')).toBeInTheDocument();
801+
expect(selectors.fieldInput(false, 'group.field')).toBeInTheDocument();
785802
});
786803

787804
it('Should disable field', () => {

Diff for: packages/components/src/components/Form/Form.tsx

+7-7
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ interface Props<TValue extends object> {
5151
/**
5252
* Fields
5353
*/
54-
fields: Array<RenderFormField<TValue>>;
54+
fields: Array<RenderFormField<TValue, TValue>>;
5555

5656
/**
5757
* Variant
@@ -81,14 +81,14 @@ const FormControl = ({ className, children }: { className: string; children: Rea
8181
/**
8282
* Form
8383
*/
84-
export const Form = <TValue extends object>({
84+
export const Form = <TFormValue extends object>({
8585
name,
8686
expanded = {},
8787
onToggleExpanded = () => null,
8888
fields,
8989
variant = 'default',
9090
readonly = false,
91-
}: Props<TValue>) => {
91+
}: Props<TFormValue>) => {
9292
/**
9393
* Styles
9494
*/
@@ -98,7 +98,7 @@ export const Form = <TValue extends object>({
9898
* Group Fields In Rows
9999
* @param fields
100100
*/
101-
const groupFieldsInRows = <TObject extends object>(fields: RenderFormField<TObject>[]) => {
101+
const groupFieldsInRows = <TGroupValue extends object>(fields: RenderFormField<TFormValue, TGroupValue>[]) => {
102102
const rowsMap = fields.reduce(
103103
(acc, field, index) => {
104104
const rowKey = 'view' in field && field.view?.row ? `row_${field.view.row}` : index.toString();
@@ -111,12 +111,12 @@ export const Form = <TValue extends object>({
111111
/**
112112
* Add field to row
113113
*/
114-
rowValue.push(field as RenderFormField<TObject>);
114+
rowValue.push(field as RenderFormField<TFormValue, TGroupValue>);
115115
acc.set(rowKey, rowValue);
116116

117117
return acc;
118118
},
119-
new Map() as Map<string, RenderFormField<TObject>[]>
119+
new Map() as Map<string, RenderFormField<TFormValue, TGroupValue>[]>
120120
);
121121

122122
return Array.from(rowsMap.values());
@@ -126,7 +126,7 @@ export const Form = <TValue extends object>({
126126
* Render Field
127127
* @param field
128128
*/
129-
const renderField = <TObject extends object>(field: RenderFormField<TObject>) => {
129+
const renderField = <TGroupValue extends object>(field: RenderFormField<TFormValue, TGroupValue>) => {
130130
if (!field.showIf()) {
131131
return null;
132132
}

0 commit comments

Comments
 (0)