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

Refactor QueryControls component to TypeScript #46721

Merged
merged 59 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
a20e5ea
Begin conversion of QueryControls to TypeScript
kienstra Dec 21, 2022
26991a7
Remove the extra value properties
kienstra Dec 21, 2022
df0b006
Fix failing unit test
kienstra Dec 21, 2022
3e88bbb
Create a TermsWithChildren type
kienstra Dec 21, 2022
d3ceb5e
Change the id to a string
kienstra Dec 21, 2022
5017969
Make onCategoryChange an intersection type
kienstra Dec 21, 2022
17382e9
Shorten the JSDoc lines
kienstra Dec 21, 2022
56e177e
Correct the React import
kienstra Dec 21, 2022
8c6dab7
Make the properties of QueryControlsProps optional
kienstra Dec 21, 2022
b6854a1
Alphabetize types.ts
kienstra Dec 21, 2022
992ce11
Fix the suggestions
kienstra Dec 21, 2022
f68e197
Rename Term to Entity
kienstra Dec 21, 2022
f4ab051
Fix the type of categorySuggestions
kienstra Dec 21, 2022
51e43fa
Correct the JSDoc summary for QueryControls
kienstra Dec 21, 2022
b87ddbc
Make SelectControl accept a number
kienstra Dec 21, 2022
c887428
Change the order of number and string
kienstra Dec 21, 2022
7957dd8
Empty commit to trigger CI/CD
kienstra Dec 21, 2022
cda599c
Remove needless change of id type
kienstra Dec 21, 2022
6b34f84
Update README.md based on types.ts
kienstra Dec 22, 2022
c52a456
Let TS infer the return type of buildTermsTree()
kienstra Dec 22, 2022
be24601
Change array to object, as that's what categorySuggestions is
kienstra Dec 22, 2022
a27485e
Add a CHANGELOG entry
kienstra Dec 22, 2022
b9994d4
Add basic stories for QueryControls
kienstra Dec 23, 2022
fe344b3
Correct the type for selectedCategories
kienstra Dec 24, 2022
c2574a7
Correct the type of categorySuggestions
kienstra Dec 24, 2022
599a5b3
Merge branch 'trunk' into update/query-controls-ts
kienstra Dec 24, 2022
d7ed654
Update the types in README.md
kienstra Dec 24, 2022
87c3588
Commit Marco's suggestion: Update packages/components/src/query-contr…
kienstra Jan 12, 2023
c18ae18
Merge in trunk, resolve conflicts
kienstra Jan 12, 2023
7290d54
Add hideCancelButton back in
kienstra Jan 12, 2023
2442dd5
Revert change to SelectControl and TreeSelect, instead change what's …
kienstra Jan 12, 2023
ecec1df
Revert README.md change to select-control
kienstra Jan 12, 2023
25aaac3
Commit Marco's suggestions for flatTerms verbatim
kienstra Jan 12, 2023
d714ede
Commit Marco's suggestion for parameters
kienstra Jan 12, 2023
4e19a9f
Remove fallback to ''
kienstra Jan 12, 2023
0220eb9
Save the author as a Number
kienstra Jan 13, 2023
bb7331c
Fix the order and orderBy types
kienstra Jan 13, 2023
30b3e9c
Alphabetize the QueryControls props
kienstra Jan 13, 2023
b03a5fb
Fix unit tests for the ID being a string
kienstra Jan 13, 2023
65e69de
Disable controls for values that you can change via the UI
kienstra Jan 13, 2023
2840517
Apply Marco's suggestion for selected*Id
kienstra Jan 18, 2023
9a7e30c
Merge branch 'trunk' into update/query-controls-ts
kienstra Jan 18, 2023
6a89206
Commit Marco's suggestion to prevent type casting
kienstra Jan 19, 2023
09e96fd
Part 1: Commit Marco's changes that handle multiple types
kienstra Jan 19, 2023
a90ced1
Part 2: Commit Marco's change that handle multiple types
kienstra Jan 19, 2023
07d7291
Update README.md for Marco's changes
kienstra Jan 19, 2023
30748b9
Remove Category type entirely, replace with Entity intersection
kienstra Jan 19, 2023
c6410a5
Make the category name optional
kienstra Jan 19, 2023
9a7ea70
Remove the name from selectedCategories in the unit test
kienstra Jan 19, 2023
b37a4a2
Remove needless casting
kienstra Jan 19, 2023
50b2a78
Commit Marco's formatting diff
kienstra Jan 24, 2023
a08a240
Commit Marco's diff verbatim to fix the types
kienstra Jan 24, 2023
9e512a3
Update README.md for categorySuggestions
kienstra Jan 24, 2023
11f6bda
Merge branch 'trunk' into update/query-controls-ts
kienstra Jan 24, 2023
474ff5e
Move CHANGELOG entry to Unreleased
kienstra Jan 24, 2023
531d337
Marco's diff: Update packages/components/src/query-controls/README.md
kienstra Jan 24, 2023
46f3c60
Marco's diff for types.ts DocBlocks
kienstra Jan 24, 2023
9dc0480
Merge in trunk, resolve conflict
kienstra Jan 24, 2023
4aa1375
Marco's update to types.ts and README.md
kienstra Jan 25, 2023
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
23 changes: 11 additions & 12 deletions packages/components/src/query-controls/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@ import { QueryControls } from '@wordpress/components';
import { useState } from '@wordpress/element';

