@@ -20,7 +20,15 @@ import type {
2020 UseActionFormSubmit ,
2121} from "../use-action-form/types.js" ;
2222import { isPlainObject , processDefaultValues , toDefaultValues } from "../use-action-form/utils.js" ;
23- import { getRelatedModelFields , isHasManyOrHasManyThroughField , isRelationshipField , pathListToSelection } from "../use-table/helpers.js" ;
23+ import {
24+ fileSelection ,
25+ getRelatedModelFields ,
26+ isHasManyOrHasManyThroughField ,
27+ isRelationshipField ,
28+ pathListToSelection ,
29+ richTextSelection ,
30+ roleAssignmentsSelection ,
31+ } from "../use-table/helpers.js" ;
2432import type { FieldErrors , FieldValues , UseFormReturn } from "../useActionForm.js" ;
2533import { useActionForm } from "../useActionForm.js" ;
2634import { get , getFlattenedObjectKeys , set , type ErrorWrapper , type OptionsType } from "../utils.js" ;
@@ -219,7 +227,7 @@ const useFormSelection = (props: {
219227 if ( ! select || ! modelApiIdentifier ) {
220228 return ;
221229 }
222- return forceIdsIntoSelect ( { select, rootFieldsMetadata } ) ;
230+ return forceRequiredFieldsIntoSelect ( { select, rootFieldsMetadata } ) ;
223231 } , [ select , modelApiIdentifier , rootFieldsMetadata ] ) ;
224232
225233 if ( ! modelApiIdentifier || ! fields . length ) {
@@ -236,38 +244,56 @@ const useFormSelection = (props: {
236244 return pathListToSelection ( modelApiIdentifier , paths , fieldMetaData ) ;
237245} ;
238246
239- const forceIdsIntoSelect = ( props : { select : FieldSelection ; rootFieldsMetadata : FieldMetadata [ ] } ) => {
247+ const forceRequiredFieldsIntoSelect = ( props : { select : FieldSelection ; rootFieldsMetadata : FieldMetadata [ ] } ) => {
240248 const { select : originalSelect , rootFieldsMetadata } = props ;
241249 const select = structuredClone ( originalSelect ) ;
242250
243251 select . id = true ; // Always select the ID for the root model
244252
245- const addIdToSelection = ( selectPath : string , fieldMetadata : FieldMetadata ) => {
246- if ( ! isRelationshipField ( fieldMetadata ) ) {
247- return ; // Non relationships do not need additional selection
248- }
253+ const addRequiredFieldsToSelection = ( selectPath : string , fieldMetadata : FieldMetadata ) => {
254+ const isRichTextField = fieldMetadata . fieldType === FieldType . RichText ;
255+ const isFileField = fieldMetadata . fieldType === FieldType . File ;
256+ const isRolesField = fieldMetadata . fieldType === FieldType . RoleAssignments ;
257+ const isRelationship = isRelationshipField ( fieldMetadata ) ;
249258
250259 const existingSelection = get ( select , selectPath ) ;
251- if ( ! existingSelection || typeof existingSelection !== "object" ) {
252- // Do not go deeper than what is defined in the select object
253- return ;
260+ if ( ! existingSelection ) {
261+ return ; // Do not select at all
254262 }
255263
256- const isManyRelation = isHasManyOrHasManyThroughField ( fieldMetadata ) ;
257- const currentFieldSelectPathPrefix = isManyRelation ? ` ${ selectPath } .edges.node` : ` ${ selectPath } ` ;
258- const idPath = ` ${ currentFieldSelectPathPrefix } .id` ;
264+ if ( isRichTextField ) {
265+ return set ( select , selectPath , richTextSelection ) ; // Assume that the whole rich text is expected to be selected
266+ }
259267
260- set ( select , idPath , true ) ;
268+ if ( isFileField ) {
269+ return set ( select , selectPath , fileSelection ) ; // Assume whole file is expected to be selected
270+ }
271+ if ( isRolesField ) {
272+ return set ( select , selectPath , roleAssignmentsSelection ) ; // Assume whole role assignments are expected to be selected
273+ }
261274
262- const relatedModelFields = getRelatedModelFields ( fieldMetadata ) ;
275+ if ( isRelationship ) {
276+ if ( typeof existingSelection !== "object" ) {
277+ // Do not go deeper than what is defined in the select object
278+ return ;
279+ }
280+
281+ const isManyRelation = isHasManyOrHasManyThroughField ( fieldMetadata ) ;
282+ const currentFieldSelectPathPrefix = isManyRelation ? `${ selectPath } .edges.node` : `${ selectPath } ` ;
283+ const idPath = `${ currentFieldSelectPathPrefix } .id` ;
284+
285+ set ( select , idPath , true ) ;
263286
264- for ( const relatedModelField of relatedModelFields ) {
265- addIdToSelection ( `${ currentFieldSelectPathPrefix } .${ relatedModelField . apiIdentifier } ` , relatedModelField ) ;
287+ const relatedModelFields = getRelatedModelFields ( fieldMetadata ) ;
288+
289+ for ( const relatedModelField of relatedModelFields ) {
290+ addRequiredFieldsToSelection ( `${ currentFieldSelectPathPrefix } .${ relatedModelField . apiIdentifier } ` , relatedModelField ) ;
291+ }
266292 }
267293 } ;
268294
269295 for ( const field of rootFieldsMetadata ) {
270- addIdToSelection ( field . apiIdentifier , field ) ;
296+ addRequiredFieldsToSelection ( field . apiIdentifier , field ) ;
271297 }
272298
273299 return select ;
0 commit comments