Skip to content

Commit

Permalink
Merge pull request #5681 from haiwen/search-result-can-use-keyboard-s…
Browse files Browse the repository at this point in the history
…elect

search result support keyboard
  • Loading branch information
Michael18811380328 authored Oct 11, 2023
2 parents 6b5c750 + 716efbd commit c342ece
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
10 changes: 7 additions & 3 deletions frontend/src/components/search/search-result-item.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import React from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { Utils } from '../../utils/utils';

const propTypes = {
item: PropTypes.object.isRequired,
onItemClickHandler: PropTypes.func.isRequired,
isHighlight: PropTypes.bool,
};

class SearchResultItem extends React.Component {
Expand All @@ -16,7 +18,6 @@ class SearchResultItem extends React.Component {

render() {
let item = this.props.item;
let className = item.link_content ? 'item-img' : 'lib-item-img';
let folderIconUrl = item.link_content ? Utils.getFolderIconUrl(false, 192) : Utils.getDefaultLibIconUrl(true);
let fileIconUrl = item.is_dir ? folderIconUrl : Utils.getFileIconUrl(item.name, 192);

Expand All @@ -25,8 +26,11 @@ class SearchResultItem extends React.Component {
}

return (
<li className="search-result-item" onClick={this.onClickHandler}>
<img className={className} src={fileIconUrl} alt="" />
<li
className={classnames('search-result-item', {'search-result-item-highlight': this.props.isHighlight })}
onClick={this.onClickHandler}
>
<img className={item.link_content ? 'item-img' : 'lib-item-img'} src={fileIconUrl} alt="" />
<div className="item-content">
<div className="item-name ellipsis">{item.name}</div>
<div className="item-link ellipsis">{item.repo_name}/{item.link_content}</div>
Expand Down
31 changes: 30 additions & 1 deletion frontend/src/components/search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Search extends Component {
width: 'default',
value: '',
resultItems: [],
highlightIndex: 0,
page: 0,
isLoading: false,
hasMore: true,
Expand Down Expand Up @@ -64,6 +65,26 @@ class Search extends Component {
e.preventDefault();
this.resetToDefault();
}
else if (isHotkey('enter', e)) {
e.preventDefault();
let item = this.state.resultItems[this.state.highlightIndex];
if (item) {
if (document.activeElement) {
document.activeElement.blur();
}
this.onItemClickHandler(item);
}
}
else if (isHotkey('up', e)) {
this.setState({
highlightIndex: Math.max(this.state.highlightIndex - 1, 0),
});
}
else if (isHotkey('down', e)) {
this.setState({
highlightIndex: Math.min(this.state.highlightIndex + 1, this.state.resultItems.length - 1),
});
}
};

onFocusHandler = () => {
Expand Down Expand Up @@ -94,6 +115,7 @@ class Search extends Component {

if (this.inputValue === '' || _this.getValueLength(this.inputValue) < 3) {
this.setState({
highlightIndex: 0,
resultItems: [],
isResultShow: false,
isResultGetted: false
Expand Down Expand Up @@ -122,6 +144,7 @@ class Search extends Component {
isResultShow: true,
isResultGetted: false,
resultItems: [],
highlightIndex: 0,
});
this.source = seafileAPI.getSource();
this.sendRequest(queryData, this.source.token, 1);
Expand All @@ -136,6 +159,7 @@ class Search extends Component {
this.source = null;
if (res.data.total > 0) {
this.setState({
highlightIndex: 0,
resultItems: [...this.state.resultItems, this.formatResultItems(res.data.results)],
isResultGetted: true,
page: page + 1,
Expand All @@ -144,6 +168,7 @@ class Search extends Component {
});
} else {
this.setState({
highlightIndex: 0,
resultItems: [],
isLoading: false,
isResultGetted: true,
Expand All @@ -163,6 +188,7 @@ class Search extends Component {
this.source = null;
if (res.data.total > 0) {
this.setState({
highlightIndex: 0,
resultItems: [...this.state.resultItems, ...this.formatResultItems(res.data.results)],
isResultGetted: true,
isLoading: false,
Expand All @@ -171,6 +197,7 @@ class Search extends Component {
});
} else {
this.setState({
highlightIndex: 0,
resultItems: [],
isLoading: false,
isResultGetted: true,
Expand Down Expand Up @@ -252,12 +279,13 @@ class Search extends Component {
isResultShow: false,
isResultGetted: false,
resultItems: [],
highlightIndex: 0,
isSearchInputShow: false,
});
}

renderSearchResult() {
const { resultItems } = this.state;
const { resultItems, highlightIndex } = this.state;
if (!this.state.isResultShow) {
return;
}
Expand All @@ -279,6 +307,7 @@ class Search extends Component {
key={index}
item={item}
onItemClickHandler={this.onItemClickHandler}
isHighlight={index === highlightIndex}
/>
);
})}
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/css/search.css
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,12 @@
font-size: 0.8125rem;
cursor: pointer;
margin-right: 1rem;
border-radius: 4px;
}

.search-result-container .search-result-item:hover {
.search-result-container .search-result-item:hover,
.search-result-container .search-result-item.search-result-item-highlight {
background-color: #f0f0f0;
border-radius: 4px;
}

.search-result-item .item-img {
Expand Down

0 comments on commit c342ece

Please sign in to comment.