-
Notifications
You must be signed in to change notification settings - Fork 144
Make Search
accept sync autocompleter.options.
#6884
Conversation
33e3389
to
535c73b
Compare
to let CI wait longer for options to be rendered.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pre-approving, but asking for some small tweaks to the test.
I had to modify the config slightly to test - our autocompleters do deviate from Gutenberg in more ways like:
woocommerce-admin/packages/components/src/search/autocompleters/attributes.js
Lines 161 to 169 in 1ae8188
// This is slightly different than gutenberg/Autocomplete, we don't support different methods | |
// of replace/insertion, so we can just return the value. | |
getOptionCompletion( attribute ) { | |
const value = { | |
key: attribute.id, | |
label: attribute.name, | |
}; | |
return value; | |
}, |
The shape of the options are:
woocommerce-admin/packages/components/src/search/autocompleters/attributes.js
Lines 136 to 140 in 1ae8188
const nameOption = { | |
key: 'name', | |
label, | |
value: { id: query, name: query }, | |
}; |
readme.txt
Outdated
@@ -314,12 +315,12 @@ Release and roadmap notes are available on the [WooCommerce Developers Blog](htt | |||
- Dev: Revert work done in #4857 for automated shipping after OBW is completed #5971 | |||
- Add: Welcome modal when coming from Calypso #6004 | |||
- Enhancement: Add an a/b experiment for installing free business features #5786 | |||
- Dev: Add `onChangeCallback` feature to the wc-admin <Form> component #5786 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we revert all these newline changes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Phew... resolving newline conflicts from revert after your merge main
, was the hardest part of this PR ;)
// Emulate typing to render available options. | ||
userEvent.type( getByRole( 'combobox' ), 'A' ); | ||
// Wait for async options procesing. | ||
await delay( 1000 ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than use a delay, you can use waitFor
:
userEvent.type( getByRole( 'combobox' ), query ); | |
// Wait for the search promise to resolve. | |
await waitFor( () => | |
expect( | |
getByRole( 'option', { name: 'lorem 1' } ) | |
).toBeInTheDocument() | |
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, TIL.
from "Add a changelog entry." This reverts commit 535c73b.
to speed up `Search`es tests. Addresses: #6884 (comment).
to make debugging easier.
@jeffstieler, Also, do you mean by this comment, I should change something in this test's |
@tomalec here's a sample I used for testing (working off the fruit example from Gutenberg): {
label: __( 'Fruit', 'woocommerce-admin' ),
value: 'select_fruit',
chartMode: 'item-comparison',
subFilters: [
{
component: 'Search',
value: 'single_fruit',
chartMode: 'item-comparison',
path: [ 'select_fruit' ],
settings: {
type: 'custom',
param: 'fruit',
getLabels: ( fruitId ) => {
return Promise.resolve(
fruits
.filter( ( { id } ) => id == fruitId )
.map( ( { id, name } ) => ( {
key: id,
label: name,
} ) )
);
},
labels: {
placeholder: __(
'Type to search for a fruit',
'woocommerce-admin'
),
button: __( 'Fruit', 'woocommerce-admin' ),
},
autocompleter: {
// The option data
options: fruits,
getOptionIdentifier: ( fruit ) => {
return fruit.id;
},
// Returns a label for an option like "🍊 Orange"
getOptionLabel: ( option ) => (
<span>
<span className="icon">
{ option.visual }
</span>
{ option.name }
</span>
),
// Declares that options should be matched by their name
getOptionKeywords: ( option ) => [ option.name ],
// This is slightly different than gutenberg/Autocomplete, we don't support different methods
// of replace/insertion, so we can just return the value.
getOptionCompletion( attribute ) {
const value = {
key: attribute.id,
label: attribute.name,
};
return value;
},
},
},
},
],
}, You can drop that into a report config ( |
Thanks for the example. I added a minimalistic |
…mmerce-admin#6884) Co-authored-by: Jeff Stieler <[email protected]> Make `Search` component accept `autocompleter.options` that meet the requirements stated in [the docs](https://github.com/WordPress/gutenberg/tree/trunk/packages/components/src/autocomplete#options): > May be an array, a function that returns an array, or a function that returns a promise for an array. Fixes woocommerce/woocommerce-admin#6061.
Make
Search
component acceptautocompleter.options
that meet the requirements stated in the docs:Fixes #6061.
Accessibility
No texts or animations were changed
prefers-reduced-motion
Screenshots
Detailed test instructions:
Search
component withautocompleter
prop, that containsoptions
property with a static array, or function that returns an array:See the attached tests.
Additional notes:
Search
/SelectControl
component. #6883fetchOptions
look cleaner, but decided to use the same structure that is used inwordpress/components.Autocompleter
https://github.com/WordPress/gutenberg/blob/trunk/packages/components/src/autocomplete/index.js#L168-L171Alternative,
async/await
version is waiting at https://github.com/woocommerce/woocommerce-admin/compare/fix/6061-search-options-asyncDoubts:
1000
ms makes the test less stable, but I didn't find any official/documented way to wait for the exact promise/event so I can check for options to be rendered.With plain HTML (custom) elements, I'd expect either
filterPicker
to dispatch a DOM event or expose a promise, or at least DOM itself would notifyMutationObserver
with option/<search-element>
being added, but I'm not sure how it's meant to be monitored here.