1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-19 18:29:23 +00:00

00 search result support keyboard

This commit is contained in:
Michael An
2023-10-11 15:58:06 +08:00
parent 6b5c750e82
commit 7c1577505e
3 changed files with 45 additions and 6 deletions

View File

@@ -1,10 +1,14 @@
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,
onMouseEnter: PropTypes.func,
onMouseLeave: PropTypes.func,
isHighlight: PropTypes.bool,
};
class SearchResultItem extends React.Component {
@@ -16,7 +20,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);
@@ -25,8 +28,13 @@ 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}
onMouseEnter={this.props.onMouseEnter}
onMouseLeave={this.props.onMouseLeave}
>
<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>

View File

@@ -28,6 +28,7 @@ class Search extends Component {
width: 'default',
value: '',
resultItems: [],
highlightIndex: 0,
page: 0,
isLoading: false,
hasMore: true,
@@ -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 = () => {
@@ -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
@@ -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);
@@ -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,
@@ -144,6 +168,7 @@ class Search extends Component {
});
} else {
this.setState({
highlightIndex: 0,
resultItems: [],
isLoading: false,
isResultGetted: true,
@@ -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,
@@ -171,6 +197,7 @@ class Search extends Component {
});
} else {
this.setState({
highlightIndex: 0,
resultItems: [],
isLoading: false,
isResultGetted: true,
@@ -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;
}
@@ -279,6 +307,9 @@ class Search extends Component {
key={index}
item={item}
onItemClickHandler={this.onItemClickHandler}
onMouseEnter={(e) => this.setState({ highlightIndex: index })}
onMouseLeave={(e) => this.setState({ highlightIndex: -1 })}
isHighlight={index === highlightIndex}
/>
);
})}