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

<CategorySelect> support post_tags and hierarchical = false taxonomy. #13076

Merged
merged 5 commits into from
Jan 22, 2019
Merged
Show file tree
Hide file tree
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
13 changes: 12 additions & 1 deletion packages/components/src/query-controls/terms.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,18 @@ import { groupBy } from 'lodash';
* @return {Array} Array of terms in tree format.
*/
export function buildTermsTree( flatTerms ) {
const termsByParent = groupBy( flatTerms, 'parent' );
const flatTermsWithParentAndChildren = flatTerms.map( ( term ) => {
return {
children: [],
parent: null,
...term,
};
} );

const termsByParent = groupBy( flatTermsWithParentAndChildren, 'parent' );
if ( termsByParent.null && termsByParent.null.length ) {
return flatTermsWithParentAndChildren;
}
const fillWithChildren = ( terms ) => {
return terms.map( ( term ) => {
const children = termsByParent[ term.id ];
Expand Down
61 changes: 61 additions & 0 deletions packages/components/src/query-controls/test/terms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Internal dependencies
*/
import { buildTermsTree } from '../terms';

describe( 'buildTermsTree()', () => {
it( 'Should return same array as input with null parent and empty children added if parent is never specified.', () => {
const input = Object.freeze( [
{ id: 2232, dummy: true },
{ id: 2245, dummy: true },
] );
const output = Object.freeze( [
{ id: 2232, parent: null, children: [], dummy: true },
{ id: 2245, parent: null, children: [], dummy: true },
] );
const termsTreem = buildTermsTree( input );
expect( termsTreem ).toEqual( output );
} );
it( 'Should return same array as input with empty children added if all the elements are top level', () => {
const input = Object.freeze( [
{ id: 2232, parent: 0, dummy: true },
{ id: 2245, parent: 0, dummy: false },
] );
const output = [
{ id: 2232, parent: 0, children: [], dummy: true },
{ id: 2245, parent: 0, children: [], dummy: false },
];
const termsTreem = buildTermsTree( input );
expect( termsTreem ).toEqual( output );
} );
it( 'Should return element with its child if a child exists', () => {
const input = Object.freeze( [
{ id: 2232, parent: 0 },
{ id: 2245, parent: 2232 },
] );
const output = [
{ id: 2232, parent: 0, children: [
{ id: 2245, parent: 2232, children: [] },
] },
];
const termsTreem = buildTermsTree( input );
expect( termsTreem ).toEqual( output );
} );
it( 'Should return elements with multiple children and elements with no children', () => {
const input = Object.freeze( [
{ id: 2232, parent: 0 },
{ id: 2245, parent: 2232 },
{ id: 2249, parent: 0 },
{ id: 2246, parent: 2232 },
] );
const output = [
{ id: 2232, parent: 0, children: [
{ id: 2245, parent: 2232, children: [] },
{ id: 2246, parent: 2232, children: [] },
] },
{ id: 2249, parent: 0, children: [] },
];
const termsTreem = buildTermsTree( input );
expect( termsTreem ).toEqual( output );
} );
} );
13 changes: 12 additions & 1 deletion packages/editor/src/utils/terms.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,18 @@ import { groupBy } from 'lodash';
* @return {Array} Array of terms in tree format.
*/
export function buildTermsTree( flatTerms ) {
const termsByParent = groupBy( flatTerms, 'parent' );
const flatTermsWithParentAndChildren = flatTerms.map( ( term ) => {
return {
children: [],
parent: null,
...term,
};
} );

const termsByParent = groupBy( flatTermsWithParentAndChildren, 'parent' );
if ( termsByParent.null && termsByParent.null.length ) {
return flatTermsWithParentAndChildren;
}
const fillWithChildren = ( terms ) => {
return terms.map( ( term ) => {
const children = termsByParent[ term.id ];
Expand Down
13 changes: 10 additions & 3 deletions packages/editor/src/utils/test/terms.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@
import { buildTermsTree } from '../terms';

describe( 'buildTermsTree()', () => {
it( 'Should return empty array if parent is never specified.', () => {
const input = Object.freeze( [ { term: 2232 }, { term: 2245 } ] );
it( 'Should return same array as input with null parent and empty children added if parent is never specified.', () => {
const input = Object.freeze( [
{ id: 2232, dummy: true },
{ id: 2245, dummy: true },
] );
const output = Object.freeze( [
{ id: 2232, parent: null, children: [], dummy: true },
{ id: 2245, parent: null, children: [], dummy: true },
] );
const termsTreem = buildTermsTree( input );
expect( termsTreem ).toEqual( [] );
expect( termsTreem ).toEqual( output );
} );
it( 'Should return same array as input with empty children added if all the elements are top level', () => {
const input = Object.freeze( [
Expand Down