From d0db26c917bca2e39cc95eefd2cac22db774a060 Mon Sep 17 00:00:00 2001 From: Ulion Date: Wed, 5 Apr 2023 12:51:23 +0800 Subject: [PATCH] feat(small): add searchProperty option for resource to allow custom property search. --- src/backend/actions/search/search-action.ts | 6 ++++-- src/backend/decorators/resource/resource-decorator.ts | 11 +++++++++++ .../decorators/resource/resource-options.interface.ts | 4 ++++ src/backend/utils/build-feature/build-feature.ts | 2 +- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/backend/actions/search/search-action.ts b/src/backend/actions/search/search-action.ts index 896294437..a3bee1189 100644 --- a/src/backend/actions/search/search-action.ts +++ b/src/backend/actions/search/search-action.ts @@ -31,10 +31,12 @@ export const SearchAction: Action = { const { query } = request const decorated = resource.decorate() - const titlePropertyName = request.query?.searchProperty ?? decorated.titleProperty().name() + const titlePropertyName = request.query?.searchProperty ?? decorated.searchProperty().name() const { - sortBy = decorated.options?.sort?.sortBy || titlePropertyName, + sortBy = decorated.options?.sort?.sortBy + || request.query?.searchProperty + || decorated.titleProperty().name(), direction = 'asc', filters: customFilters = {}, perPage = 50, diff --git a/src/backend/decorators/resource/resource-decorator.ts b/src/backend/decorators/resource/resource-decorator.ts index 3bea98eb6..ef626697b 100644 --- a/src/backend/decorators/resource/resource-decorator.ts +++ b/src/backend/decorators/resource/resource-decorator.ts @@ -267,6 +267,17 @@ class ResourceDecorator { return record.get(this.titleProperty().name()) as string } + /** + * Returns PropertyDecorator of a property which should be used to search. + * If searchProperty not set, use titleProperty as fallback + * + * @return {PropertyDecorator} PropertyDecorator of search property + */ + searchProperty(): PropertyDecorator { + return (this.options.searchProperty && this.getPropertyByKey(this.options.searchProperty)) + || this.titleProperty() + } + getHref(currentAdmin?: CurrentAdmin): string | null { const { href } = this.options if (href) { diff --git a/src/backend/decorators/resource/resource-options.interface.ts b/src/backend/decorators/resource/resource-options.interface.ts index f7a52b499..d6684f7f5 100644 --- a/src/backend/decorators/resource/resource-options.interface.ts +++ b/src/backend/decorators/resource/resource-options.interface.ts @@ -105,6 +105,10 @@ export interface ResourceOptions { direction: 'asc' | 'desc'; sortBy: string; }; + /** + * Property which should be used to search + */ + searchProperty?: string; /** * List of properties along with their options */ diff --git a/src/backend/utils/build-feature/build-feature.ts b/src/backend/utils/build-feature/build-feature.ts index 555001cd6..c94e2cf53 100644 --- a/src/backend/utils/build-feature/build-feature.ts +++ b/src/backend/utils/build-feature/build-feature.ts @@ -29,7 +29,7 @@ function mergeActionHooks( return hooks.length ? { [key]: hooks } : {} } -const basicOptions = ['id', 'href', 'parent', 'sort', 'navigation'] as const +const basicOptions = ['id', 'href', 'parent', 'sort', 'navigation', 'searchProperty'] as const const listOptions = [ 'listProperties', 'showProperties', 'editProperties', 'filterProperties', ] as const