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

feat: add multi-select capabillity to AutocompleteWithSuggestions #975

Merged
merged 15 commits into from
May 28, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion assets/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"dependencies": {
"@material-ui/core": "^4.11.2",
"@material-ui/icons": "^4.11.2",
"@wordpress/base-styles": "^1.6.0",
"@wordpress/base-styles": "^3.4.4",
"@wordpress/components": "^12.0.6",
"@wordpress/element": "^2.19.0",
"@wordpress/i18n": "^3.17.0",
Expand Down
29 changes: 23 additions & 6 deletions assets/components/src/autocomplete-tokenfield/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,21 @@ class AutocompleteTokenField extends Component {
* @return {Array} Array of valid values corresponding to the labels.
*/
getValuesForLabels( labels ) {
const { returnFullObjects } = this.props; // If this prop is passed, return both the value and label. Otherwise, return just the label.
const { validValues } = this.state;

if ( returnFullObjects ) {
return labels.reduce( ( acc, label ) => {
Object.keys( validValues ).forEach( key => {
if ( validValues[ key ] === label ) {
acc.push( { value: key, label } );
}
} );

return acc;
}, [] );
}

return labels.map( label =>
Object.keys( validValues ).find( key => validValues[ key ] === label )
);
Expand Down Expand Up @@ -120,15 +134,18 @@ class AutocompleteTokenField extends Component {
return;
}

const { validValues } = this.state;
const currentSuggestions = [];
const currentSuggestions = [ ...suggestions ];
const currentValidValues = {};

suggestions.forEach( suggestion => {
currentSuggestions.push( suggestion.label );
validValues[ suggestion.value ] = suggestion.label;
currentValidValues[ suggestion.value ] = suggestion.label;
} );

this.setState( { suggestions: currentSuggestions, validValues, loading: false } );
this.setState( {
suggestions: currentSuggestions,
validValues: currentValidValues,
loading: false,
} );
} )
.catch( () => {
if ( this.suggestionsRequest === request ) {
Expand Down Expand Up @@ -173,7 +190,7 @@ class AutocompleteTokenField extends Component {
<div className="newspack-autocomplete-tokenfield">
<FormTokenField
value={ this.getTokens() }
suggestions={ suggestions }
suggestions={ suggestions.map( suggestion => suggestion.label ) }
onChange={ tokens => this.handleOnChange( tokens ) }
onInputChange={ input => this.debouncedUpdateSuggestions( input ) }
label={ label }
Expand Down
Loading