Skip to content
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

Fix parent post selector: ensure initial value available, search performed, all results shown. #26397

Merged
merged 3 commits into from
Oct 26, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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