Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to set titleProperty in resource options #1532

Merged
merged 1 commit into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/backend/decorators/property/property-options.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ export default interface PropertyOptions {
* Indicates if property should be treated as an ID
*/
isId?: boolean;

/**
* One of given property should be treated as an "title property". Title property is "clickable"
* when user sees the record in a list or show views.
*
* @deprecated Use ResourceOptions#titleProperty
*/
isTitle?: boolean;

Expand Down
9 changes: 8 additions & 1 deletion src/backend/decorators/resource/resource-decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,15 @@ class ResourceDecorator {
* @return {PropertyDecorator} PropertyDecorator of title property
*/
titleProperty(): PropertyDecorator {
let titleProperty

const properties = Object.values(this.properties)
const titleProperty = properties.find((p) => p.isTitle())
if (this.options.titleProperty) {
titleProperty = properties.find((p) => p.propertyPath === this.options.titleProperty)
} else {
titleProperty = properties.find((p) => p.isTitle())
}

return titleProperty || properties[0]
}

Expand Down
4 changes: 4 additions & 0 deletions src/backend/decorators/resource/resource-options.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ export interface ResourceOptions {
* List of properties which should be visible on the filter
*/
filterProperties?: Array<string>;
/**
* Name of title property
*/
titleProperty?: string;
/**
* Where resource link in sidebar should redirect. Default to the list action.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/backend/utils/build-feature/build-feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function mergeActionHooks<T>(
return hooks.length ? { [key]: hooks } : {}
}

const basicOptions = ['id', 'href', 'parent', 'sort', 'navigation'] as const
const basicOptions = ['id', 'href', 'parent', 'sort', 'navigation', 'titleProperty'] as const
const listOptions = [
'listProperties', 'showProperties', 'editProperties', 'filterProperties',
] as const
Expand Down
Loading