1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-08-25 18:20:48 +00:00

Merge pull request #5681 from haiwen/search-result-can-use-keyboard-select

search result support keyboard
This commit is contained in:
Michael An 2023-10-11 16:49:31 +08:00 committed by GitHub
commit c342ece474
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 6 deletions

View File

@ -1,10 +1,12 @@
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import classnames from 'classnames';
import { Utils } from '../../utils/utils'; import { Utils } from '../../utils/utils';
const propTypes = { const propTypes = {
item: PropTypes.object.isRequired, item: PropTypes.object.isRequired,
onItemClickHandler: PropTypes.func.isRequired, onItemClickHandler: PropTypes.func.isRequired,
isHighlight: PropTypes.bool,
}; };
class SearchResultItem extends React.Component { class SearchResultItem extends React.Component {
@ -16,7 +18,6 @@ class SearchResultItem extends React.Component {
render() { render() {
let item = this.props.item; 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 folderIconUrl = item.link_content ? Utils.getFolderIconUrl(false, 192) : Utils.getDefaultLibIconUrl(true);
let fileIconUrl = item.is_dir ? folderIconUrl : Utils.getFileIconUrl(item.name, 192); let fileIconUrl = item.is_dir ? folderIconUrl : Utils.getFileIconUrl(item.name, 192);
@ -25,8 +26,11 @@ class SearchResultItem extends React.Component {
} }
return ( return (
<li className="search-result-item" onClick={this.onClickHandler}> <li
<img className={className} src={fileIconUrl} alt="" /> 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-content">
<div className="item-name ellipsis">{item.name}</div> <div className="item-name ellipsis">{item.name}</div>
<div className="item-link ellipsis">{item.repo_name}/{item.link_content}</div> <div className="item-link ellipsis">{item.repo_name}/{item.link_content}</div>

View File

@ -28,6 +28,7 @@ class Search extends Component {
width: 'default', width: 'default',
value: '', value: '',
resultItems: [], resultItems: [],
highlightIndex: 0,
page: 0, page: 0,
isLoading: false, isLoading: false,
hasMore: true, hasMore: true,
@ -64,6 +65,26 @@ class Search extends Component {
e.preventDefault(); e.preventDefault();
this.resetToDefault(); 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 = () => { onFocusHandler = () => {
@ -94,6 +115,7 @@ class Search extends Component {
if (this.inputValue === '' || _this.getValueLength(this.inputValue) < 3) { if (this.inputValue === '' || _this.getValueLength(this.inputValue) < 3) {
this.setState({ this.setState({
highlightIndex: 0,
resultItems: [], resultItems: [],
isResultShow: false, isResultShow: false,
isResultGetted: false isResultGetted: false
@ -122,6 +144,7 @@ class Search extends Component {
isResultShow: true, isResultShow: true,
isResultGetted: false, isResultGetted: false,
resultItems: [], resultItems: [],
highlightIndex: 0,
}); });
this.source = seafileAPI.getSource(); this.source = seafileAPI.getSource();
this.sendRequest(queryData, this.source.token, 1); this.sendRequest(queryData, this.source.token, 1);
@ -136,6 +159,7 @@ class Search extends Component {
this.source = null; this.source = null;
if (res.data.total > 0) { if (res.data.total > 0) {
this.setState({ this.setState({
highlightIndex: 0,
resultItems: [...this.state.resultItems, this.formatResultItems(res.data.results)], resultItems: [...this.state.resultItems, this.formatResultItems(res.data.results)],
isResultGetted: true, isResultGetted: true,
page: page + 1, page: page + 1,
@ -144,6 +168,7 @@ class Search extends Component {
}); });
} else { } else {
this.setState({ this.setState({
highlightIndex: 0,
resultItems: [], resultItems: [],
isLoading: false, isLoading: false,
isResultGetted: true, isResultGetted: true,
@ -163,6 +188,7 @@ class Search extends Component {
this.source = null; this.source = null;
if (res.data.total > 0) { if (res.data.total > 0) {
this.setState({ this.setState({
highlightIndex: 0,
resultItems: [...this.state.resultItems, ...this.formatResultItems(res.data.results)], resultItems: [...this.state.resultItems, ...this.formatResultItems(res.data.results)],
isResultGetted: true, isResultGetted: true,
isLoading: false, isLoading: false,
@ -171,6 +197,7 @@ class Search extends Component {
}); });
} else { } else {
this.setState({ this.setState({
highlightIndex: 0,
resultItems: [], resultItems: [],
isLoading: false, isLoading: false,
isResultGetted: true, isResultGetted: true,
@ -252,12 +279,13 @@ class Search extends Component {
isResultShow: false, isResultShow: false,
isResultGetted: false, isResultGetted: false,
resultItems: [], resultItems: [],
highlightIndex: 0,
isSearchInputShow: false, isSearchInputShow: false,
}); });
} }
renderSearchResult() { renderSearchResult() {
const { resultItems } = this.state; const { resultItems, highlightIndex } = this.state;
if (!this.state.isResultShow) { if (!this.state.isResultShow) {
return; return;
} }
@ -279,6 +307,7 @@ class Search extends Component {
key={index} key={index}
item={item} item={item}
onItemClickHandler={this.onItemClickHandler} onItemClickHandler={this.onItemClickHandler}
isHighlight={index === highlightIndex}
/> />
); );
})} })}

View File

@ -102,11 +102,12 @@
font-size: 0.8125rem; font-size: 0.8125rem;
cursor: pointer; cursor: pointer;
margin-right: 1rem; 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; background-color: #f0f0f0;
border-radius: 4px;
} }
.search-result-item .item-img { .search-result-item .item-img {