-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: references are now added as separate fields with suffix (#111)
BREAKING CHANGE: `ReferenceArrayField/Input` or `Referenceinput/Field` now require you to add a suffix to the `source` property: `_id` or `_ids` (array). E.g. consider a relation between User and UserRole. A User has many UserRoles. new: ```tsx <ReferenceArrayField label="Roles" source="roles_ids" // <-- suffix _ids because it is an array field reference="UserRole" >.... ``` before: ```tsx <ReferenceArrayField label="Roles" source="roles" // <--- old approach reference="UserRole" >.... ``` *Reasoning* There was some sanitizing going on to support using the parent field name (e.g. "roles") directly, without adding a suffix for convenience. However there were some problems and inconsitency: - if you use a custom fragment for a resource ("ResourceView") this sanitizing was already disabled - mixing custom fragments with the default behavior lead to cache inconsistencies
- Loading branch information
Showing
8 changed files
with
96 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
export const sanitizeKey = (key: string) => { | ||
if (key.endsWith("_ids")) { | ||
return key.substring(0, key.lastIndexOf("_ids")); | ||
} | ||
if (key.endsWith("_id")) { | ||
return key.substring(0, key.lastIndexOf("_id")); | ||
} | ||
return key; | ||
}; | ||
/** | ||
* Due to some implementation details in react-admin, we have to add copies with suffixed keys of certain field data. | ||
* This function sanitizes these keys: | ||
* - suffix _id: a string reference | ||
* - suffix _ids: an array of ids referencing | ||
* @param data data | ||
* @returns data where the suffixes got removed and the original data is overwritten with the suffixed version | ||
*/ | ||
export const sanitizeData = (data: { [key: string]: any }) => { | ||
return Object.fromEntries( | ||
Object.entries(data).reduce((acc, [keyRaw, value]) => { | ||
const key = sanitizeKey(keyRaw); | ||
return [...acc, [key, value]]; | ||
}, []), | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters