Skip to content

Commit

Permalink
Fix search & only build a hierarchical tree when not searching.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsilverstein committed Oct 22, 2020
1 parent c3add15 commit 4a27a9b
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions packages/editor/src/components/page-attributes/parent.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function getTitle( post ) {
export function PageAttributesParent() {
const { editPost } = useDispatch( 'core/editor' );
const [ fieldValue, setFieldValue ] = useState( false );
const isSearching = fieldValue && '' !== fieldValue;
const { parentPost, parentPostId, items, postType } = useSelect(
( select ) => {
const { getPostType, getEntityRecords, getEntityRecord } = select(
Expand All @@ -53,7 +54,9 @@ export function PageAttributesParent() {
order: 'asc',
_fields: 'id,title,parent',
};
if ( parentPost && fieldValue && '' !== fieldValue ) {

// Perform a search when the field is changed.
if ( isSearching ) {
query.search = fieldValue;
}

Expand Down Expand Up @@ -85,21 +88,29 @@ export function PageAttributesParent() {
};

const parentOptions = useMemo( () => {
const tree = buildTermsTree(
pageItems.map( ( item ) => ( {
id: item.id,
parent: item.parent,
name: getTitle( item ),
} ) )
);
let tree = pageItems.map( ( item ) => ( {
id: item.id,
parent: item.parent,
name: getTitle( item ),
} ) );

// Only build a hierarchical tree when not searching.
if ( ! isSearching ) {
tree = buildTermsTree( tree );
}

const opts = getOptionsFromTree( tree );
const itemsHaveParent = find( opts, item => item.value === parentPostId );
if ( parentPost && ! itemsHaveParent ) {

// Ensure the current parent is in the options list.
const optsHasParent = find(
opts,
( item ) => item.value === parentPostId
);
if ( parentPost && ! optsHasParent ) {
opts.unshift( {
value: parentPostId,
label: getTitle( parentPost ),
} );

} );
}
return opts;
}, [ pageItems ] );
Expand Down

0 comments on commit 4a27a9b

Please sign in to comment.