add blueprint extension for result type filter in search plugin
Signed-off-by: Mark Dunphy <markd@spotify.com>
This commit is contained in:
@@ -17,6 +17,43 @@ export type BaseSearchResultListItemProps<T = {}> = T & {
|
||||
result?: SearchDocument;
|
||||
} & Omit<ListItemProps, 'button'>;
|
||||
|
||||
// @alpha (undocumented)
|
||||
export const SearchFilterResultTypeBlueprint: ExtensionBlueprint<{
|
||||
kind: 'search-filter-result-type';
|
||||
name: undefined;
|
||||
params: SearchFilterResultTypeBlueprintParams;
|
||||
output: ConfigurableExtensionDataRef<
|
||||
{
|
||||
value: string;
|
||||
name: string;
|
||||
icon: JSX.Element;
|
||||
},
|
||||
'search.filters.result-types.type',
|
||||
{}
|
||||
>;
|
||||
inputs: {};
|
||||
config: {};
|
||||
configInput: {};
|
||||
dataRefs: {
|
||||
resultType: ConfigurableExtensionDataRef<
|
||||
{
|
||||
value: string;
|
||||
name: string;
|
||||
icon: JSX.Element;
|
||||
},
|
||||
'search.filters.result-types.type',
|
||||
{}
|
||||
>;
|
||||
};
|
||||
}>;
|
||||
|
||||
// @alpha (undocumented)
|
||||
export interface SearchFilterResultTypeBlueprintParams {
|
||||
icon: JSX.Element;
|
||||
name: string;
|
||||
value: string;
|
||||
}
|
||||
|
||||
// @alpha (undocumented)
|
||||
export type SearchResultItemExtensionComponent = <
|
||||
P extends BaseSearchResultListItemProps,
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* Copyright 2025 The Backstage Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import React from 'react';
|
||||
import {
|
||||
PageBlueprint,
|
||||
createExtensionInput,
|
||||
} from '@backstage/frontend-plugin-api';
|
||||
import { SearchFilterResultTypeBlueprint } from './SearchFilterResultTypeBlueprint';
|
||||
import { searchResultTypeDataRef } from './types';
|
||||
import {
|
||||
createExtensionTester,
|
||||
renderInTestApp,
|
||||
} from '@backstage/frontend-test-utils';
|
||||
|
||||
describe('SearchFilterResultTypeBlueprint', () => {
|
||||
it('should return an extension', () => {
|
||||
const extension = SearchFilterResultTypeBlueprint.make({
|
||||
name: 'test',
|
||||
params: {
|
||||
value: 'test',
|
||||
name: 'Test',
|
||||
icon: <div>Hello</div>,
|
||||
},
|
||||
});
|
||||
|
||||
expect(extension).toMatchInlineSnapshot(`
|
||||
{
|
||||
"$$type": "@backstage/ExtensionDefinition",
|
||||
"T": undefined,
|
||||
"attachTo": {
|
||||
"id": "page:search",
|
||||
"input": "resultTypes",
|
||||
},
|
||||
"configSchema": undefined,
|
||||
"disabled": false,
|
||||
"factory": [Function],
|
||||
"inputs": {},
|
||||
"kind": "search-filter-result-type",
|
||||
"name": "test",
|
||||
"output": [
|
||||
[Function],
|
||||
],
|
||||
"override": [Function],
|
||||
"toString": [Function],
|
||||
"version": "v2",
|
||||
}
|
||||
`);
|
||||
});
|
||||
|
||||
it('should render result types', async () => {
|
||||
const extension = SearchFilterResultTypeBlueprint.make({
|
||||
name: 'test',
|
||||
params: {
|
||||
value: 'test',
|
||||
name: 'Test Result Type',
|
||||
icon: <div>Hello</div>,
|
||||
},
|
||||
});
|
||||
|
||||
const searchPage = PageBlueprint.makeWithOverrides({
|
||||
name: 'search',
|
||||
inputs: {
|
||||
resultTypes: createExtensionInput([searchResultTypeDataRef]),
|
||||
},
|
||||
factory(originalFactory, { inputs }) {
|
||||
return originalFactory({
|
||||
defaultPath: '/',
|
||||
loader: async () => {
|
||||
const resultTypes = inputs.resultTypes.map(t =>
|
||||
t.get(searchResultTypeDataRef),
|
||||
);
|
||||
return (
|
||||
<div>
|
||||
{resultTypes.map((t, i) => (
|
||||
<div key={i}>{t.name}</div>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
},
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
await expect(
|
||||
renderInTestApp(
|
||||
createExtensionTester(searchPage).add(extension).reactElement(),
|
||||
).findByText('Test Result Type'),
|
||||
).resolves.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2025 The Backstage Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { createExtensionBlueprint } from '@backstage/frontend-plugin-api';
|
||||
import { searchResultTypeDataRef } from './types';
|
||||
|
||||
/** @alpha */
|
||||
export interface SearchFilterResultTypeBlueprintParams {
|
||||
/**
|
||||
* The value of the result type.
|
||||
*/
|
||||
value: string;
|
||||
/**
|
||||
* The name of the result type.
|
||||
*/
|
||||
name: string;
|
||||
/**
|
||||
* The icon of the result type.
|
||||
*/
|
||||
icon: JSX.Element;
|
||||
}
|
||||
|
||||
/**
|
||||
* @alpha
|
||||
*/
|
||||
export const SearchFilterResultTypeBlueprint = createExtensionBlueprint({
|
||||
kind: 'search-filter-result-type',
|
||||
attachTo: {
|
||||
id: 'page:search',
|
||||
input: 'resultTypes',
|
||||
},
|
||||
output: [searchResultTypeDataRef],
|
||||
dataRefs: {
|
||||
resultType: searchResultTypeDataRef,
|
||||
},
|
||||
*factory(params: SearchFilterResultTypeBlueprintParams) {
|
||||
yield searchResultTypeDataRef({
|
||||
value: params.value,
|
||||
name: params.name,
|
||||
icon: params.icon,
|
||||
});
|
||||
},
|
||||
});
|
||||
@@ -23,3 +23,8 @@ export {
|
||||
type SearchResultItemExtensionComponent,
|
||||
type SearchResultItemExtensionPredicate,
|
||||
} from './types';
|
||||
|
||||
export {
|
||||
SearchFilterResultTypeBlueprint,
|
||||
type SearchFilterResultTypeBlueprintParams,
|
||||
} from './SearchFilterResultTypeBlueprint';
|
||||
|
||||
@@ -40,3 +40,9 @@ export const searchResultListItemDataRef = createExtensionDataRef<{
|
||||
predicate?: SearchResultItemExtensionPredicate;
|
||||
component: SearchResultItemExtensionComponent;
|
||||
}>().with({ id: 'search.search-result-list-item.item' });
|
||||
|
||||
export const searchResultTypeDataRef = createExtensionDataRef<{
|
||||
value: string;
|
||||
name: string;
|
||||
icon: JSX.Element;
|
||||
}>().with({ id: 'search.filters.result-types.type' });
|
||||
|
||||
Reference in New Issue
Block a user