Skip to content

Commit

Permalink
Fix parent post selector: ensure initial value available, search perf…
Browse files Browse the repository at this point in the history
…ormed, all results shown. (#26397)

Co-authored-by: Nik Tsekouras <ntsekouras@outlook.com>
  • Loading branch information
2 people authored and tellthemachines committed Oct 27, 2020
1 parent 7bbc996 commit 5482af9
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions packages/editor/src/components/page-attributes/parent.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
debounce,
flatMap,
repeat,
find,
} from 'lodash';

/**
Expand All @@ -31,6 +32,7 @@ function getTitle( post ) {
export function PageAttributesParent() {
const { editPost } = useDispatch( 'core/editor' );
const [ fieldValue, setFieldValue ] = useState( false );
const isSearching = fieldValue;
const { parentPost, parentPostId, items, postType } = useSelect(
( select ) => {
const { getPostType, getEntityRecords, getEntityRecord } = select(
Expand All @@ -52,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 @@ -84,15 +88,30 @@ 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 );

// 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 5482af9

Please sign in to comment.