0.1.0 • Published 6 years ago

google-search-for-react v0.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
6 years ago

google-search-for-react

English | 简体中文

A simple search components it's UI just like google search

Installation

npm i google-search-for-react --save

Usage

import React from 'react'
import Search from 'google-search-for-react'
class Demo extends React.PureComponent {

  mockData = value =>
    Array(18).fill({ name: `${value} :随机数${Math.random()}` });
  onSearchHistory = value =>
    new Promise(resolve => resolve(this.mockData(`${value}历史数据`)));
  onSearch = value => new Promise(resolve => resolve(this.mockData(value)));
  onEnter = value => {
    console.log("searchword:" + value);
  };
  placeholder = open =>
    open
      ? "指标搜索:最多支持三个关键词搜索,以空格隔开"
      : "指标搜索 (微贷/财富/保险)";
  renderContent = options => {
    const { rows, highlight, searchWords, handleClose } = options;
    return rows.map((item, index) => {
      const text = highlight(searchWords, item.name);
      return (
        <li
          key={`${index}content_li`}
          onClick={() => {
            console.log(item.name);
            handleClose();
          }}
        >
          <div dangerouslySetInnerHTML={{ __html: text }} />
        </li>
      );
    });
  };
  render() {
    return (
      <div>
        <div style={{ width: "90%" }}>
          <Search
            anchor="right"
            placeholder={this.placeholder}
            onSearchHistory={this.onSearchHistory} // 历史搜索
            onSearch={this.onSearch} // 搜索查询
            onEnter={this.onEnter} // 回车键入
            renderContent={this.renderContent} // 渲染内容
            renderFooter={this.renderFooter}
          />
        </div>
      </div>
    );
  }
}

API

成员说明类型默认值
anchorsearchbar展开的方向String'right'
placeholder必填,输入框底文String/Function:(isOpen)=>{}
onSearch搜索查询Function (searchWords)=> Promise
onSearchHistory查询历史记录Function (searchWords)=> Promise
onEnter搜索回车事件Function (searchWords)=>{}
renderHeader自定义搜索结果面板的headerFunction (rows)=>{}
renderContent定义搜索结果面板的ContentFunction ( {rows,highlight,searchWords,handleClose})=>{}
renderFooter定义搜索结果面板的FooterFunction (rows)=>{}

Feature

  • Highlight Search:通过点击searchbar后,搜索框动态放大,占据视觉主体,从而突出搜索功能
  • Intelligence Related:智能联想,补全用户想要的内容
  • Recent Pages/Most Visted Pages:最近搜索的内容和热度最多的内容,帮助用户快速检索,增加用户粘性
  • One Search , All Products:同一体系下,搜索的交互和体验一致,提供后端接口服务
  • Search as a service:提供后端接口API,高效灵活,适配不同场景

reference

  • google search npm.io npm.io