Skip to content

Commit 2d790fc

Browse files
committed
feat(small): add searchProperty option for resource to allow custom property search.
1 parent 7fc4ca4 commit 2d790fc

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

src/backend/actions/search/search-action.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ export const SearchAction: Action<SearchActionResponse> = {
3131
const { query } = request
3232

3333
const decorated = resource.decorate()
34-
const titlePropertyName = request.query?.searchProperty ?? decorated.titleProperty().name()
34+
const titlePropertyName = query?.searchProperty ?? decorated.searchProperty().name()
3535

3636
const {
37-
sortBy = decorated.options?.sort?.sortBy || titlePropertyName,
37+
sortBy = decorated.options?.sort?.sortBy
38+
|| query?.searchProperty
39+
|| decorated.titleProperty().name(),
3840
direction = 'asc',
3941
filters: customFilters = {},
4042
perPage = 50,

src/backend/decorators/resource/resource-decorator.ts

+11
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,17 @@ class ResourceDecorator {
267267
return record.get(this.titleProperty().name()) as string
268268
}
269269

270+
/**
271+
* Returns PropertyDecorator of a property which should be used to search.
272+
* If searchProperty not set, use titleProperty as fallback
273+
*
274+
* @return {PropertyDecorator} PropertyDecorator of search property
275+
*/
276+
searchProperty(): PropertyDecorator {
277+
return (this.options.searchProperty && this.getPropertyByKey(this.options.searchProperty))
278+
|| this.titleProperty()
279+
}
280+
270281
getHref(currentAdmin?: CurrentAdmin): string | null {
271282
const { href } = this.options
272283
if (href) {

src/backend/decorators/resource/resource-options.interface.ts

+4
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ export interface ResourceOptions {
105105
direction: 'asc' | 'desc';
106106
sortBy: string;
107107
};
108+
/**
109+
* Property which should be used to search
110+
*/
111+
searchProperty?: string;
108112
/**
109113
* List of properties along with their options
110114
*/

src/backend/utils/build-feature/build-feature.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function mergeActionHooks<T>(
2929
return hooks.length ? { [key]: hooks } : {}
3030
}
3131

32-
const basicOptions = ['id', 'href', 'parent', 'sort', 'navigation'] as const
32+
const basicOptions = ['id', 'href', 'parent', 'sort', 'navigation', 'searchProperty'] as const
3333
const listOptions = [
3434
'listProperties', 'showProperties', 'editProperties', 'filterProperties',
3535
] as const

0 commit comments

Comments
 (0)