GithubHelp home page GithubHelp logo

xinchanghao / input-search Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 17 KB

:peach::peach::peach: A real-time search component

Home Page: https://5ee446fe8448670007dd563d--chhxin.netlify.app/

License: MIT License

JavaScript 25.66% CSS 4.74% TypeScript 69.59%
input-search react-component hoc search-input

input-search's Introduction

@chhxin/input-search

TODO: 现在还没提到 npm !!!

安装

npm install --save @chhxin/input-search

storybook 启动

npm install
npm run storybook

使用

import React, { Component } from "react";
import fetch from "unfetch";

import SearchBar from "@chhxin/input-search";
import "@chhxin/input-search/lib/index.css";

const URL = "https://api.npms.io/v2/search/suggestions";
function NpmSearchExtension(query) {
  const prom = fetch(`${URL}?q=${query}&size=10`);
  return prom
    .then((res) => res.json())
    .then((packages) =>
      packages.map((item) => ({
        title: item.package.name,
        url: item.package.links.npm,
      }))
    );
}

function BasicExtension() {
  return [
    { title: "a", url: "https://a.com" },
    { title: "b", url: "https://b.com" },
    { title: "c", url: "https://c.com" },
  ];
}

class Example extends Component {
  render() {
    return <SearchBar extensions={[BasicExtension]} />;
  }
}

扩展

import React, { Component } from "react";
import fetch from "unfetch";
import SearchBar from "@chhxin/input-search";
import "@chhxin/input-search/lib/index.css";

const URL = "https://api.npms.io/v2/search/suggestions";
function NpmSearchExtension(query) {
  const prom = fetch(`${URL}?q=${query}&size=10`);
  return prom
    .then((res) => res.json())
    .then((packages) =>
      packages.map((item) => ({
        title: item.package.name,
        url: item.package.links.npm,
        author: item.package.author.name,
      }))
    );
}

function ResultItem({ item }) {
  return (
    <div>
      <a href={item.url}>{item.author + item.title}</a>
    </div>
  );
}

class Example extends Component {
  render() {
    return <SearchBar extensions={[NpmSearchExtension]} render={ResultItem} />;
  }
}

// or
class Example extends Component {
  render() {
    return (
      <SearchBar extensions={[NpmSearchExtension]}>
        {({ item }) => <div>{item.author + item.title}</div>}
      </SearchBar>
    );
  }
}

HOC

import React, { Component } from "react";
import fetch from "unfetch";
import SearchBar, { withExtensions } from "@chhxin/input-search";
import "@chhxin/input-search/lib/index.css";

function GitHubSearchExtension(query) {
  const prom = fetch(`https://api.github.com/search/repositories?q=${query}`);

  return prom
    .then((res) => res.json())
    .then((resp) =>
      resp.items.map((item) => ({
        title: item.full_name,
        url: item.html_url,
      }))
    );
}

const GitHubSearchBar = withExtensions([GitHubSearchExtension])(SearchBar);

class Example extends Component {
  render() {
    return <GitHubSearchBar />;
  }
}

国际化

简单的实现了 placeholder 的多语言配置

多主题

简单的实现了 darklight 两个内置主题

Props

Prop Type Required Description
value string false 自定义搜索框 value
placeholder string false placeholder
className string false 根节点 class
theme dark light false 主题,用来修改输入框 hover 时的边框颜色
autoFocus boolean false autoFocus
children Function false 自定义单个结果的渲染方法, 参数{ item, isSelected, isHighlighted }
render Function false 与 children 相同
maxResults number false 最多展示多少条结果
maxViewableResults number false 未滚动时最多展示多少条结果
inputDelay number false 延迟查询 debounce 时间
extensions Array<Function> false 查询 function 数组
style React.CSSProperties false 输入框 style
resultsStyle object false 结果框 style
rootStyle object false 根节点 style
onSelect Function false 结果选中时回调
onQuery Function false 当触发某项搜索时回调
onChange Function false 输入框值变化时回调
onFocus Function false 输入框获得焦点时回调
onBlur Function false 输入框是去焦点时回调

input-search's People

Contributors

xinchanghao avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.