Skip to content

Commit

Permalink
[Autocomplete] Show an integration example (#7477)
Browse files Browse the repository at this point in the history
* [Autocomplete] Show an integration example

* Update IntegrationAutosuggest.js
  • Loading branch information
oliviertassinari authored Jul 20, 2017
1 parent 7751a63 commit c2c4a72
Show file tree
Hide file tree
Showing 4 changed files with 265 additions and 11 deletions.
2 changes: 2 additions & 0 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@
"webpack-dev-server": "^2.5.1"
},
"dependencies": {
"autosuggest-highlight": "^3.1.0",
"marked": "^0.3.6",
"prismjs": "^1.6.0",
"react-autosuggest": "^9.3.1",
"react-redux": "^5.0.5",
"react-router": "^3.0.5",
"react-router-scroll": "^0.4.2",
Expand Down
208 changes: 208 additions & 0 deletions docs/src/pages/component-demos/autocomplete/IntegrationAutosuggest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
// @flow
/* eslint-disable react/no-array-index-key */

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import Autosuggest from 'react-autosuggest';
import TextField from 'material-ui/TextField';
import Paper from 'material-ui/Paper';
import { MenuItem } from 'material-ui/Menu';
import match from 'autosuggest-highlight/match';
import parse from 'autosuggest-highlight/parse';
import { withStyles, createStyleSheet } from 'material-ui/styles';

const suggestions = [
{ label: 'Afghanistan' },
{ label: 'Aland Islands' },
{ label: 'Albania' },
{ label: 'Algeria' },
{ label: 'American Samoa' },
{ label: 'Andorra' },
{ label: 'Angola' },
{ label: 'Anguilla' },
{ label: 'Antarctica' },
{ label: 'Antigua and Barbuda' },
{ label: 'Argentina' },
{ label: 'Armenia' },
{ label: 'Aruba' },
{ label: 'Australia' },
{ label: 'Austria' },
{ label: 'Azerbaijan' },
{ label: 'Bahamas' },
{ label: 'Bahrain' },
{ label: 'Bangladesh' },
{ label: 'Barbados' },
{ label: 'Belarus' },
{ label: 'Belgium' },
{ label: 'Belize' },
{ label: 'Benin' },
{ label: 'Bermuda' },
{ label: 'Bhutan' },
{ label: 'Bolivia, Plurinational State of' },
{ label: 'Bonaire, Sint Eustatius and Saba' },
{ label: 'Bosnia and Herzegovina' },
{ label: 'Botswana' },
{ label: 'Bouvet Island' },
{ label: 'Brazil' },
{ label: 'British Indian Ocean Territory' },
{ label: 'Brunei Darussalam' },
];

function renderInput(inputProps) {
const { classes, home, value, ref, ...other } = inputProps;

return (
<TextField
autoFocus={home}
className={classes.textField}
value={value}
inputRef={ref}
InputProps={{
classes: {
input: classes.input,
},
...other,
}}
/>
);
}

function renderSuggestion(suggestion, { query, isHighlighted }) {
const matches = match(suggestion.label, query);
const parts = parse(suggestion.label, matches);

return (
<MenuItem selected={isHighlighted} component="div">
<div>
{parts.map((part, index) => {
return part.highlight
? <span key={index} style={{ fontWeight: 300 }}>
{part.text}
</span>
: <strong key={index} style={{ fontWeight: 500 }}>
{part.text}
</strong>;
})}
</div>
</MenuItem>
);
}

function renderSuggestionsContainer(options) {
const { containerProps, children } = options;

return (
<Paper {...containerProps} square>
{children}
</Paper>
);
}

function getSuggestionValue(suggestion) {
return suggestion.label;
}

function getSuggestions(value) {
const inputValue = value.trim().toLowerCase();
const inputLength = inputValue.length;
let count = 0;

return inputLength === 0
? []
: suggestions.filter(suggestion => {
const keep =
count < 5 && suggestion.label.toLowerCase().slice(0, inputLength) === inputValue;

if (keep) {
count += 1;
}

return keep;
});
}

const styleSheet = createStyleSheet('IntegrationAutosuggest', theme => ({
container: {
flexGrow: 1,
position: 'relative',
height: 200,
},
suggestionsContainerOpen: {
position: 'absolute',
marginTop: theme.spacing.unit,
marginBottom: theme.spacing.unit * 3,
left: 0,
right: 0,
},
suggestion: {
display: 'block',
},
suggestionsList: {
margin: 0,
padding: 0,
listStyleType: 'none',
},
textField: {
width: '100%',
},
}));

class IntegrationAutosuggest extends Component {
state = {
value: '',
suggestions: [],
};

handleSuggestionsFetchRequested = ({ value }) => {
this.setState({
suggestions: getSuggestions(value),
});
};

handleSuggestionsClearRequested = () => {
this.setState({
suggestions: [],
});
};

handleChange = (event, { newValue }) => {
this.setState({
value: newValue,
});
};

render() {
const { classes } = this.props;

return (
<Autosuggest
theme={{
container: classes.container,
suggestionsContainerOpen: classes.suggestionsContainerOpen,
suggestionsList: classes.suggestionsList,
suggestion: classes.suggestion,
}}
renderInputComponent={renderInput}
suggestions={this.state.suggestions}
onSuggestionsFetchRequested={this.handleSuggestionsFetchRequested}
onSuggestionsClearRequested={this.handleSuggestionsClearRequested}
renderSuggestionsContainer={renderSuggestionsContainer}
getSuggestionValue={getSuggestionValue}
renderSuggestion={renderSuggestion}
inputProps={{
autoFocus: true,
classes,
placeholder: 'Search a country (start with a)',
value: this.state.value,
onChange: this.handleChange,
}}
/>
);
}
}

IntegrationAutosuggest.propTypes = {
classes: PropTypes.object.isRequired,
};

export default withStyles(styleSheet)(IntegrationAutosuggest);
9 changes: 9 additions & 0 deletions docs/src/pages/component-demos/autocomplete/autocomplete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Autocomplete

The autocomplete is a normal text input enhanced by a panel of suggested options.

## react-autosuggest

In the following example, we demonstrate how to use [react-autosuggest](https://github.com/moroshko/react-autosuggest).

{{demo='pages/component-demos/autocomplete/IntegrationAutosuggest.js'}}
57 changes: 46 additions & 11 deletions docs/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ autoprefixer@^6.3.1:
postcss "^5.2.16"
postcss-value-parser "^3.2.3"

autosuggest-highlight@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/autosuggest-highlight/-/autosuggest-highlight-3.1.0.tgz#d5df5115b4840af49f2370a8fb88e5ce9b461ef9"
dependencies:
diacritic "0.0.2"

aws-sign2@~0.6.0:
version "0.6.0"
resolved "https://registry.yarnpkg.com/aws-sign2/-/aws-sign2-0.6.0.tgz#14342dd38dbcc94d0e5b87d763cd63612c0e794f"
Expand Down Expand Up @@ -1018,6 +1024,10 @@ detect-indent@^4.0.0:
dependencies:
repeating "^2.0.0"

diacritic@0.0.2:
version "0.0.2"
resolved "https://registry.yarnpkg.com/diacritic/-/diacritic-0.0.2.tgz#fc2a887b5a5bc0a0a854fb614c7c2f209061ee04"

diffie-hellman@^5.0.0:
version "5.0.2"
resolved "https://registry.yarnpkg.com/diffie-hellman/-/diffie-hellman-5.0.2.tgz#b5835739270cfe26acf632099fded2a07f209e5e"
Expand Down Expand Up @@ -2361,6 +2371,10 @@ oauth-sign@~0.8.1:
version "0.8.2"
resolved "https://registry.yarnpkg.com/oauth-sign/-/oauth-sign-0.8.2.tgz#46a6ab7f0aead8deae9ec0565780b7d4efeb9d43"

object-assign@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-3.0.0.tgz#9bedd5ca0897949bca47e7ff408062d549f587f2"

object-assign@^4.0.1, object-assign@^4.1.0, object-assign@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
Expand Down Expand Up @@ -2843,7 +2857,7 @@ promise@^7.1.1:
dependencies:
asap "~2.0.3"

prop-types@^15.5.10, prop-types@^15.5.4, prop-types@^15.5.6:
prop-types@^15.5.10, prop-types@^15.5.4, prop-types@^15.5.6, prop-types@^15.5.8:
version "15.5.10"
resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.5.10.tgz#2797dfc3126182e3a95e3dfbb2e893ddd7456154"
dependencies:
Expand Down Expand Up @@ -2938,6 +2952,22 @@ rc@^1.1.7:
minimist "^1.2.0"
strip-json-comments "~2.0.1"

react-autosuggest@^9.3.1:
version "9.3.1"
resolved "https://registry.yarnpkg.com/react-autosuggest/-/react-autosuggest-9.3.1.tgz#cf106d95e650de292a08e6506372d3eb4b6b2614"
dependencies:
prop-types "^15.5.10"
react-autowhatever "^10.1.0"
shallow-equal "^1.0.0"

react-autowhatever@^10.1.0:
version "10.1.0"
resolved "https://registry.yarnpkg.com/react-autowhatever/-/react-autowhatever-10.1.0.tgz#41f6d69382437d3447a0a3c8913bb8ca2feaabc1"
dependencies:
prop-types "^15.5.8"
react-themeable "^1.1.0"
section-iterator "^2.0.0"

react-deep-force-update@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/react-deep-force-update/-/react-deep-force-update-2.0.1.tgz#4f7f6c12c3e7de42f345992a3c518236fa1ecad3"
Expand Down Expand Up @@ -3029,6 +3059,12 @@ react-swipeable-views@^0.12.4:
react-swipeable-views-utils "^0.12.0"
warning "^3.0.0"

react-themeable@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/react-themeable/-/react-themeable-1.1.0.tgz#7d4466dd9b2b5fa75058727825e9f152ba379a0e"
dependencies:
object-assign "^3.0.0"

read-pkg-up@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/read-pkg-up/-/read-pkg-up-1.0.1.tgz#9d63c13276c065918d57f002a57f40a1b643fb02"
Expand Down Expand Up @@ -3065,16 +3101,7 @@ readdirp@^2.0.0:
readable-stream "^2.0.2"
set-immediate-shim "^1.0.1"

redbox-react@^1.2.5:
version "1.4.2"
resolved "https://registry.yarnpkg.com/redbox-react/-/redbox-react-1.4.2.tgz#7fe35d3c567301e97938cc7fd6a10918f424c6b4"
dependencies:
error-stack-parser "^1.3.6"
object-assign "^4.0.1"
prop-types "^15.5.4"
sourcemapped-stacktrace "^1.1.6"

redbox-react@^1.4.3:
redbox-react@^1.2.5, redbox-react@^1.4.3:
version "1.4.3"
resolved "https://registry.yarnpkg.com/redbox-react/-/redbox-react-1.4.3.tgz#51744987867ea4627d58ccb1b0e5df5a5ae40e57"
dependencies:
Expand Down Expand Up @@ -3240,6 +3267,10 @@ scroll-behavior@^0.9.3:
dom-helpers "^3.0.0"
invariant "^2.2.1"

section-iterator@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/section-iterator/-/section-iterator-2.0.0.tgz#bf444d7afeeb94ad43c39ad2fb26151627ccba2a"

select-hose@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/select-hose/-/select-hose-2.0.0.tgz#625d8658f865af43ec962bfc376a37359a4994ca"
Expand Down Expand Up @@ -3323,6 +3354,10 @@ sha.js@^2.3.6:
dependencies:
inherits "^2.0.1"

shallow-equal@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/shallow-equal/-/shallow-equal-1.0.0.tgz#508d1838b3de590ab8757b011b25e430900945f7"

shebang-command@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/shebang-command/-/shebang-command-1.2.0.tgz#44aac65b695b03398968c39f363fee5deafdf1ea"
Expand Down

0 comments on commit c2c4a72

Please sign in to comment.