Skip to content

Commit 050f230

Browse files
committed
Address code review feedback
1 parent 0cbbab7 commit 050f230

File tree

7 files changed

+30
-26
lines changed

7 files changed

+30
-26
lines changed

components/content-picker/SortableList.tsx

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import PickedItem, { PickedItemType } from './PickedItem';
3232
import { DraggableChip } from './DraggableChip';
3333
import { ContentSearchMode, QueryFieldsFilter } from '../content-search/types';
3434
import type { PickedItemFilter } from './index';
35+
import { Term } from './types';
3536

3637
const dropAnimation = {
3738
...defaultDropAnimation,
@@ -49,18 +50,6 @@ interface SortableListProps {
4950
pickedItemFilter?: PickedItemFilter;
5051
}
5152

52-
type Term = {
53-
count: number;
54-
description: string;
55-
id: number;
56-
link: string;
57-
meta: Record<string, unknown>;
58-
name: string;
59-
parent: number;
60-
slug: string;
61-
taxonomy: string;
62-
};
63-
6453
function getEntityKind(mode: ContentSearchMode) {
6554
let type;
6655
switch (mode) {

components/content-picker/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { select } from '@wordpress/data';
33
import { useMemo } from '@wordpress/element';
44
import { __ } from '@wordpress/i18n';
55
import { VisuallyHidden } from '@wordpress/components';
6+
import { Post, User } from '@wordpress/core-data';
67
import { v4 as uuidv4 } from 'uuid';
78
import { ContentSearch } from '../content-search';
89
import SortableList from './SortableList';
@@ -17,10 +18,11 @@ import {
1718
} from '../content-search/types';
1819
import { NormalizedSuggestion } from '../content-search/utils';
1920
import { PickedItemType } from './PickedItem';
21+
import { Term } from './types';
2022

2123
export type PickedItemFilter = (
2224
item: Partial<PickedItemType>,
23-
originalResult: any,
25+
originalResult: Post | Term | User,
2426
) => Partial<PickedItemType>;
2527

2628
const NAMESPACE = 'tenup-content-picker';

components/content-picker/readme.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@ function MyComponent( props ) {
4141
}, [] );
4242

4343
const searchResultFilter = useCallback( (item, result) => {
44-
item.url = '';
45-
item.info = `<strong>ID:</strong> ${result.id}<br>${result.excerpt}`;
46-
return item;
44+
const info = `<strong>ID:</strong> ${result.id}<br>${result.excerpt}`;
45+
return { ...item, url: '', info };
4746
}, [] );
4847

4948
const pickedItemFilter = useCallback( (item, result) => {
@@ -71,7 +70,7 @@ function MyComponent( props ) {
7170
|----------------------|------------|----------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
7271
| `onPickChange` | `function` | `undefined` | Callback function the list of picked content gets changed |
7372
| `queryFilter` | `function` | `undefined` | Function called to allow you to customize the query before is made. It's advisable to use `useCallback` to save this parameter |
74-
| `queryFieldsFilter` | `function` | `undefined` | Function to customize which fields are fetched from the API for both search and picked items. Receives `(fields: string[], mode: ContentSearchMode) => string[]`. It's advisable to use `useCallback` to save this parameter. |
73+
| `queryFieldsFilter` | `function` | `undefined` | Function to customize which fields are fetched from the API for both search and picked items. Receives `(fields: string[], mode: ContentSearchMode) => string[]`. When requesting additional fields from the WordPress REST API search endpoint, you may need to register those fields in PHP using `register_rest_field` so that they are available on search results as well as on picked items. It's advisable to use `useCallback` to save this parameter. |
7574
| `searchResultFilter` | `function` | `undefined` | Function to customize the normalized search result item. Receives `(item: NormalizedSuggestion, originalResult: WP_REST_API_Search_Result \| WP_REST_API_User) => NormalizedSuggestion`. It's advisable to use `useCallback` to save this parameter. |
7675
| `pickedItemFilter` | `function` | `undefined` | Function to customize the picked item before it's displayed in the list. Receives `(item: Partial<PickedItemType>, originalResult: Post \| Term \| User) => Partial<PickedItemType>`. It's advisable to use `useCallback` to save this parameter. |
7776
| `label` | `string` | `''` | Renders a label for the Search Field.

components/content-picker/types.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export type Term = {
2+
count: number;
3+
description: string;
4+
id: number;
5+
link: string;
6+
meta: Record<string, unknown>;
7+
name: string;
8+
parent: number;
9+
slug: string;
10+
taxonomy: string;
11+
};

components/content-search/readme.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ function MyComponent( props ) {
3939
}, [] );
4040

4141
const searchResultFilter = useCallback( (item, result) => {
42-
item.url = '';
43-
item.info = `<strong>ID:</strong> ${result.id}<br>${result.excerpt}`;
44-
return item;
42+
return {
43+
...item,
44+
url: '',
45+
info: `<strong>ID:</strong> ${result.id}<br>${result.excerpt}`,
46+
};
4547
}, [] );
4648

4749
return (

example/src/blocks/content-picker-example/edit.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ export const BlockEdit = (props) => {
2424
}, []);
2525

2626
const searchResultFilter = useCallback((item, result) => {
27-
item.url = '';
28-
item.info = `<strong>ID:</strong> ${result.id}<br>${result.excerpt || ''}`;
29-
return item;
27+
const info = `<strong>ID:</strong> ${result.id}<br>${result.excerpt || ''}`;
28+
return { ...item, url: '', info };
3029
}, []);
3130

3231
const pickedItemFilter = useCallback((item, result) => {

example/src/blocks/content-search-example/edit.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ export const BlockEdit = (props) => {
2424
}, []);
2525

2626
const searchResultFilter = useCallback((item, result) => {
27-
item.url = '';
28-
item.info = `<strong>ID:</strong> ${result.id}<br>${result.excerpt || ''}`;
29-
return item;
27+
return {
28+
...item,
29+
url: '',
30+
info: `<strong>ID:</strong> ${result.id}<br>${result.excerpt || ''}`,
31+
};
3032
}, []);
3133

3234
const blockProps = useBlockProps();

0 commit comments

Comments
 (0)