2.0.1 • Published 2 years ago

react-autocomplete-email v2.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
2 years ago

react-autocomplete-email

A configurable & lightweight React wrapper component that enables "out of the box" email autocomplete/suggestions on input elements.

✅ A wrapper component so you can use alongside other form enabled libraries (such as Buefy).\ ✅ Customizable.\ ✅ Allow users to easily navigate the suggestions list by simply using the "up/down" keys.\ ✅ Users can also auto-fill the input with the desired value by hitting the "enter" key upon selection.

Check out the Vue.js version here.

Installation

# yarn
yarn add react-autocomplete-email

# npm
npm install react-autocomplete-email --save

Basic Usage

/* Import useState and useRef from React and import package */

import React, { useState, useRef } from 'react';
import EmailAutoComplete from 'react-autocomplete-email';

function App() {

  /* Set state and ref */

  const [inputVal, setInputVal] = useState('')
  const emailAutoCompleteRef = useRef();

  return (
      
    <EmailAutoComplete ref={emailAutoCompleteRef} onCompletion={val => setInputVal(val)}>
      <input type="text" value={inputVal} onChange={(e) => {setInputVal(e.target.value); emailAutoCompleteRef.current.change(e)}} onKeyDown={(e) => emailAutoCompleteRef.current.check(e)} />
    </EmailAutoComplete>
    
  );

}

export default App;

Configuration Example (Custom Domain Lists)

/* A "domains" prop is added to the component and references the array of domains within the data property below */

/* Import useState and useRef from React and import package */

import React, { useState, useRef } from 'react';
import EmailAutoComplete from 'react-autocomplete-email';

function App() {

  /* Set state and ref */
  
  const [inputVal, setInputVal] = useState('')
  const emailAutoCompleteRef = useRef();

  /* Domain List */

  const customDomains = [
    
    "domain1.com",
    "domain2.com",
    "domain3.com",
    "domain4.com",

  ]

  return (

    <EmailAutoComplete ref={emailAutoCompleteRef} onCompletion={val => setInputVal(val)} domains={customDomains}>
      <input type="text" name="email" placeholder="Email..." value={inputVal} onChange={(e) => {setInputVal(e.target.value); emailAutoCompleteRef.current.change(e)}} onKeyDown={(e) => emailAutoCompleteRef.current.check(e)} />
    </EmailAutoComplete>
    
  );

}

export default App;

Configuration Example (On Submit Callbacks)

/* An "onSubmit" prop is added to the component and references a method below */

/* Import useState and useRef from React and import package */

import React, { useState, useRef } from 'react';
import EmailAutoComplete from 'react-autocomplete-email';

function App() {

  /* Set state and ref */

  const [inputVal, setInputVal] = useState('')
  const emailAutoCompleteRef = useRef();
  
  const validateForm = () => {
    
    /* Called when a user hits enter when focused on the wrapped input element */
    
  };

  return (
      
    <EmailAutoComplete ref={emailAutoCompleteRef} onCompletion={val => setInputVal(val)} onSubmit={() => validateForm()}>
      <input type="text" value={inputVal} onChange={(e) => {setInputVal(e.target.value); emailAutoCompleteRef.current.change(e)}} onKeyDown={(e) => emailAutoCompleteRef.current.check(e)} />
    </EmailAutoComplete>
    
  );

}

export default App;

Configuration Example (Custom Inline Styles)

/* A "css" prop is added to the component and references a computed property below */


/* Import useState and useRef from React and import package */

import React, { useState, useRef } from 'react';
import EmailAutoComplete from 'react-autocomplete-email';

function App() {

  /* Set state and ref */

  const [inputVal, setInputVal] = useState('')
  const emailAutoCompleteRef = useRef();
  
  const styleOverrides = {
  
    /* Edit style for the suggestions "outer" container */

    container: {
      position: 'fixed',
      top: '40px',
      left: '40px'
    },

    /* Edit style for the suggestions overlay */

    overlay: {
      backgroundColor: #FFF
    },

    /* Edit style for the suggestions text */

    text: {

      /* Main text */

      suggestion: {
        color: purple
      },

      /* Highlighted/matched text */

      highlight: {
        color: blue
      }
  };

  return (
      
    <EmailAutoComplete ref={emailAutoCompleteRef} onCompletion={val => setInputVal(val)} css={styleOverrides}>
      <input type="text" value={inputVal} onChange={(e) => {setInputVal(e.target.value); emailAutoCompleteRef.current.change(e)}} onKeyDown={(e) => emailAutoCompleteRef.current.check(e)} />
    </EmailAutoComplete>
    
  );

}

export default App;

Configuration Example (CSS stylesheet overriddes)

Coming Soon!

Props

:racing_car: Roadmap

  • Add extra CSS override mappings.
  • Add ability to override CSS with a stylesheet (enables usage with media queries).
  • Autocomplete default suggestions list to be based on browser language detection, which will make the suggestions more regionally relevant.

Contributions

If you'd like to contribute and add functionality to this project, feel free to fork this repo, create a new feature branch and then submit a pull request.