-
Notifications
You must be signed in to change notification settings - Fork 50
Add extensibility filters to ContentPicker and ContentSearch components #373
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
Add extensibility filters to ContentPicker and ContentSearch components #373
Conversation
… and to filter the search result before it is returned
# Conflicts: # components/content-picker/PickedItem.tsx # components/content-picker/SortableList.tsx # package-lock.json # package.json
|
Merge conflicts were addressed. This is ready for review. |
components/content-search/utils.ts
Outdated
| * @param {ContentSearchMode} mode - The mode of the content search. | ||
| * @returns {string[]} - The filtered fields. | ||
| */ | ||
| fields = applyFilters('tenup.contentSearch.queryFields', fields, mode) as string[]; |
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.
I'm a little worried about the introduction of all these filters... Unlike in PHP here in JS they just behave oddly and are no longer really recommended by the core team...
Also I am a bit worried that there is no way to figure out the context of which component we are talking about. It just applies to all ContentSearch components on the site.
I would rather see an implementation that has the filters needed within the content connect plugin and then passes its callback function into the component as an optional prop. That feels more "safe" to me here.
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.
Hi @fabiankaegy this is ready for another round of review
- Removed all
applyFilterscalls fromContentSearchandContentPickercomponents - Added optional callback props with proper TypeScript types
# Conflicts: # package-lock.json
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.
Pull request overview
This PR adds extensibility to the ContentPicker and ContentSearch components through optional filter callback props, enabling developers to customize API queries and UI display on a per-instance basis while maintaining backward compatibility.
Key Changes:
- Added three filter callbacks to ContentPicker:
queryFieldsFilter,searchResultFilter, andpickedItemFilter - Added two filter callbacks to ContentSearch:
queryFieldsFilterandsearchResultFilter - Refactored code to support conditional field querying and result normalization
- Updated documentation and examples to demonstrate the new functionality
- Added PHP example for registering custom REST API fields
Reviewed changes
Copilot reviewed 14 out of 16 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
| package.json | Removed self-referencing workspace entry and added npm scripts for environment management |
| example/plugin.php | Fixed PHP coding standards (removed unnecessary semicolons) and added REST API field registration example |
| example/package.json | Minor formatting fix (added trailing newline) |
| example/src/blocks/content-search-example/edit.tsx | Demonstrates usage of new filter props with ContentSearch component |
| example/src/blocks/content-picker-example/edit.tsx | Demonstrates usage of new filter props with ContentPicker component |
| components/content-search/utils.ts | Added support for field filtering and result customization in search utilities |
| components/content-search/types.ts | Added TypeScript types for new filter callbacks |
| components/content-search/index.tsx | Integrated filter props into ContentSearch component |
| components/content-search/SearchItem.tsx | Updated UI to conditionally display URL and custom info fields with HTML sanitization |
| components/content-search/readme.md | Added documentation for new filter props with usage examples |
| components/content-picker/index.tsx | Added filter props to ContentPicker interface and passed to child components |
| components/content-picker/SortableList.tsx | Integrated filters into picked item data fetching and normalization |
| components/content-picker/PickedItem.tsx | Updated UI to support optional URL and custom info fields |
| components/content-picker/readme.md | Added documentation for new filter props with usage examples |
| CONTRIBUTING.md | Updated local environment setup instructions to clarify workspace usage |
Comments suppressed due to low confidence (1)
components/content-search/readme.md:58
- The documentation should include a note that when using
queryFieldsFilterto fetch additional fields from the WordPress REST API search endpoint, developers may need to register those fields via PHP usingregister_rest_field. Without this note, developers might be confused when additional fields don't appear in search results. Consider adding a brief note with a link to WordPress documentation or a code example.
### Customizing Fields and Results
You can customize which fields are fetched from the API and how search results are normalized using the `queryFieldsFilter` and `searchResultFilter` props:
```js
import { ContentSearch } from '@10up/block-components';
import { useCallback } from '@wordpress/element';
function MyComponent( props ) {
const queryFieldsFilter = useCallback( (fields, mode) => {
if ( mode === 'post' ) {
fields.push('excerpt');
}
return fields;
}, [] );
const searchResultFilter = useCallback( (item, result) => {
item.url = '';
item.info = `<strong>ID:</strong> ${result.id}<br>${result.excerpt}`;
return item;
}, [] );
return (
<ContentSearch
onSelectItem={ (item) => { console.log(item) } }
mode="post"
label={ "Please select a Post or Page:" }
contentTypes={ [ 'post', 'page' ] }
queryFieldsFilter={queryFieldsFilter}
searchResultFilter={searchResultFilter}
/>
)
}
</details>
---
💡 <a href="/10up/block-components/new/develop/.github/instructions?filename=*.instructions.md" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Add Copilot custom instructions</a> for smarter, more guided reviews. <a href="https://docs.github.com/en/copilot/customizing-copilot/adding-repository-custom-instructions-for-github-copilot" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Learn how to get started</a>.
fabiankaegy
left a comment
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.
This is great! :) Thank you for making the changes :)
Description of the Change
This PR introduces optional callback props in the
ContentPickerandContentSearchcomponents, enabling developers to extend queried fields and customize UI display while maintaining explicit, context-aware component customization.This approach:
Problem
Client projects frequently require additional information beyond the default fields (title, URL) when picking or searching content. Common requests include displaying post IDs, parent page titles, or custom metadata to help content editors make informed selections.
Solution
Added callback props that:
New Props
ContentPicker
queryFieldsFilter- Customizes which fields are fetched from the API for both search and picked itemssearchResultFilter- Customizes the normalized search result itempickedItemFilter- Customizes the picked item before it's displayed in the listContentSearch
queryFieldsFilter- Customizes which fields are fetched from the APIsearchResultFilter- Customizes the normalized search result itemNote: For the WordPress REST API search endpoint to return additional fields like
excerpt, you may need to register them via PHP:Technical Changes
How to test the Change
npm run build(from root directory)npm run build -w examplenpm run start-test-envpickedItemFiltersearchResultFiltersearchResultFilterChangelog Entry
Credits
Props @s3rgiosan
Checklist: