Skip to content

Commit 309342b

Browse files
Merge pull request #728 from gadget-inc/mill/fixCustomParamsOnModels
Fix custom action params in AutoForm
2 parents d42df6f + 86af8a0 commit 309342b

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

packages/react/spec/auto/polaris/PolarisAutoForm.stories.jsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,26 @@ export const Primary = {
5151
},
5252
};
5353

54+
export const CreateWithCustomParams = {
55+
args: {
56+
action: api.widget.createWithCustomParams,
57+
},
58+
};
59+
5460
export const UpdateRecord = {
5561
args: {
5662
action: api.widget.update,
5763
findBy: "999",
5864
},
5965
};
6066

67+
export const UpdateRecordWithCustomParams = {
68+
args: {
69+
action: api.widget.updateWithCustomParams,
70+
findBy: "999",
71+
},
72+
};
73+
6174
export const UpsertRecordWithFindBy = {
6275
args: {
6376
action: api.widget.upsert,
Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
1+
import { type FieldMetadata } from "../../metadata.js";
12
import { useAutoFormMetadata } from "../AutoFormContext.js";
23

34
export const useFieldMetadata = (fieldApiIdentifier: string) => {
45
const { model, fields } = useAutoFormMetadata();
5-
const metaDataPath =
6-
model && model.apiIdentifier
7-
? model.apiIdentifier + "." + fieldApiIdentifier // Model action
8-
: fieldApiIdentifier; // Global action
96

10-
const targetFieldMetadata = fields.find((field) => field.path === metaDataPath);
7+
const isModelAction = model && model.apiIdentifier;
8+
const metaDataPath = isModelAction
9+
? model.apiIdentifier + "." + fieldApiIdentifier // Model action
10+
: fieldApiIdentifier; // Global action
11+
12+
const targetFieldMetadata = fields.find(
13+
(field) => field.path === metaDataPath || isFieldCustomParamOnModelAction(fieldApiIdentifier, field)
14+
);
1115

1216
if (!targetFieldMetadata) {
1317
throw new Error(`Field "${fieldApiIdentifier}" not found in metadata`);
1418
}
1519

16-
return {
17-
path: metaDataPath,
18-
metadata: targetFieldMetadata.metadata,
19-
};
20+
return targetFieldMetadata;
2021
};
22+
23+
const isFieldCustomParamOnModelAction = (
24+
fieldApiIdentifier: string,
25+
fieldCandidate: {
26+
path: string;
27+
metadata: FieldMetadata;
28+
}
29+
) => fieldCandidate.metadata.__typename !== "GadgetModelField" && fieldCandidate.path === fieldApiIdentifier;

packages/react/src/use-table/helpers.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,12 @@ const roleAssignmentsSelection = {
347347
name: true,
348348
};
349349

350-
const getNonRelationshipSelectionValue = (field: FieldMetadata) => {
350+
const getNonRelationshipSelectionValue = (field: FieldMetadata, onlyAllowModelFields = false) => {
351+
if (field.__typename !== "GadgetModelField" && onlyAllowModelFields) {
352+
// Only model fields are selectable
353+
return false;
354+
}
355+
351356
switch (field.fieldType) {
352357
case GadgetFieldType.RichText:
353358
return richTextSelection;
@@ -524,7 +529,7 @@ export const pathListToSelection = (modelIdentifier: string, pathList: string[],
524529
}
525530
} else {
526531
// Non relationship field
527-
selection[field.apiIdentifier] = getNonRelationshipSelectionValue(field);
532+
selection[field.apiIdentifier] = getNonRelationshipSelectionValue(field, true);
528533
}
529534
}
530535

0 commit comments

Comments
 (0)