+ { sprintf( __( 'Or, select a recent %s:', 'newspack' ), postTypeLabel ) }
+
{ suggestions.map( renderSuggestion ) }
+ { suggestions.length < ( maxItemsToSuggest || maxSuggestions ) && (
+
+ ) }
+
+ { 0 < selectedItems.length && renderSelections() }
+ { 1 < postTypes.length && renderPostTypeSelect() }
fetchSavedPosts( postIDs ) }
+ tokens={ [] }
+ onChange={ handleOnChange }
+ fetchSuggestions={ async search => handleFetchSuggestions( search, 0, postTypeToSearch ) }
+ fetchSavedInfo={ postIds => handleFetchSaved( postIds ) }
label={ label }
- help={ help }
+ help={ ! hideHelp && help }
+ returnFullObjects
/>
- { 0 < suggestions.length && renderSuggestions() }
+ { renderSuggestions() }
);
};
diff --git a/assets/components/src/autocomplete-with-suggestions/style.scss b/assets/components/src/autocomplete-with-suggestions/style.scss
index 9c590a3f62..e0fcade489 100644
--- a/assets/components/src/autocomplete-with-suggestions/style.scss
+++ b/assets/components/src/autocomplete-with-suggestions/style.scss
@@ -8,12 +8,41 @@
.newspack-autocomplete-with-suggestions {
width: 100%;
+ &.hide-help {
+ > .components-base-control,
+ > .newspack-autocomplete-tokenfield,
+ > .newspack-autocomplete-with-suggestions__search-suggestions,
+ > .newspack-autocomplete-with-suggestions__search-suggestions-multiselect {
+ margin-top: 1.5rem;
+ }
+ }
+
.components-spinner {
- position: absolute;
right: 0;
top: 2em;
}
+ .components-select-control__input {
+ max-width: none;
+ }
+
+ .components-input-control__backdrop {
+ border-color: $gray-900 !important;
+ }
+
+ .newspack-autocomplete-with-suggestions__label,
+ .components-form-token-field__label,
+ .components-input-control__label,
+ .components-checkbox-control__label {
+ color: $gray-900 !important;
+ }
+
+ &__suggestions-spinner .components-spinner {
+ display: block;
+ margin: 0 auto 1rem;
+ position: relative;
+ }
+
.newspack-autocomplete-tokenfield__help {
font-size: inherit;
}
@@ -38,16 +67,53 @@
margin-bottom: 4px;
}
- &__search-suggestions {
+ &__selected-items {
+ border-bottom: 1px solid $gray-300;
+ margin-bottom: 2rem;
+ padding-bottom: 1.75rem;
+
+ .newspack-autocomplete-with-suggestions__label {
+ font-weight: bold;
+
+ .components-button.is-link {
+ color: $alert-red;
+ font-weight: normal;
+ }
+ }
+ }
+
+ &__selected-item-button.components-button.is-tertiary {
+ background-color: $gray-300;
border-radius: 2px;
- border: 1px solid $gray-900;
- max-height: 164px;
- overflow: scroll;
+ color: #1e1e1e;
+ font-size: 13px;
+ height: auto;
+ margin-bottom: 4px;
+ margin-right: 4px;
+ padding-left: 8px;
+ padding-right: 8px;
- .components-button {
+ &::after {
+ content: '✕';
+ margin-left: 8px;
+ }
+
+ &:hover {
+ box-shadow: none;
+ color: black;
+ }
+ }
+
+ &__search-suggestions,
+ &__search-suggestions-multiselect {
+ max-height: 166px;
+ overflow-x: visible;
+ overflow-y: auto;
+
+ .components-button.is-link {
color: $gray-900;
display: block;
- margin: 0;
+ margin: auto;
padding: 8px;
text-decoration: none;
width: 100%;
@@ -59,5 +125,23 @@
color: white;
}
}
+ .components-button.is-secondary {
+ display: block;
+ margin: 1rem auto;
+ }
+ }
+
+ /* Extra space on sides to handle outline styles of focused checkboxes. */
+ &__search-suggestions-multiselect {
+ margin-left: -4px;
+ margin-right: -4px;
+ padding: 0 4px;
+ }
+
+ &__search-suggestions {
+ border-radius: 2px;
+ border: 1px solid $gray-900;
+ max-height: 192px;
+ padding: 4px;
}
}
diff --git a/assets/wizards/componentsDemo/index.js b/assets/wizards/componentsDemo/index.js
index 3b7d582bf1..a5c8c72bf5 100644
--- a/assets/wizards/componentsDemo/index.js
+++ b/assets/wizards/componentsDemo/index.js
@@ -10,9 +10,6 @@ import '../../shared/js/public-path';
* WordPress dependencies.
*/
import { Component, Fragment, render } from '@wordpress/element';
-import apiFetch from '@wordpress/api-fetch';
-import { decodeEntities } from '@wordpress/html-entities';
-import { addQueryArgs } from '@wordpress/url';
import { __ } from '@wordpress/i18n';
import { audio, plus, reusableBlock, typography } from '@wordpress/icons';
@@ -49,6 +46,7 @@ class ComponentsDemo extends Component {
super( ...arguments );
this.state = {
selectedPostForAutocompleteWithSuggestions: [],
+ selectedPostsForAutocompleteWithSuggestionsMultiSelect: [],
inputTextValue1: 'Input value',
inputTextValue2: '',
inputNumValue: 0,
@@ -67,6 +65,7 @@ class ComponentsDemo extends Component {
render() {
const {
selectedPostForAutocompleteWithSuggestions,
+ selectedPostsForAutocompleteWithSuggestionsMultiSelect,
inputTextValue1,
inputTextValue2,
inputNumValue,
@@ -77,6 +76,7 @@ class ComponentsDemo extends Component {
toggleGroupChecked,
color1,
} = this.state;
+
return (
@@ -87,50 +87,37 @@ class ComponentsDemo extends Component {
- { __( 'Autocomplete with Suggestions', 'newspack' ) }
+ { __( 'Autocomplete with Suggestions (single-select)', 'newspack' ) }
{
- const posts = await apiFetch( {
- path: addQueryArgs( '/wp/v2/posts', {
- per_page: 100,
- include: postIDs.join( ',' ),
- _fields: 'id,title',
- } ),
- } );
-
- return posts.map( post => ( {
- value: post.id,
- label: decodeEntities( post.title ) || __( '(no title)', 'newspack' ),
- } ) );
- } }
- fetchSuggestions={ async search => {
- const posts = await apiFetch( {
- path: addQueryArgs( '/wp/v2/posts', {
- search,
- per_page: 10,
- _fields: 'id,title',
- } ),
- } );
+ onChange={ items =>
+ this.setState( { selectedPostForAutocompleteWithSuggestions: items } )
+ }
+ selectedItems={ selectedPostForAutocompleteWithSuggestions }
+ />
- // Format suggestions for FormTokenField display.
- return posts.reduce( ( acc, post ) => {
- acc.push( {
- value: post.id,
- label: decodeEntities( post.title.rendered ) || __( '(no title)', 'newspack' ),
- } );
+
- return acc;
- }, [] );
- } }
+ { __( 'Autocomplete with Suggestions (multi-select)', 'newspack' ) }
+
- this.setState( { selectedPostForAutocompleteWithSuggestions: items.pop() } )
+ this.setState( { selectedPostsForAutocompleteWithSuggestionsMultiSelect: items } )
}
- selectedPost={ selectedPostForAutocompleteWithSuggestions }
+ postTypes={ [ { slug: 'page', label: 'Pages' }, { slug: 'post', label: 'Posts' } ] }
+ postTypeLabel={ 'widget' }
+ postTypeLabelPlural={ 'widgets' }
+ selectedItems={ selectedPostsForAutocompleteWithSuggestionsMultiSelect }
/>