react-autocomplete-email
A configurable & lightweight React wrapper component that enables "out of the box" email autocomplete/suggestions on input elements.
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
Prop | Type | Optional | Default | Description |
---|---|---|---|---|
domains | Array | No | A selection of the most statistically popular email domain extensions. | A customized list of email domain extensions. |
onCompletion | Function | Yes | N/A | A function that you'd like the component to invoke after the user has selected a suggested completion (e.g. update the value/model of the input element). |
onSubmit | Function | No | N/A | A function that you'd like the component to invoke once the user hits the "enter" key when the nested input is in focus (e.g. carry out validations or submit the form). |
css | Object | No | N/A | CSS style overrides for specific elements of the suggestions component (See above examples). |
🏎️ 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.