npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@rcb-plugins/input-validator

v0.1.1

Published

A lightweight plugin to validate user input with visual feedbacks in React ChatBotify!

Downloads

104

Readme

Table of Contents

Introduction

Input Validator is a plugin that adds user input validation to the React ChatBotify Core Library. By default, the core library does not ship with user input validation. This plugin relies on chatbot events to intercept & validate messages, as well as chatbot hooks to provide visual feedback to users. The demo gif above should give you a pretty good idea of what this plugin is capable of doing.

For support, join the plugin community on Discord to connect with other developers and get help.

Quickstart

The plugin is incredibly straightforward to use and is available on npm. Simply follow the steps below:

  1. Install the plugin with the following command within your project folder:

    npm install @rcb-plugins/input-validator
  2. Import the plugin:

    import InputValidator from "@rcb-plugins/input-validator";
  3. Initialize the plugin within the plugins prop of ChatBot:

    import ChatBot from "react-chatbotify";
    import InputValidator from "@rcb-plugins/input-validator";
    
    const MyComponent = () => {
      return (
        <ChatBot plugins=[InputValidator()]/>
      );
    };
  4. Add the validateInput attribute to the Block that requires validation:

    import ChatBot from "react-chatbotify";
    import InputValidator, { InputValidatorBlock } from "@rcb-plugins/input-validator";
    
    const MyComponent = () => {
      const flow = {
        start: {
          message: "What is your age?"
          validateInput: (userInput) => {
            if (isNaN(userInput)) {
              return {success: false, promptContent: "Age must be a number!", promptDuration: 3000}
            }
          }
        } as InputValidatorBlock
      }
    
      return (
        <ChatBot plugins=[InputValidator()]/>
      );
    };

The quickstart above shows how input validation can be done for age input (e.g. ensuring that age is a number). The documentation website for the React ChatBotify Core Library also contains a live input validation example that uses this plugin. You may wish to check it out!

Features

Input Validator is a lightweight plugin that provides the following features to your chatbot:

  • Validate user input submissions
  • Notify users with toasts/highlights upon successful/failed validations
  • Advanced styling options upon successful/failed validations
  • Auto setups with necessary events enabled out-of-the-box (plug & play!)

API Documentation

Plugin Configuration

The InputValidator plugin accepts a configuration object that allows you to customize its behavior and appearance. An example configuration is passed in below to initialize the plugin:

import ChatBot from "react-chatbotify";
import InputValidator from "@rcb-plugins/input-validator";

const MyComponent = () => {
  const pluginConfig = {
    // defaults to true, auto enable events required for plugin to work
    autoConfig: true,
    // base color schemes used for toasts, based on validation result
    promptBaseColors: {
      "error": "#dc3545",
		  "success": "#28a745",
    },
    // color schemes used when toasts are hovered, based on validation result
    promptHoveredColors: {
      "error": "#c82333",
      "success": "#218838",
    },
    // color schemes used for text area highlights, based on validation result
    textAreaHighlightColors: {
      "error": "#dc3545",
      "success": "#28a745",
	  }
    // advanced styles to reflect validation result in other parts of the chatbot
    advancedStyles: {
      error: {
        sendButtonStyle: { backgroundColor: "red" },
      },
    },
  }

  return (
    <ChatBot plugins={[InputValidator(pluginConfig)]}/>
  )
}

As you may be able to tell from above, there are 5 configurable sections within the plugin configuration which are autoConfig, promptBaseColors, promptHoveredColors, textAreaHighlightColors and advancedStyles. These are described in the table below:

| Configuration Option | Type | Default Value | Description | |------------------------------|----------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------| | autoConfig | boolean | true | Enables automatic configuration of required events for input validation. Recommended to keep as true. If set to false, you need to configure events manually. | | promptBaseColors | object | { "info": "#007bff", "warning": "#ffc107", "error": "#dc3545", "success": "#28a745" } | Maps prompt types to colors shown in toast notifications for validation results (success, error, etc.). Define custom prompt types and colors. | | promptHoveredColors | object | { "info": "#0056b3", "warning": "#d39e00", "error": "#c82333", "success": "#218838" } | Maps prompt types to colors shown when toast notifications are hovered over during validation. Define custom prompt types and colors. | | textAreaHighlightColors | object | { "info": "#007bff", "warning": "#ffc107", "error": "#dc3545", "success": "#28a745" } | Maps prompt types to colors shown in the text area according to validation results. Define custom prompt types and colors. | | advancedStyles | object | {} | Customizes styles for different validation results across the chatbot (not just toast notifications). Each key is a prompt type with corresponding style properties. |

Validating User Input

To validate user input, add the validateInput attribute to any Block that requires validation. The validateInput attribute is a function that receives the user's input and returns an object indicating the validation result. An example can be seen below:

import ChatBot from "react-chatbotify";
import InputValidator from "@rcb-plugins/input-validator";

const MyComponent = () => {
  const flow = {
    start: {
      message: "What is your age?",
      validateInput: (userInput) => {
        if (isNaN(userInput)) {
          return {
            success: false,
            promptContent: "Age must be a number!",
            promptDuration: 3000,
            promptType: 'error',
            highlightTextarea: true,
          };
        }
        return { success: true };
      },
    },
    // ... other blocks
  };

  return (
    <ChatBot plugins={[InputValidator(pluginConfig)]}/>
  )
}

As you can see from the example above, validateInput takes in a userInput parameter and returns an object representing the validation result. The validation result contains a total of 5 properties described in the table below:

| Property | Type | Default Value | Description | |---------------------|----------|----------|---------------------------------------------------------------------------------------------------------------------------------------------| | success | boolean | false | If true, validation passed and user input is allowed to be sent. If false, user input is blocked. | | promptContent | string | "" | The message displayed to the user if validation fails (e.g., "Age must be a number!"). | | promptDuration | number | 3000 | The duration (in milliseconds) that the prompt message is shown. | | promptType | string | "info" | Defines the type of prompt to display (e.g., "error", "warning", etc.), which influences styling and colors set in plugin configurations. | | highlightTextArea | boolean | true | If set to true, highlights the input text area according to validation result, providing more visual feedback. |

Note that all above properties have default values assigned to them. This means that if the validateInput attribute does not return an expected object, validation fails by default since success would be false.

Team

Contributing

If you have code to contribute to the project, open a pull request from your fork and describe clearly the changes and what they are intended to do (enhancement, bug fixes etc). Alternatively, you may simply raise bugs or suggestions by opening an issue.

Others

For any questions regarding the project, please reach out for support via discord.