sort type by default

Signed-off-by: Mattia <matdtr@gmail.com>
This commit is contained in:
Mattia
2024-12-16 22:43:37 +01:00
parent 458878a0f5
commit 01250a873f
3 changed files with 7 additions and 38 deletions
@@ -189,34 +189,4 @@ describe('<EntityTypePicker/>', () => {
type: new EntityTypeFilter(['tool']),
});
});
it('sorts the available entity types when sorted prop is true', async () => {
await renderInTestApp(
<ApiProvider apis={apis}>
<MockEntityListContextProvider
value={{ filters: { kind: new EntityKindFilter('component') } }}
>
<EntityTypePicker sorted />
</MockEntityListContextProvider>
</ApiProvider>,
);
expect(screen.getByText('Type')).toBeInTheDocument();
const input = screen.getByTestId('select');
fireEvent.mouseDown(within(input).getByRole('button'));
await waitFor(() => screen.getByText('library'));
const sortedEntities = entities
.map(e => e.spec!.type as string)
.sort((a, b) =>
a
.toLocaleLowerCase('en-US')
.localeCompare(b.toLocaleLowerCase('en-US')),
);
sortedEntities.forEach(entityType => {
expect(screen.getByText(entityType)).toBeInTheDocument();
});
});
});
@@ -31,12 +31,11 @@ import { useTranslationRef } from '@backstage/core-plugin-api/alpha';
export interface EntityTypePickerProps {
initialFilter?: string;
hidden?: boolean;
sorted?: boolean;
}
/** @public */
export const EntityTypePicker = (props: EntityTypePickerProps) => {
const { hidden, initialFilter, sorted } = props;
const { hidden, initialFilter } = props;
const alertApi = useApi(alertApiRef);
const { error, availableTypes, selectedTypes, setSelectedTypes } =
useEntityTypeFilter();
@@ -55,11 +54,11 @@ export const EntityTypePicker = (props: EntityTypePickerProps) => {
}, [error, alertApi, initialFilter, setSelectedTypes, t]);
if (availableTypes.length === 0 || error) return null;
if (sorted) {
availableTypes.sort((a, b) =>
a.toLocaleLowerCase('en-US').localeCompare(b.toLocaleLowerCase('en-US')),
);
}
availableTypes.sort((a, b) =>
a.toLocaleLowerCase('en-US').localeCompare(b.toLocaleLowerCase('en-US')),
);
const items = [
{ value: 'all', label: t('entityTypePicker.optionAllTitle') },
...availableTypes.map((type: string) => ({