Skip to content

Commit

Permalink
fix: check title before collecting search result.(close #33)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyi committed Aug 13, 2020
1 parent 661becb commit 1e54f12
Showing 1 changed file with 15 additions and 53 deletions.
68 changes: 15 additions & 53 deletions packages/@rcpress/theme-default/components/search-box/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react';
import { List, Input, Icon, Breadcrumb } from 'antd';
import { PageInfo } from '../utils';
import Link from '../MyLink';

type filterDatas = {
Expand All @@ -17,7 +16,7 @@ interface SearchState {
}

interface SearchProps {
datas: Array<PageInfo>;
datas: Array<any>;
max: number;
mobile?: boolean;
}
Expand All @@ -33,18 +32,13 @@ function flattenToc(items: any[]): any[] {
return items
? items.reduce((pre, cur) => {
return pre
.concat(cur as any)
.concat((cur.items && cur.items.length
? flattenToc(cur.items)
: []) as any);
.concat(cur.title ? cur : [])
.concat((cur.items && cur.items.length ? flattenToc(cur.items) : []) as any);
}, [])
: [];
}

export default class Search extends React.Component<
SearchProps,
SearchState
> {
export default class Search extends React.Component<SearchProps, SearchState> {
searchInput: Input | null | undefined;
isClickLink: boolean = false;

Expand Down Expand Up @@ -103,7 +97,7 @@ export default class Search extends React.Component<
);
}

function resolveOnePageItem(currentItem: PageInfo) {
function resolveOnePageItem(currentItem: any) {
if (match(currentItem.title, query)) {
results.push([
{
Expand All @@ -112,17 +106,9 @@ export default class Search extends React.Component<
important: currentItem.important
}
]);
} else if (
currentItem.toc &&
currentItem.toc.items &&
currentItem.toc.items.length
) {
} else if (currentItem.toc && currentItem.toc.items && currentItem.toc.items.length) {
let tocs = flattenToc(currentItem.toc.items);
for (
let i = 0;
i < tocs.length && results.length < max;
i++
) {
for (let i = 0; i < tocs.length && results.length < max; i++) {
let t = tocs[i];
if (match(t.title, query)) {
results.push([
Expand All @@ -140,23 +126,12 @@ export default class Search extends React.Component<
}
}

for (
let i = 0;
i < datas.length && results.length < max;
i++
) {
for (let i = 0; i < datas.length && results.length < max; i++) {
const currentItem = datas[i];
if (currentItem.path) {
resolveOnePageItem(currentItem);
} else if (
currentItem.children &&
currentItem.children.length
) {
for (
let j = 0;
j < currentItem.children.length;
j++
) {
} else if (currentItem.children && currentItem.children.length) {
for (let j = 0; j < currentItem.children.length; j++) {
resolveOnePageItem(currentItem.children[j]);
}
}
Expand All @@ -175,10 +150,7 @@ export default class Search extends React.Component<
<div id="search-box" className="search-box">
<div className="searchInput-component">
<div className="icon-container">
<Icon
type="search"
onClick={this.handleClickIcon}
/>
<Icon type="search" onClick={this.handleClickIcon} />
</div>
<Input
ref={ref => {
Expand All @@ -193,19 +165,15 @@ export default class Search extends React.Component<
</div>

<div className="search-result-list">
{this.state.isSearchListShow &&
this.state.filterDatas.length ? (
{this.state.isSearchListShow && this.state.filterDatas.length ? (
<List
key="search-list"
dataSource={filterDatas}
renderItem={dataItem => {
return (
<List.Item>
<Link
to={
dataItem[dataItem.length - 1]
.url as string
}
to={dataItem[dataItem.length - 1].url as string}
className="search-item "
onMouseDown={() => {
this.isClickLink = true;
Expand All @@ -217,10 +185,7 @@ export default class Search extends React.Component<
>
<List.Item.Meta
description={
<Breadcrumb
separator=">"
className="ellipsis"
>
<Breadcrumb separator=">" className="ellipsis">
{dataItem.map((item, index) => (
// <Badge dot={i.important}>
<Breadcrumb.Item key={index}>
Expand Down Expand Up @@ -251,10 +216,7 @@ export default class Search extends React.Component<
componentDidMount() {
const { searchInput } = this;
document.addEventListener('keyup', event => {
if (
event.keyCode === 83 &&
event.target === document.body
) {
if (event.keyCode === 83 && event.target === document.body) {
searchInput && searchInput.focus();
}
});
Expand Down

0 comments on commit 1e54f12

Please sign in to comment.