From 881d816eab353b8c24cbbd29779743a71795cfdb Mon Sep 17 00:00:00 2001 From: Adam Borowski Date: Fri, 14 May 2021 11:15:00 +0200 Subject: [PATCH] feat: add AutocompleteWithSuggestions to components demo --- assets/wizards/componentsDemo/index.js | 52 ++++++++++++++++++++++ includes/wizards/class-components-demo.php | 2 +- 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/assets/wizards/componentsDemo/index.js b/assets/wizards/componentsDemo/index.js index 715d34dfbc..d95a074b7a 100644 --- a/assets/wizards/componentsDemo/index.js +++ b/assets/wizards/componentsDemo/index.js @@ -10,6 +10,9 @@ 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'; /** @@ -33,6 +36,7 @@ import { Modal, ToggleGroup, WebPreview, + AutocompleteWithSuggestions, } from '../../components/src'; class ComponentsDemo extends Component { @@ -42,6 +46,7 @@ class ComponentsDemo extends Component { constructor() { super( ...arguments ); this.state = { + selectedPostForAutocompleteWithSuggestions: [], inputTextValue1: 'Input value', inputTextValue2: '', inputNumValue: 0, @@ -59,6 +64,7 @@ class ComponentsDemo extends Component { */ render() { const { + selectedPostForAutocompleteWithSuggestions, inputTextValue1, inputTextValue2, inputNumValue, @@ -78,6 +84,52 @@ class ComponentsDemo extends Component {
+ + { + 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', + } ), + } ); + + // 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; + }, [] ); + } } + onChange={ value => + this.setState( { selectedPostForAutocompleteWithSuggestions: parseInt( value ) } ) + } + selectedPost={ selectedPostForAutocompleteWithSuggestions } + /> +

{ __( 'Plugin toggles' ) }

get_script_dependencies(), + $this->get_script_dependencies( [ 'wp-html-entities' ] ), filemtime( dirname( NEWSPACK_PLUGIN_FILE ) . '/dist/componentsDemo.js' ), true );