Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Autocomplete] Add multiple downshift example #10550

Merged
merged 1 commit into from Mar 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -123,7 +123,6 @@ const styles = theme => ({
flexGrow: 1,
position: 'relative',
height: 250,
width: 200,
},
suggestionsContainerOpen: {
position: 'absolute',
Expand Down
170 changes: 139 additions & 31 deletions docs/src/pages/demos/autocomplete/IntegrationDownshift.js
@@ -1,10 +1,12 @@
import React from 'react';
import PropTypes from 'prop-types';
import keycode from 'keycode';
import Downshift from 'downshift';
import { withStyles } from 'material-ui/styles';
import TextField from 'material-ui/TextField';
import Paper from 'material-ui/Paper';
import { MenuItem } from 'material-ui/Menu';
import Downshift from 'downshift';
import Chip from 'material-ui/Chip';

const suggestions = [
{ label: 'Afghanistan' },
Expand Down Expand Up @@ -63,7 +65,8 @@ function renderInput(inputProps) {
function renderSuggestion(params) {
const { suggestion, index, itemProps, highlightedIndex, selectedItem } = params;
const isHighlighted = highlightedIndex === index;
const isSelected = selectedItem === suggestion.label;
const isSelected =
selectedItem === suggestion.label || selectedItem.indexOf(suggestion.label) > -1;

return (
<MenuItem
Expand Down Expand Up @@ -96,12 +99,111 @@ function getSuggestions(inputValue) {
});
}

class DownshiftMultiple extends React.Component {
state = {
inputValue: '',
selectedItem: [],
};

handleKeyDown = event => {
const { inputValue, selectedItem } = this.state;
if (selectedItem.length && !inputValue.length && keycode(event) === 'backspace') {
this.setState({
selectedItem: selectedItem.slice(0, selectedItem.length - 1),
});
}
};

handleInputChange = event => {
this.setState({ inputValue: event.target.value });
};

handleChange = item => {
let { selectedItem } = this.state;

if (selectedItem.indexOf(item) === -1) {
selectedItem = [...selectedItem, item];
}

this.setState({
inputValue: '',
selectedItem,
});
};

handleDelete = item => () => {
const selectedItem = [...this.state.selectedItem];
selectedItem.splice(selectedItem.indexOf(item), 1);

this.setState({ selectedItem });
};

render() {
const { classes } = this.props;
const { inputValue, selectedItem } = this.state;

return (
<Downshift inputValue={inputValue} onChange={this.handleChange} selectedItem={selectedItem}>
{({
getInputProps,
getItemProps,
isOpen,
inputValue: inputValue2,
selectedItem: selectedItem2,
highlightedIndex,
}) => (
<div className={classes.container}>
{renderInput({
fullWidth: true,
classes,
InputProps: getInputProps({
startAdornment: selectedItem.map(item => (
<Chip
key={item}
tabIndex={-1}
label={item}
className={classes.chip}
onDelete={this.handleDelete(item)}
/>
)),
onChange: this.handleInputChange,
onKeyDown: this.handleKeyDown,
placeholder: 'Select multiple countries',
id: 'integration-downshift-multiple',
}),
})}
{isOpen ? (
<Paper className={classes.paper} square>
{getSuggestions(inputValue2).map((suggestion, index) =>
renderSuggestion({
suggestion,
index,
itemProps: getItemProps({ item: suggestion.label }),
highlightedIndex,
selectedItem: selectedItem2,
}),
)}
</Paper>
) : null}
</div>
)}
</Downshift>
);
}
}

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

const styles = theme => ({
root: {
flexGrow: 1,
height: 250,
},
container: {
flexGrow: 1,
position: 'relative',
height: 250,
width: 200,
},
paper: {
position: 'absolute',
Expand All @@ -110,39 +212,45 @@ const styles = theme => ({
left: 0,
right: 0,
},
chip: {
margin: `${theme.spacing.unit}px ${theme.spacing.unit / 4}px`,
},
});

function IntegrationDownshift(props) {
const { classes } = props;

return (
<Downshift>
{({ getInputProps, getItemProps, isOpen, inputValue, selectedItem, highlightedIndex }) => (
<div className={classes.container}>
{renderInput({
fullWidth: true,
classes,
InputProps: getInputProps({
placeholder: 'Search a country (start with a)',
id: 'integration-downshift',
}),
})}
{isOpen ? (
<Paper className={classes.paper} square>
{getSuggestions(inputValue).map((suggestion, index) =>
renderSuggestion({
suggestion,
index,
itemProps: getItemProps({ item: suggestion.label }),
highlightedIndex,
selectedItem,
}),
)}
</Paper>
) : null}
</div>
)}
</Downshift>
<div className={classes.root}>
<Downshift>
{({ getInputProps, getItemProps, isOpen, inputValue, selectedItem, highlightedIndex }) => (
<div className={classes.container}>
{renderInput({
fullWidth: true,
classes,
InputProps: getInputProps({
placeholder: 'Search a country (start with a)',
id: 'integration-downshift-simple',
}),
})}
{isOpen ? (
<Paper className={classes.paper} square>
{getSuggestions(inputValue).map((suggestion, index) =>
renderSuggestion({
suggestion,
index,
itemProps: getItemProps({ item: suggestion.label }),
highlightedIndex,
selectedItem,
}),
)}
</Paper>
) : null}
</div>
)}
</Downshift>
<DownshiftMultiple classes={classes} />
</div>
);
}

Expand Down
5 changes: 2 additions & 3 deletions docs/src/pages/demos/autocomplete/IntegrationReactSelect.js
Expand Up @@ -123,7 +123,6 @@ const styles = theme => ({
root: {
flexGrow: 1,
height: 250,
width: 200,
},
chip: {
margin: theme.spacing.unit / 4,
Expand Down Expand Up @@ -259,7 +258,7 @@ class IntegrationReactSelect extends React.Component {
classes,
value: single,
onChange: this.handleChangeSingle,
placeholder: 'Select single-value…',
placeholder: 'Search a country (start with a)',
instanceId: 'react-select-single',
id: 'react-select-single',
name: 'react-select-single',
Expand All @@ -275,7 +274,7 @@ class IntegrationReactSelect extends React.Component {
value: multi,
multi: true,
onChange: this.handleChangeMulti,
placeholder: 'Select multi-value…',
placeholder: 'Select multiple countries',
instanceId: 'react-select-chip',
id: 'react-select-chip',
name: 'react-select-chip',
Expand Down