-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathindex.tsx
More file actions
193 lines (186 loc) · 5.08 KB
/
index.tsx
File metadata and controls
193 lines (186 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
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 { VStack } from '../v-stack';
import type {
QueryControlsProps,
QueryControlsWithMultipleCategorySelectionProps,
QueryControlsWithSingleCategorySelectionProps,
} from './types';
const DEFAULT_MIN_ITEMS = 1;
const DEFAULT_MAX_ITEMS = 100;
const MAX_CATEGORIES_SUGGESTIONS = 20;
function isSingleCategorySelection(
props: QueryControlsProps
): props is QueryControlsWithSingleCategorySelectionProps {
return 'categoriesList' in props;
}
function isMultipleCategorySelection(
props: QueryControlsProps
): props is QueryControlsWithMultipleCategorySelectionProps {
return 'categorySuggestions' in props;
}
/**
* Controls to query for posts.
*
* ```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,
numberOfItems,
order,
orderBy,
maxItems = DEFAULT_MAX_ITEMS,
minItems = DEFAULT_MIN_ITEMS,
onAuthorChange,
onNumberOfItemsChange,
onOrderChange,
onOrderByChange,
// Props for single OR multiple category selection are not destructured here,
// but instead are destructured inline where necessary.
...props
}: QueryControlsProps ) {
return (
<VStack spacing="2">
{ [
onOrderChange && onOrderByChange && (
<SelectControl
__nextHasNoMarginBottom
key="query-controls-order-select"
label={ __( 'Order by' ) }
value={ `${ orderBy }/${ order }` }
options={ [
{
label: __( 'Newest to oldest' ),
value: 'date/desc',
},
{
label: __( 'Oldest to newest' ),
value: 'date/asc',
},
{
/* translators: label for ordering posts by title in ascending order */
label: __( 'A → Z' ),
value: 'title/asc',
},
{
/* translators: label for ordering posts by title in descending order */
label: __( 'Z → A' ),
value: 'title/desc',
},
] }
onChange={ ( value ) => {
if ( typeof value !== 'string' ) {
return;
}
const [ newOrderBy, newOrder ] = value.split( '/' );
if ( newOrder !== order ) {
onOrderChange(
newOrder as NonNullable<
QueryControlsProps[ 'order' ]
>
);
}
if ( newOrderBy !== orderBy ) {
onOrderByChange(
newOrderBy as NonNullable<
QueryControlsProps[ 'orderBy' ]
>
);
}
} }
/>
),
isSingleCategorySelection( props ) &&
props.categoriesList &&
props.onCategoryChange && (
<CategorySelect
key="query-controls-category-select"
categoriesList={ props.categoriesList }
label={ __( 'Category' ) }
noOptionLabel={ __( 'All' ) }
selectedCategoryId={ props.selectedCategoryId }
onChange={ props.onCategoryChange }
/>
),
isMultipleCategorySelection( props ) &&
props.categorySuggestions &&
props.onCategoryChange && (
<FormTokenField
key="query-controls-categories-select"
label={ __( 'Categories' ) }
value={
props.selectedCategories &&
props.selectedCategories.map( ( item ) => ( {
id: item.id,
// Keeping the fallback to `item.value` for legacy reasons,
// even if items of `selectedCategories` should not have a
// `value` property.
// @ts-expect-error
value: item.name || item.value,
} ) )
}
suggestions={ Object.keys(
props.categorySuggestions
) }
onChange={ props.onCategoryChange }
maxSuggestions={ MAX_CATEGORIES_SUGGESTIONS }
/>
),
onAuthorChange && (
<AuthorSelect
key="query-controls-author-select"
authorList={ authorList }
label={ __( 'Author' ) }
noOptionLabel={ __( 'All' ) }
selectedAuthorId={ selectedAuthorId }
onChange={ onAuthorChange }
/>
),
onNumberOfItemsChange && (
<RangeControl
__nextHasNoMarginBottom
key="query-controls-range-control"
label={ __( 'Number of items' ) }
value={ numberOfItems }
onChange={ onNumberOfItemsChange }
min={ minItems }
max={ maxItems }
required
/>
),
] }
</VStack>
);
}
export default QueryControls;