-
Notifications
You must be signed in to change notification settings - Fork 586
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add alwaysRenderSuggestions prop (#155)
closes #133
- Loading branch information
1 parent
3309cd3
commit 6ff95ae
Showing
9 changed files
with
414 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
143 changes: 143 additions & 0 deletions
143
demo/src/components/App/components/Examples/components/AlwaysOpen/AlwaysOpen.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import styles from './AlwaysOpen.less'; | ||
|
||
import React, { Component } from 'react'; | ||
import isMobile from 'ismobilejs'; | ||
import Autosuggest from 'AutosuggestContainer'; | ||
import languages from './languages'; | ||
import { escapeRegexCharacters } from 'utils/utils'; | ||
|
||
const focusInputOnSuggestionClick = !isMobile.any; | ||
|
||
function getSuggestions(value) { | ||
const escapedValue = escapeRegexCharacters(value.trim()); | ||
|
||
if (escapedValue === '') { | ||
return languages; | ||
} | ||
|
||
const regex = new RegExp('^' + escapedValue, 'i'); | ||
|
||
return languages.filter(language => regex.test(language.name)); | ||
} | ||
|
||
function getSuggestionValue(suggestion) { | ||
return suggestion.name; | ||
} | ||
|
||
function renderSuggestion(suggestion) { | ||
return ( | ||
<span>{suggestion.name}</span> | ||
); | ||
} | ||
|
||
export default class AlwaysOpen extends Component { | ||
constructor() { | ||
super(); | ||
|
||
this.state = { | ||
isOpen: false, | ||
value: '', | ||
suggestions: getSuggestions('') | ||
}; | ||
|
||
this.onChange = this.onChange.bind(this); | ||
this.onSuggestionsUpdateRequested = this.onSuggestionsUpdateRequested.bind(this); | ||
this.onSuggestionSelected = this.onSuggestionSelected.bind(this); | ||
this.onOpenModal = this.onToggleModal.bind(this, true); | ||
this.onCloseModal = this.onToggleModal.bind(this, false); | ||
this.onClickOverlay = this.onClickOverlay.bind(this); | ||
} | ||
|
||
onToggleModal(isOpen = !this.state.isOpen) { | ||
this.setState({ | ||
isOpen: isOpen | ||
}); | ||
|
||
if (isOpen) { | ||
document.body.style.overflow = 'hidden'; | ||
} else { | ||
document.body.style.overflow = ''; | ||
} | ||
} | ||
|
||
onClickOverlay(e) { | ||
const isOverlay = e.target.getAttribute('data-overlay'); | ||
|
||
if (isOverlay) { | ||
this.onCloseModal(); | ||
e.preventDefault(); | ||
} | ||
} | ||
|
||
onChange(event, { newValue }) { | ||
this.setState({ | ||
value: newValue | ||
}); | ||
} | ||
|
||
onSuggestionsUpdateRequested({ value }) { | ||
this.setState({ | ||
suggestions: getSuggestions(value) | ||
}); | ||
} | ||
|
||
onSuggestionSelected(e, { suggestionValue }) { | ||
this.setState({ | ||
value: suggestionValue, | ||
suggestions: getSuggestions(suggestionValue) | ||
}); | ||
|
||
this.onCloseModal(); | ||
} | ||
|
||
render() { | ||
const { isOpen, value, suggestions } = this.state; | ||
const inputProps = { | ||
placeholder: 'Type \'c\'', | ||
value, | ||
onChange: this.onChange | ||
}; | ||
|
||
return ( | ||
<div id="basic-example" className={styles.container}> | ||
<div className={styles.textContainer}> | ||
<div className={styles.title}> | ||
Always open | ||
</div> | ||
<div className={styles.description}> | ||
Propose default suggestions even if the user hasn’t typed yet | ||
</div> | ||
</div> | ||
<div className={styles.autosuggest}> | ||
<a href="javascript:void(0)" onClick={this.onOpenModal} className={styles.button}>View suggestions</a> | ||
{isOpen ? ( | ||
<div className={styles.overlay} data-overlay={true} onClick={this.onClickOverlay}> | ||
<div className={styles.modal}> | ||
<Autosuggest suggestions={suggestions} | ||
onSuggestionsUpdateRequested={this.onSuggestionsUpdateRequested} | ||
onSuggestionSelected={this.onSuggestionSelected} | ||
getSuggestionValue={getSuggestionValue} | ||
renderSuggestion={renderSuggestion} | ||
inputProps={inputProps} | ||
alwaysRenderSuggestions={true} | ||
focusInputOnSuggestionClick={focusInputOnSuggestionClick} | ||
id="alwaysopen-example" | ||
theme={{ | ||
container: 'react-autosuggest__container', | ||
containerOpen: 'react-autosuggest__container--open', | ||
input: 'react-autosuggest__input', | ||
suggestionsContainer: `react-autosuggest__suggestions-container ${styles.suggestionsContainer}`, | ||
suggestion: 'react-autosuggest__suggestion', | ||
suggestionFocused: 'react-autosuggest__suggestion--focused', | ||
sectionContainer: 'react-autosuggest__section-container', | ||
sectionTitle: 'react-autosuggest__section-title', | ||
sectionSuggestionsContainer: 'react-autosuggest__section-suggestions-container' | ||
}} /> | ||
</div> | ||
</div> | ||
) : null} | ||
</div> | ||
</div> | ||
); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
demo/src/components/App/components/Examples/components/AlwaysOpen/AlwaysOpen.less
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
@import '~variables'; | ||
|
||
.button { | ||
color: #209FD3; | ||
text-decoration: none; | ||
} | ||
|
||
.suggestionsContainer { | ||
position: static; | ||
max-height: 300px; | ||
overflow-y: scroll; | ||
} | ||
|
||
.overlay { | ||
position: fixed; | ||
top: 0; | ||
left: 0; | ||
bottom: 0; | ||
right: 0; | ||
z-index: 100; | ||
background-color: rgba(0, 0, 0, 0.7); | ||
} | ||
|
||
.modal { | ||
position: absolute; | ||
top: 10vh; | ||
left: 50%; | ||
transform: translateX(-50%); | ||
z-index: 101; | ||
} | ||
|
||
.container { | ||
display: flex; | ||
justify-content: space-between; | ||
width: 34 * @columns; | ||
margin: (11 * @rows) (0.5 * @column) 0; | ||
|
||
@media @examples-vertical { | ||
flex-direction: column; | ||
width: 14 * @columns; | ||
margin-top: 9 * @rows; | ||
} | ||
|
||
@media @small { | ||
margin-top: 7 * @rows; | ||
} | ||
} | ||
|
||
.textContainer { | ||
display: flex; | ||
flex-direction: column; | ||
width: 15 * @columns; | ||
} | ||
|
||
.title { | ||
font-size: 30px; | ||
font-weight: 400; | ||
line-height: 5 * @rows; | ||
|
||
@media @small { | ||
font-size: 25px; | ||
} | ||
} | ||
|
||
.description { | ||
margin-top: @row; | ||
font-size: 20px; | ||
} | ||
|
||
.codepenLink { | ||
margin-top: @row; | ||
font-size: 20px; | ||
color: #209FD3; | ||
} | ||
|
||
.autosuggest { | ||
margin-top: 6 * @rows; | ||
|
||
@media @examples-vertical { | ||
margin-top: 3 * @rows; | ||
margin-left: 0; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
demo/src/components/App/components/Examples/components/AlwaysOpen/languages.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
export default [ | ||
{ | ||
name: 'C', | ||
year: 1972 | ||
}, | ||
{ | ||
name: 'C#', | ||
year: 2000 | ||
}, | ||
{ | ||
name: 'C++', | ||
year: 1983 | ||
}, | ||
{ | ||
name: 'Clojure', | ||
year: 2007 | ||
}, | ||
{ | ||
name: 'Elm', | ||
year: 2012 | ||
}, | ||
{ | ||
name: 'Go', | ||
year: 2009 | ||
}, | ||
{ | ||
name: 'Haskell', | ||
year: 1990 | ||
}, | ||
{ | ||
name: 'Java', | ||
year: 1995 | ||
}, | ||
{ | ||
name: 'Javascript', | ||
year: 1995 | ||
}, | ||
{ | ||
name: 'Perl', | ||
year: 1987 | ||
}, | ||
{ | ||
name: 'PHP', | ||
year: 1995 | ||
}, | ||
{ | ||
name: 'Python', | ||
year: 1991 | ||
}, | ||
{ | ||
name: 'Ruby', | ||
year: 1995 | ||
}, | ||
{ | ||
name: 'Scala', | ||
year: 2003 | ||
} | ||
]; |
This file contains 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
Oops, something went wrong.