instantsearch-tagsbox-react

中文文档
1.2.1发布于 2018/11/28ESM + CJS
原始使用文档
文档目录11 节
  1. Demo
  2. Description
  3. Get the code
  4. JavaScript
  5. CSS
  6. Usage
  7. Requirements
  8. Examples
  9. Props
  10. Implementation details
  11. Contributing

TagsBox Widget

🎥 See it Live on CodeSandbox

Demo

Demo of TagsBox Widget

Description

This is the TagsBox Labs widget for React InstantSearch. You can use this widget to search and select tags with Algolia and use them later on for any purpose want.

This widget displays an input that will offer tag suggestions while user is typing. Tags can be then clicked to be added to the input.

This helps user to select records for another purpose than searching through them but to reuse them to complete another action later (like choosing a contact for sending an email or a category while creating a blog post for instance).

Table of Contents

Get the code

This widget comes with JavaScript but also pre-defined CSS.

JavaScript

You can copy and paste the JavaScript code from the repository itself, grab it in the src/lib folder and use the <Tag /> component.

CSS

You can copy and paste the necessary CSS code from the repository itself, grab it in src/lib/Tags.css.

Usage

Install it with yarn add instantsearch-tagsbox-react then, use it as simply as:

import { InstantSearch } from 'react-instantsearch-dom';
import TagsBox from 'instantsearch-tagsbox-react';

export const SelectedTagComponent = ({ hit }) => (
    <Fragment>
        {hit.lastname}
    </Fragment>
);

export const SuggestedTagComponent = ({ hit }) => (
    <Fragment>
        {hit.firstname} {hit.lastname}
    </Fragment>
);

<InstantSearch
    appId="..."
    apiKey="..."
    indexName="..."
>
    <TagsBox
        selectedTagComponent={SelectedTagComponent}
        suggestedTagComponent={SuggestedTagComponent}
        onUpdate={(newTags, previousTags) => console.log(newTags, previousTags)}
        translations={{ placeholder: "Search…", noResult: "…" }}
    />
</InstantSearch>

Requirements

You should encapsulate the <TagsBox /> component inside an <InstantSearch /> one.

To work properly, you'll need to provide three props to the component:

  • selectedTagComponent (component): describe how a selected tag should be displayed
    • receive a hit as parameter (coming from Algolia)
  • suggestedTagComponent (component): describe how a suggested tag should be displayed
    • receive a hit as parameter (coming from Algolia)
  • onUpdate (function): called each time a tag is added or removed
    • receive two parameters that represents the new tags and the previous tags. They are both arrays that respectively contains the current selected tags and the previous ones

It is possible to encapsulate the <TagBox /> component in an <Index /> one if you wish to target different indices from the same InstantSearch instance.

Examples

  • using the connectAutoComplete() connector: CodeSandbox

Props

NameTypeUse
selectedTagComponent (required)ComponentDescribe how a selected tag should be displayed.
suggestedTagComponent (required)ComponentDescribe how a suggested tag should be displayed.
NoResultComponentComponentDescribe how an no existing tag should be displayed (should be use with onAddTag prop).
onUpdate (required)FunctionCalled each time a tag is added or removed.
onAddTagFunctionCalled before a tag is added to manipulate the object. It takes either a hit (object) or a value (string) as parameter and should always return an object with a objectID value.
translationsObjectSet the placeholder and noResult translations.
limitToNumberRestrict the number of tags to select.

Implementation details

This widget is implemented using the connectAutoComplete() connector. To learn more about connectors, you can read the associated guide.

Contributing

To contribute to the project, clone this repository then run:

yarn
yarn start

版本与发布记录

instantsearch-tagsbox-react 共发布 1 个版本,首次发布于 2018/11/28;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.2.1latest2018/11/287 年前

关于 instantsearch-tagsbox-react 的常见问题

instantsearch-tagsbox-react 是什么?

🎥 See it Live on CodeSandbox

如何安装 instantsearch-tagsbox-react?

在项目目录运行 npm install instantsearch-tagsbox-react,或使用 pnpm add / yarn add / bun add。

instantsearch-tagsbox-react 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/instantsearch-tagsbox-react 是否存在。

instantsearch-tagsbox-react 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

instantsearch-tagsbox-react 可以商用吗?

instantsearch-tagsbox-react 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

instantsearch-tagsbox-react 还在维护吗?

最新版本 1.2.1 发布于 2018/11/28(7 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/11/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。