Created
December 18, 2019 16:28
-
-
Save nelsson/fe0f63d908ce5d24804c5ef4fd8e058b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React from 'react' | |
| import makeStyles from '@material-ui/styles/makeStyles' | |
| import Typography from '@material-ui/core/Typography' | |
| import { c, f } from '@bah/ui/src/utils/theme' | |
| import Autosuggest from 'react-autosuggest' | |
| import AutosuggestHighlightMatch from 'autosuggest-highlight/match' | |
| import AutosuggestHighlightParse from 'autosuggest-highlight/parse' | |
| const people = [ | |
| { | |
| first: 'Charlie', | |
| last: 'Brown', | |
| twitter: 'dancounsell' | |
| }, | |
| { | |
| first: 'Charlotte', | |
| last: 'White', | |
| twitter: 'mtnmissy' | |
| }, | |
| { | |
| first: 'Chloe', | |
| last: 'Jones', | |
| twitter: 'ladylexy' | |
| }, | |
| { | |
| first: 'Cooper', | |
| last: 'King', | |
| twitter: 'steveodom' | |
| } | |
| ] | |
| // https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions#Using_Special_Characters | |
| function escapeRegexCharacters(str) { | |
| return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') | |
| } | |
| function getSuggestions(value) { | |
| const escapedValue = escapeRegexCharacters(value.trim()) | |
| if (escapedValue === '') { | |
| return [] | |
| } | |
| const regex = new RegExp('\\b' + escapedValue, 'i') | |
| return people.filter(person => regex.test(getSuggestionValue(person))) | |
| } | |
| function getSuggestionValue(suggestion) { | |
| return `${suggestion.first} ${suggestion.last}` | |
| } | |
| function renderSuggestion(suggestion, { query }) { | |
| const suggestionText = `${suggestion.first} ${suggestion.last}` | |
| const matches = AutosuggestHighlightMatch(suggestionText, query) | |
| const parts = AutosuggestHighlightParse(suggestionText, matches) | |
| return ( | |
| <span className={'suggestion-content ' + suggestion.twitter}> | |
| <span className="name"> | |
| {parts.map((part, index) => { | |
| const className = part.highlight ? 'highlight' : null | |
| return ( | |
| <span className={className} key={index}> | |
| {part.text} | |
| </span> | |
| ) | |
| })} | |
| </span> | |
| </span> | |
| ) | |
| } | |
| class SearchExistingUser extends React.Component { | |
| constructor() { | |
| super() | |
| this.state = { | |
| value: '', | |
| suggestions: [] | |
| } | |
| } | |
| onChange = (event, { newValue, method }) => { | |
| this.setState({ | |
| value: newValue | |
| }) | |
| } | |
| onSuggestionsFetchRequested = ({ value }) => { | |
| this.setState({ | |
| suggestions: getSuggestions(value) | |
| }) | |
| } | |
| onSuggestionsClearRequested = () => { | |
| this.setState({ | |
| suggestions: [] | |
| }) | |
| } | |
| render() { | |
| const { value, suggestions } = this.state | |
| const inputProps = { | |
| placeholder: "Type 'c'", | |
| value, | |
| onChange: this.onChange | |
| } | |
| return ( | |
| <Autosuggest | |
| suggestions={suggestions} | |
| onSuggestionsFetchRequested={this.onSuggestionsFetchRequested} | |
| onSuggestionsClearRequested={this.onSuggestionsClearRequested} | |
| getSuggestionValue={getSuggestionValue} | |
| renderSuggestion={renderSuggestion} | |
| inputProps={inputProps} | |
| /> | |
| ) | |
| } | |
| } | |
| export default SearchExistingUser |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment