@@ -13,10 +13,7 @@ import { useModelManager } from "./useModelManager.js";
13
13
export const optionRecordsToLoadCount = 25 ;
14
14
export const selectedRecordsToLoadCount = 25 ;
15
15
16
- export const useRelatedModelOptions = ( props : {
17
- field : string ; // Field API identifier
18
- optionLabel ?: OptionLabel ; // The label to display for each related model record
19
- } ) => {
16
+ export const useRelatedModelRecords = ( props : { field : string } ) => {
20
17
const { field } = props ;
21
18
const { metadata } = useFieldMetadata ( field ) ;
22
19
const { findBy, model } = useAutoFormMetadata ( ) ;
@@ -30,11 +27,6 @@ export const useRelatedModelOptions = (props: {
30
27
const relatedModelInverseFieldApiId =
31
28
"inverseField" in relationshipFieldConfig ? relationshipFieldConfig . inverseField ?. apiIdentifier : undefined ;
32
29
33
- const optionLabel = assert (
34
- props . optionLabel ?? relationshipFieldConfig . relatedModel ?. defaultDisplayField . apiIdentifier ,
35
- "Option label is required for relationships"
36
- ) ;
37
-
38
30
const { selected } = isBelongsToField
39
31
? // eslint-disable-next-line
40
32
useLinkedChildModelRelatedModelRecords ( {
@@ -54,8 +46,30 @@ export const useRelatedModelOptions = (props: {
54
46
55
47
const relatedModelRecords = useAllRelatedModelRecords ( {
56
48
relatedModel : { apiIdentifier : relatedModelApiIdentifier ! , namespace : relatedModelNamespace } ,
57
- optionLabel ,
49
+ filter : isBelongsToField ? undefined : { [ relatedModelInverseFieldApiId + "Id" ] : { isSet : false } } ,
58
50
} ) ;
51
+
52
+ return {
53
+ selected,
54
+ relatedModelRecords,
55
+ } ;
56
+ } ;
57
+
58
+ export const useRelatedModelOptions = ( props : {
59
+ field : string ; // Field API identifier
60
+ optionLabel ?: OptionLabel ; // The label to display for each related model record
61
+ } ) => {
62
+ const { field } = props ;
63
+ const { metadata } = useFieldMetadata ( field ) ;
64
+
65
+ const relationshipFieldConfig = metadata . configuration as RelationshipFieldConfig ;
66
+
67
+ const optionLabel = assert (
68
+ props . optionLabel ?? relationshipFieldConfig . relatedModel ?. defaultDisplayField . apiIdentifier ,
69
+ "Option label is required for relationships"
70
+ ) ;
71
+ const { selected, relatedModelRecords } = useRelatedModelRecords ( props ) ;
72
+
59
73
const { relatedModel, pagination, search } = relatedModelRecords ;
60
74
61
75
const getOptions = ( ) => {
@@ -120,7 +134,7 @@ export const getRecordsAsOptions = (records: Record<string, any>[], optionLabel:
120
134
*
121
135
* The lookup is done using the `findBy` to lookup on the current model to retrieve the related model record data
122
136
*/
123
- export const useLinkedChildModelRelatedModelRecords = ( props : {
137
+ const useLinkedChildModelRelatedModelRecords = ( props : {
124
138
belongsToFieldApiId : string ;
125
139
currentRecordId ?: string ;
126
140
currentModel : { apiIdentifier : string ; namespace ?: string [ ] | string | null } ;
@@ -154,7 +168,7 @@ export const useLinkedChildModelRelatedModelRecords = (props: {
154
168
/**
155
169
* For getting the related child model records in a HasOne/HasMany relationship
156
170
*/
157
- export const useLinkedParentModelRelatedModelRecords = ( props : {
171
+ const useLinkedParentModelRelatedModelRecords = ( props : {
158
172
relatedModel : {
159
173
apiIdentifier : string ;
160
174
namespace ?: string [ ] | string | null ;
@@ -177,7 +191,7 @@ export const useLinkedParentModelRelatedModelRecords = (props: {
177
191
pause : ! currentRecordId , // HasOne/HasMany need the current record to query the inverse field in the related model
178
192
179
193
first : selectedRecordsToLoadCount , // Many records can point to the current record in hasOne/hasMany
180
- filter : { [ inverseFieldApiIdentifier ] : { equals : currentRecordId } } , // Filter by the inverse field belongsTo field value
194
+ filter : { [ inverseFieldApiIdentifier + "Id" ] : { equals : currentRecordId } } , // Filter by the inverse field belongsTo field value
181
195
} ) ;
182
196
183
197
return {
@@ -189,8 +203,9 @@ export const useLinkedParentModelRelatedModelRecords = (props: {
189
203
} ;
190
204
} ;
191
205
192
- export const useAllRelatedModelRecords = ( props : {
206
+ const useAllRelatedModelRecords = ( props : {
193
207
optionLabel ?: OptionLabel ;
208
+ filter ?: Record < string , any > ;
194
209
relatedModel : { apiIdentifier : string ; namespace ?: string [ ] | string | null } ;
195
210
} ) => {
196
211
const { optionLabel, relatedModel } = props ;
@@ -204,6 +219,7 @@ export const useAllRelatedModelRecords = (props: {
204
219
205
220
const [ { data : newlyFetchedRecords , fetching, error } , _refetch ] = useFindMany ( relatedModelManager as any , {
206
221
first : optionRecordsToLoadCount ,
222
+ ...( props . filter && { filter : props . filter } ) ,
207
223
...( paginationPage && { after : paginationPage } ) ,
208
224
...( searchValue && { search : searchValue } ) ,
209
225
...( optionLabelIsFieldName && { select : { id : true , [ optionLabel ] : true } } ) ,
0 commit comments