React 程序设计简单的轻量级自动完成搜索框应用

 更新时间:2022年10月25日 15:22:10   作者:Jovie  
这篇文章主要为大家介绍了React 程序设计简单的轻量级自动完成搜索框应用,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

实现效果

一个为React应用程序设计的简单的轻量级自动完成搜索框。

如何使用它

1.安装并导入该组件

# Yarn
$ yarn add react-search-box
# NPM
$ npm i react-search-box
import React, { Component } from "react";
import ReactSearchBox from "react-search-box";

2.将ReactSearchBox 组件添加到应用程序中

<ReactSearchBox
  placeholder="Type Something..."
  value="ReactScript"
  data={this.data}
  callback={(record) => console.log(record)}
/>

3.定义你的自动建议列表的数据

export default class App extends Component {
  data = [
    {
      key: "react",
      value: "React Native",
    },
    {
      key: "vue",
      value: "Vue Component",
    },
    // ...
  ];
  render() {
    return (
      <ReactSearchBox
        placeholder="Type Something..."
        value="ReactScript"
        data={this.data}
        callback={(record) => console.log(record)}
      />
    );
  }
}

4.所有可用的组件道具

/*
 * The placeholder text for the input box.
 */
placeholder: string;
/*
 * The name attribute for the input box.
 */
name?: string;
/*
 * An array of objects which acts as the source of data for the dropdown. This prop is required.
 */
data: { key: string; value: string }[];
/*
 * Configs to override default Fuse configs.
 */
fuseConfigs?: {};
/*
 * Focus on the input box once the component is mounted.
 */
autoFocus?: boolean;
/*
 * A function which acts as a callback when any record is selected. It is triggered once a dropdown item is clicked.
 */
onSelect: (record: Record) => void;
/*
 * A function which acts as a callback when the input is focussed.
 */
onFocus?: () => void;
/*
 * A function which acts as a callback when the input value is changed.
 */
onChange: (value: string) => void;
/*
 * Color of the text in the input box.
 */
inputFontColor?: string;
/*
 * Color of the border of the input box.
 */
inputBorderColor?: string;
/*
 * Size of the font of the input box.
 */
inputFontSize?: string;
/*
 * Height of the input box.
 */
inputHeight?: string;
/*
 * Background color of the input box.
 */
inputBackgroundColor?: string;
/*
 * Background color on hover of the dropdown list items.
 */
dropdownHoverColor?: string;
/*
 * Border color of the dropdown.
 */
dropdownBorderColor?: string;
/*
 * Clear the input value when any record is selected.
 */
clearOnSelect?: boolean;
/*
 * Icon to be rendered on the left of the input box.
 */
leftIcon?: ReactNode;
/*
 * The size of the icon (based on the leftIcon prop).
 */
iconBoxSize?: number | string;
/*
 * The type of the input.
 */
type?: string;

预览

The postAutocomplete Search Box For Reactappeared first onReactScript.

以上就是React 程序设计简单的轻量级自动完成搜索框应用的详细内容,更多关于React 轻量级自动搜索框的资料请关注脚本之家其它相关文章!

相关文章

  • 详解React 16 中的异常处理

    详解React 16 中的异常处理

    这篇文章主要介绍了详解React 16 中的异常处理的相关资料,React 16.x 版本中,引入了所谓 Error Boundary 的概念,从而保证了发生在 UI 层的错误不会连锁导致整个应用程序崩溃;未被任何异常边界捕获的异常可能会导致整个 React 组件树被卸载,需要的朋友可以参考下
    2017-07-07
  • ahooks useVirtualList 封装虚拟滚动列表

    ahooks useVirtualList 封装虚拟滚动列表

    这篇文章主要为大家介绍了ahooks useVirtualList 封装虚拟滚动列表详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09
  • 详解使用WebPack搭建React开发环境

    详解使用WebPack搭建React开发环境

    这篇文章主要介绍了详解使用WebPack搭建React开发环境,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-08-08
  • React中的useState和setState的执行机制详解

    React中的useState和setState的执行机制详解

    这篇文章主要介绍了React中的useState和setState的执行机制,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-03-03
  • React特征学习之Form格式示例详解

    React特征学习之Form格式示例详解

    这篇文章主要为大家介绍了React特征学习之Form格式示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09
  • React组件重构之嵌套+继承及高阶组件详解

    React组件重构之嵌套+继承及高阶组件详解

    这篇文章主要给大家介绍了关于React组件重构之嵌套+继承及高阶组件的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-07-07
  • React和Vue的props验证示例详解

    React和Vue的props验证示例详解

    这篇文章主要介绍了React和Vue的props验证,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-08-08
  • 详解在React项目中如何集成和使用web worker

    详解在React项目中如何集成和使用web worker

    在复杂的React应用中,某些计算密集型或耗时操作可能会阻塞主线程,导致用户界面出现卡顿或响应慢的现象,为了优化用户体验,可以采用Web Worker来在后台线程中执行这些操作,本文将详细介绍在React项目中如何集成和使用Web Worker来改善应用性能,需要的朋友可以参考下
    2023-12-12
  • React错误边界Error Boundaries详解

    React错误边界Error Boundaries详解

    错误边界是一种React组件,这种组件可以捕获发生在其子组件树任何位置的JavaScript错误,并打印这些错误,同时展示降级UI,而并不会渲染那些发生崩溃的子组件树
    2022-12-12
  • react-native组件中NavigatorIOS和ListView结合使用的方法

    react-native组件中NavigatorIOS和ListView结合使用的方法

    这篇文章主要给大家介绍了关于react-native组件中NavigatorIOS和ListView结合使用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
    2017-09-09

最新评论