Skip to content

feat(issues): Allow search suggestion to filter when selected #51564

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

Merged
merged 3 commits into from
Jun 27, 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
34 changes: 29 additions & 5 deletions static/app/components/smartSearchBar/index.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1084,11 +1084,7 @@ describe('SmartSearchBar', function () {
});
it('hides the default group after typing', async function () {
render(
<SmartSearchBar
{...defaultProps}
defaultSearchGroup={defaultSearchGroup}
query=""
/>
<SmartSearchBar {...defaultProps} defaultSearchGroup={defaultSearchGroup} />
);

const textbox = screen.getByRole('textbox');
Expand All @@ -1102,5 +1098,33 @@ describe('SmartSearchBar', function () {
screen.queryByTestId('default-search-group-wrapper')
).not.toBeInTheDocument();
});

it('hides the default group after picking item with applyFilter', async function () {
render(
<SmartSearchBar
{...defaultProps}
defaultSearchGroup={{
...defaultSearchGroup,
children: [
{
type: ItemType.RECOMMENDED,
title: 'Custom Tags',
// Filter is applied to all search items when picked
applyFilter: item => item.title === 'device',
},
],
}}
/>
);

const textbox = screen.getByRole('textbox');
await userEvent.click(textbox);
expect(await screen.findByText('User identification value')).toBeInTheDocument();
await userEvent.click(screen.getByText('Custom Tags'));

expect(screen.queryByText('Custom Tags')).not.toBeInTheDocument();
expect(screen.queryByText('User identification value')).not.toBeInTheDocument();
expect(screen.getByText('device')).toBeInTheDocument();
});
});
});
18 changes: 16 additions & 2 deletions static/app/components/smartSearchBar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1562,12 +1562,14 @@ class SmartSearchBar extends Component<DefaultProps & Props, State> {
* @param recentSearchItems List of recent search items, same format as searchItem
* @param tagName The current tag name in scope
* @param type Defines the type/state of the dropdown menu items
* @param skipDefaultGroup Force hide the default group even without a query
*/
updateAutoCompleteState(
searchItems: SearchItem[],
recentSearchItems: SearchItem[],
tagName: string,
type: ItemType
type: ItemType,
skipDefaultGroup = false
) {
const {
fieldDefinitionGetter,
Expand All @@ -1589,7 +1591,7 @@ class SmartSearchBar extends Component<DefaultProps & Props, State> {
maxSearchItems,
queryCharsLeft,
true,
defaultSearchGroup,
skipDefaultGroup ? undefined : defaultSearchGroup,
fieldDefinitionGetter
);

Expand Down Expand Up @@ -1781,6 +1783,18 @@ class SmartSearchBar extends Component<DefaultProps & Props, State> {
});
}

if (item.applyFilter) {
const [tagKeys, tagType] = this.getTagKeys('');
this.updateAutoCompleteState(
tagKeys.filter(item.applyFilter),
[],
'',
tagType,
true
);
return;
}

this.onAutoCompleteFromAst(replaceText, item);
};

Expand Down
4 changes: 4 additions & 0 deletions static/app/components/smartSearchBar/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export type SearchGroup = {

export type SearchItem = {
active?: boolean;
/**
* When this item is selected, apply a filter to the search query
*/
applyFilter?: (item: SearchItem) => void;
/**
* Call a callback instead of setting a value in the search query
*/
Expand Down
4 changes: 2 additions & 2 deletions static/app/views/issueList/searchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ function IssueListSearchBar({organization, tags, ...props}: Props) {
type: ItemType.RECOMMENDED,
title: t('Custom Tags'),
desc: t('Filter events by custom tags.'),
// TODO(scttcper): handle custom tags
value: 'url:',
// Shows only tags when clicked
applyFilter: item => item.kind === FieldKind.TAG,
},
],
};
Expand Down