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 55 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
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- `DropdownMenu`: migrate Storybook to controls ([47149](https://github.com/WordPress/gutenberg/pull/47149)).
- Removed deprecated `@storybook/addon-knobs` dependency from the package ([47152](https://github.com/WordPress/gutenberg/pull/47152)).
- `ColorListPicker`: Convert to TypeScript ([#46358](https://github.com/WordPress/gutenberg/pull/46358)).
- `QueryControls`: Convert to TypeScript ([#46721](https://github.com/WordPress/gutenberg/pull/46721)).

### Bug Fix

Expand Down
95 changes: 46 additions & 49 deletions packages/components/src/query-controls/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ const QUERY_DEFAULTS = {
},
],
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 @@ -66,24 +66,33 @@ The `QueryControls` component now supports multiple category selection, to repla
const QUERY_DEFAULTS = {
orderBy: 'title',
order: 'asc',
selectedCategories: [ 1 ],
categories: [
selectedCategories: [
ciampo marked this conversation as resolved.
Show resolved Hide resolved
{
id: 1,
value: 'Category 1',
},
{
id: 2,
value: 'Category 1b',
},
kienstra marked this conversation as resolved.
Show resolved Hide resolved
],
categories: {
'Category 1': {
id: 1,
name: 'Category 1',
parent: 0,
},
{
'Category 1b': {
id: 2,
name: 'Category 1b',
parent: 1,
},
{
'Category 2': {
id: 3,
name: 'Category 2',
parent: 0,
},
],
},
numberOfItems: 10,
};

Expand Down Expand Up @@ -115,128 +124,116 @@ The format of the categories list also needs to be updated to match what `FormTo

### Props

#### authorList
#### `authorList`: `AuthorSelectProps[ 'authorList' ]`

An array of author IDs that is passed into an `AuthorSelect` sub-component.
An array of authors that is passed into an `AuthorSelect` sub-component.

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

#### selectedAuthorId
#### `categoriesList`: `CategorySelectProps[ 'categoriesList' ]`

The selected author ID.
An array of categories, renders a `CategorySelect` sub-component when passed in conjunction with `onCategoryChange`.

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

#### categoriesList
#### `categorySuggestions`: `{ [ categoryName: Category[ 'name' ] ]: Category }`

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

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

#### categorySuggestions
#### `maxItems`: `number`

An array of category names; renders a `FormTokenField` component when passed in conjunction with `onCategoryChange`.
The maximum of items.

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

#### maxItems

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

#### minItems
#### `minItems`: `number`

The minimum of items.

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

#### numberOfItems
#### `numberOfItems`: `number`

The selected number of items to retrieve via the query.

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

#### onAuthorChange
#### `onAuthorChange`: `AuthorSelectProps[ 'onChange' ]`

A function that receives the new author value. If this is not specified, the author controls are not included.

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

#### onCategoryChange
#### `onCategoryChange`: `CategorySelectProps[ 'onChange' ] | FormTokenFieldProps[ 'onChange' ]`

A function that receives the new category value. If this is not specified, the category controls are not included.

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

#### onNumberOfItemsChange
#### `onNumberOfItemsChange`: `( newNumber?: number ) => void`

A function that receives the new number of items value. If this is not specified, then the number of items range control is not included.

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

#### onOrderChange
#### `onOrderChange`: `( newOrder: Order ) => void`

A function that receives the new order value. If this or onOrderByChange are not specified, then the order controls are not included.

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

#### onOrderByChange
#### `onOrderByChange`: `( newOrderBy: OrderBy ) => void`

A function that receives the new orderby value. If this or onOrderChange are not specified, then the order controls are not included.

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

#### order
#### `order`: `Order`

The order in which to retrieve posts. Can be 'asc' or 'desc'.
The order in which to retrieve posts.

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

#### orderBy
#### `orderBy`: `OrderBy`

The meta key by which to order posts.

The meta key by which to order posts. Can be 'date' or 'title'.
- Required: No
- Platform: Web

#### `selectedAuthorId`: `AuthorSelectProps[ 'selectedAuthorId' ]`

The selected author ID.

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

#### selectedCategories
#### `selectedCategories`: `Category[]`

The selected categories for the `categorySuggestions`.

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

#### selectedCategoryId
#### `selectedCategoryId`: `CategorySelectProps[ 'selectedCategoryId' ]`

The selected category for the `categoriesList`.

- Type: `Number`
- Required: No
- Platform: Web
23 changes: 0 additions & 23 deletions packages/components/src/query-controls/author-select.js

This file was deleted.

37 changes: 37 additions & 0 deletions packages/components/src/query-controls/author-select.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Internal dependencies
*/
import { buildTermsTree } from './terms';
import TreeSelect from '../tree-select';
import type { TreeSelectProps } from '../tree-select/types';
import type { AuthorSelectProps } from './types';

export default function AuthorSelect( {
label,
noOptionLabel,
authorList,
selectedAuthorId,
onChange: onChangeProp,
}: AuthorSelectProps ) {
if ( ! authorList ) return null;
const termsTree = buildTermsTree( authorList );
return (
<TreeSelect
{ ...{
label,
noOptionLabel,
// Since the `multiple` attribute is not passed to `TreeSelect`, it is
// safe to assume that the argument of `onChange` cannot be `string[]`.
// The correct solution would be to type `SelectControl` better, so that
// the type of `value` and `onChange` vary depending on `multiple`.
onChange: onChangeProp as TreeSelectProps[ 'onChange' ],
} }
tree={ termsTree }
selectedId={
selectedAuthorId !== undefined
? String( selectedAuthorId )
: undefined
}
/>
);
}
31 changes: 0 additions & 31 deletions packages/components/src/query-controls/category-select.js

This file was deleted.

46 changes: 46 additions & 0 deletions packages/components/src/query-controls/category-select.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Internal dependencies
*/
import { buildTermsTree } from './terms';
import TreeSelect from '../tree-select';
import type { TreeSelectProps } from '../tree-select/types';

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

export default function CategorySelect( {
label,
noOptionLabel,
categoriesList,
selectedCategoryId,
onChange: onChangeProp,
...props
}: CategorySelectProps ) {
const termsTree = useMemo( () => {
return buildTermsTree( categoriesList );
}, [ categoriesList ] );

return (
<TreeSelect
{ ...{
label,
noOptionLabel,
// Since the `multiple` attribute is not passed to `TreeSelect`, it is
// safe to assume that the argument of `onChange` cannot be `string[]`.
// The correct solution would be to type `SelectControl` better, so that
// the type of `value` and `onChange` vary depending on `multiple`.
onChange: onChangeProp as TreeSelectProps[ 'onChange' ],
} }
tree={ termsTree }
selectedId={
selectedCategoryId !== undefined
? String( selectedCategoryId )
: undefined
}
{ ...props }
/>
);
}
Loading