Skip to content
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

feat(ui): Add command-k icons to search bar #9194

Merged
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
1 change: 1 addition & 0 deletions datahub-web-react/src/app/home/HomePageHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ export const HomePageHeader = () => {
combineSiblings
showQuickFilters
showViewAllResults
showCommandK
/>
{searchResultsToShow && searchResultsToShow.length > 0 && (
<SuggestionsContainer>
Expand Down
29 changes: 29 additions & 0 deletions datahub-web-react/src/app/search/CommandK.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react';
import styled from 'styled-components';
import { ANTD_GRAY } from '../entity/shared/constants';

const Container = styled.div`
color: ${ANTD_GRAY[6]};
background-color: #ffffff;
opacity: 0.9;
border-color: black;
border-radius: 6px;
border: 1px solid ${ANTD_GRAY[6]};
padding-right: 6px;
padding-left: 6px;
margin-right: 4px;
margin-left: 4px;
`;

const Letter = styled.span`
padding: 2px;
`;

export const CommandK = () => {
return (
<Container>
<Letter>⌘</Letter>
<Letter>K</Letter>
</Container>
);
};
37 changes: 24 additions & 13 deletions datahub-web-react/src/app/search/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { navigateToSearchUrl } from './utils/navigateToSearchUrl';
import ViewAllSearchItem from './ViewAllSearchItem';
import { ViewSelect } from '../entity/view/select/ViewSelect';
import { combineSiblingsInAutoComplete } from './utils/combineSiblingsInAutoComplete';
import { CommandK } from './CommandK';

const StyledAutoComplete = styled(AutoComplete)`
width: 100%;
Expand Down Expand Up @@ -114,6 +115,7 @@ interface Props {
fixAutoComplete?: boolean;
hideRecommendations?: boolean;
showQuickFilters?: boolean;
showCommandK?: boolean;
viewsEnabled?: boolean;
combineSiblings?: boolean;
setIsSearchBarFocused?: (isSearchBarFocused: boolean) => void;
Expand Down Expand Up @@ -142,6 +144,7 @@ export const SearchBar = ({
fixAutoComplete,
hideRecommendations,
showQuickFilters,
showCommandK = false,
viewsEnabled = false,
combineSiblings = false,
setIsSearchBarFocused,
Expand All @@ -153,6 +156,8 @@ export const SearchBar = ({
const [searchQuery, setSearchQuery] = useState<string | undefined>(initialQuery);
const [selected, setSelected] = useState<string>();
const [isDropdownVisible, setIsDropdownVisible] = useState(false);
const [isFocused, setIsFocused] = useState(false);

useEffect(() => setSelected(initialQuery), [initialQuery]);

const searchEntityTypes = entityRegistry.getSearchEntityTypes();
Expand Down Expand Up @@ -277,11 +282,13 @@ export const SearchBar = ({
function handleFocus() {
if (onFocus) onFocus();
handleSearchBarClick(true);
setIsFocused(true);
}

function handleBlur() {
if (onBlur) onBlur();
handleSearchBarClick(false);
setIsFocused(false);
}

function handleSearch(query: string, type?: EntityType, appliedQuickFilters?: FacetFilterInput[]) {
Expand All @@ -294,18 +301,21 @@ export const SearchBar = ({
const searchInputRef = useRef(null);

useEffect(() => {
const handleKeyDown = (event) => {
// Support command-k to select the search bar.
// 75 is the keyCode for 'k'
if ((event.metaKey || event.ctrlKey) && event.keyCode === 75) {
(searchInputRef?.current as any)?.focus();
}
};
document.addEventListener('keydown', handleKeyDown);
return () => {
document.removeEventListener('keydown', handleKeyDown);
};
}, []);
if (showCommandK) {
const handleKeyDown = (event) => {
// Support command-k to select the search bar.
// 75 is the keyCode for 'k'
if ((event.metaKey || event.ctrlKey) && event.keyCode === 75) {
(searchInputRef?.current as any)?.focus();
}
};
document.addEventListener('keydown', handleKeyDown);
return () => {
document.removeEventListener('keydown', handleKeyDown);
};
}
return () => null;
}, [showCommandK]);
Comment on lines +304 to +318
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh nice so we already supported this!


return (
<AutoCompleteContainer style={style} ref={searchBarWrapperRef}>
Expand Down Expand Up @@ -377,7 +387,7 @@ export const SearchBar = ({
data-testid="search-input"
onFocus={handleFocus}
onBlur={handleBlur}
allowClear={{ clearIcon: <ClearIcon /> }}
allowClear={(isFocused && { clearIcon: <ClearIcon /> }) || false}
prefix={
<>
{viewsEnabled && (
Expand Down Expand Up @@ -411,6 +421,7 @@ export const SearchBar = ({
</>
}
ref={searchInputRef}
suffix={(showCommandK && !isFocused && <CommandK />) || null}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you think we need this command k icon? tbh it feels a little busy and maybe not necessary to show for this. we could show something as a tooltip when you hover over the search bar that it's possible to do command K?

/>
</StyledAutoComplete>
</AutoCompleteContainer>
Expand Down
1 change: 1 addition & 0 deletions datahub-web-react/src/app/search/SearchHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export const SearchHeader = ({
fixAutoComplete
showQuickFilters
showViewAllResults
showCommandK
/>
</LogoSearchContainer>
<NavGroup>
Expand Down
Loading