const QUERY_DEFAULTS = {
category: 1,
category: '1',
categories: [
{
id: 1,
id: '1',
name: 'Category 1',
parent: 0,
},
{
id: 2,
id: '2',
name: 'Category 1b',
parent: 1,
},
{
id: 3,
id: '3',
name: 'Category 2',
parent: 0,
},
],
maxItems: 20,
minItems: 1,
minItems: 1,
numberOfItems: 10,
order: 'asc',
orderBy: 'title',
orderBy: 'title',
ciampo marked this conversation as resolved.
Show resolved Hide resolved
};

const MyQueryControls = () => {
Expand Down Expand Up @@ -69,17 +69,17 @@ const QUERY_DEFAULTS = {
selectedCategories: [ 1 ],
categories: [
{
id: 1,
id: '1',
name: 'Category 1',
parent: 0,
},
{
id: 2,
id: '2',
name: 'Category 1b',
parent: 1,
},
{
id: 3,
id: '3',
name: 'Category 2',
parent: 0,
},
Expand Down Expand Up @@ -123,17 +123,16 @@ An array of author IDs that is passed into an `AuthorSelect` sub-component.
- Required: No
- Platform: Web

#### selectedAuthorId
#### `selectedAuthorId`: `string`

The selected author ID.

- Type: `Number`
- Required: No
- Platform: Web

#### categoriesList

An array of category IDs; renders a `CategorySelect` sub-component when passed in conjunction with `onCategoryChange`.
An array of categories; renders a `CategorySelect` sub-component when passed in conjunction with `onCategoryChange`.

- Type: `Array`
- Required: No
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
*/
import { buildTermsTree } from './terms';
import TreeSelect from '../tree-select';
import type { AuthorSelectProps } from './types';

export default function AuthorSelect( {
label,
noOptionLabel,
authorList,
selectedAuthorId,
onChange,
} ) {
}: AuthorSelectProps ) {
if ( ! authorList ) return null;
const termsTree = buildTermsTree( authorList );
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
*/
import { buildTermsTree } from './terms';
import TreeSelect from '../tree-select';

/**
* WordPress dependencies
*/
import { useMemo } from '@wordpress/element';
import type { CategorySelectProps } from './types';

export default function CategorySelect( {
label,
Expand All @@ -15,7 +17,7 @@ export default function CategorySelect( {
selectedCategoryId,
onChange,
...props
} ) {
}: CategorySelectProps ) {
const termsTree = useMemo( () => {
return buildTermsTree( categoriesList );
}, [ categoriesList ] );
Expand Down
1 change: 0 additions & 1 deletion packages/components/src/query-controls/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ const QueryControls = memo(
noOptionLabel={ __( 'All' ) }
selectedCategoryId={ selectedCategoryId }
onChange={ onCategoryChange }
hideCancelButton={ true }
ciampo marked this conversation as resolved.
Show resolved Hide resolved
/>
) }
{ onNumberOfItemsChange && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,43 @@ import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import CategorySelect from './category-select';
import { RangeControl, SelectControl, FormTokenField } from '../';
import AuthorSelect from './author-select';
import CategorySelect from './category-select';
import FormTokenField from '../form-token-field';
import RangeControl from '../range-control';
import SelectControl from '../select-control';
import type { QueryControlsProps } from './types';

const DEFAULT_MIN_ITEMS = 1;
const DEFAULT_MAX_ITEMS = 100;
const MAX_CATEGORIES_SUGGESTIONS = 20;

export default function QueryControls( {
/**
* Select controls that query for entities.
*
* ```jsx
* const MyQueryControls = () => (
* <QueryControls
* { ...{ maxItems, minItems, numberOfItems, order, orderBy } }
* onOrderByChange={ ( newOrderBy ) => {
* updateQuery( { orderBy: newOrderBy } )
* }
* onOrderChange={ ( newOrder ) => {
* updateQuery( { order: newOrder } )
* }
* categoriesList={ categories }
* selectedCategoryId={ category }
* onCategoryChange={ ( newCategory ) => {
* updateQuery( { category: newCategory } )
* }
* onNumberOfItemsChange={ ( newNumberOfItems ) => {
* updateQuery( { numberOfItems: newNumberOfItems } )
* } }
* />
* );
* ```
*/
export function QueryControls( {
authorList,
selectedAuthorId,
categoriesList,
Expand All @@ -31,7 +59,7 @@ export default function QueryControls( {
onNumberOfItemsChange,
onOrderChange,
onOrderByChange,
} ) {
}: QueryControlsProps ) {
return [
onOrderChange && onOrderByChange && (
<SelectControl
Expand Down Expand Up @@ -59,6 +87,10 @@ export default function QueryControls( {
},
] }
onChange={ ( value ) => {
if ( typeof value !== 'string' ) {
return;
ciampo marked this conversation as resolved.
Show resolved Hide resolved
}

const [ newOrderBy, newOrder ] = value.split( '/' );
if ( newOrder !== order ) {
onOrderChange( newOrder );
Expand Down Expand Up @@ -87,7 +119,7 @@ export default function QueryControls( {
selectedCategories &&
selectedCategories.map( ( item ) => ( {
id: item.id,
value: item.name || item.value,
value: item.name || item.value || '',
ciampo marked this conversation as resolved.
Show resolved Hide resolved
} ) )
}
suggestions={ Object.keys( categorySuggestions ) }
Expand Down Expand Up @@ -119,3 +151,5 @@ export default function QueryControls( {
),
];
}

export default QueryControls;
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@
*/
import { groupBy } from 'lodash';

/**
* Internal dependencies
*/
import type { Entity, TermsWithChildren } from './types';

/**
* Returns terms in a tree form.
*
* @param {Array} flatTerms Array of terms in flat format.
* @param flatTerms Array of terms in flat format.
kienstra marked this conversation as resolved.
Show resolved Hide resolved
*
* @return {Array} Array of terms in tree format.
* @return Array of terms in tree format.
*/
export function buildTermsTree( flatTerms ) {
export function buildTermsTree(
flatTerms: readonly Entity[]
): TermsWithChildren {
const flatTermsWithParentAndChildren = flatTerms.map( ( term ) => {
return {
children: [],
Expand All @@ -23,7 +30,7 @@ export function buildTermsTree( flatTerms ) {
if ( termsByParent.null && termsByParent.null.length ) {
return flatTermsWithParentAndChildren;
}
const fillWithChildren = ( terms ) => {
const fillWithChildren = ( terms: Entity[] ): TermsWithChildren => {
return terms.map( ( term ) => {
const children = termsByParent[ term.id ];
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,60 +6,76 @@ 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 },
{ id: 2232, name: 'foo', dummy: true },
{ id: 2245, name: 'baz', dummy: true },
ciampo marked this conversation as resolved.
Show resolved Hide resolved
] );
const output = Object.freeze( [
{ id: 2232, parent: null, children: [], dummy: true },
{ id: 2245, parent: null, children: [], dummy: true },
{
id: 2232,
name: 'foo',
parent: null,
children: [],
dummy: true,
},
{
id: 2245,
name: 'baz',
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 },
{ id: 2232, name: 'foo', parent: 0, dummy: true },
{ id: 2245, name: 'baz', parent: 0, dummy: false },
] );
const output = [
{ id: 2232, parent: 0, children: [], dummy: true },
{ id: 2245, parent: 0, children: [], dummy: false },
{ id: 2232, name: 'foo', parent: 0, children: [], dummy: true },
{ id: 2245, name: 'baz', 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 },
{ id: 2232, name: 'foo', parent: 0 },
{ id: 2245, name: 'baz', parent: 2232 },
] );
const output = [
{
id: 2232,
name: 'foo',
parent: 0,
children: [ { id: 2245, parent: 2232, children: [] } ],
children: [
{ id: 2245, name: 'baz', 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 },
{ id: 2232, name: 'a', parent: 0 },
{ id: 2245, name: 'b', parent: 2232 },
{ id: 2249, name: 'c', parent: 0 },
{ id: 2246, name: 'd', parent: 2232 },
] );
const output = [
{
id: 2232,
name: 'a',
parent: 0,
children: [
{ id: 2245, parent: 2232, children: [] },
{ id: 2246, parent: 2232, children: [] },
{ id: 2245, name: 'b', parent: 2232, children: [] },
{ id: 2246, name: 'd', parent: 2232, children: [] },
],
},
{ id: 2249, parent: 0, children: [] },
{ id: 2249, name: 'c', parent: 0, children: [] },
];
const termsTreem = buildTermsTree( input );
expect( termsTreem ).toEqual( output );
Expand Down
Loading