-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathadd-custom-template-modal-content.js
269 lines (262 loc) · 6.38 KB
/
add-custom-template-modal-content.js
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/**
* WordPress dependencies
*/
import { useState, useMemo, useEffect } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import {
Button,
Flex,
FlexItem,
SearchControl,
TextHighlight,
__experimentalText as Text,
__experimentalVStack as VStack,
__unstableComposite as Composite,
__unstableUseCompositeState as useCompositeState,
__unstableCompositeItem as CompositeItem,
} from '@wordpress/components';
import { useEntityRecords } from '@wordpress/core-data';
import { decodeEntities } from '@wordpress/html-entities';
/**
* Internal dependencies
*/
import useDebouncedInput from '../../utils/use-debounced-input';
import { mapToIHasNameAndId } from './utils';
const EMPTY_ARRAY = [];
function SuggestionListItem( {
suggestion,
search,
onSelect,
entityForSuggestions,
composite,
} ) {
const baseCssClass =
'edit-site-custom-template-modal__suggestions_list__list-item';
return (
<CompositeItem
role="option"
as={ Button }
{ ...composite }
className={ baseCssClass }
onClick={ () =>
onSelect(
entityForSuggestions.config.getSpecificTemplate(
suggestion
)
)
}
>
<Text
size="body"
lineHeight={ 1.53846153846 } // 20px
weight={ 500 }
className={ `${ baseCssClass }__title` }
>
<TextHighlight
text={ decodeEntities( suggestion.name ) }
highlight={ search }
/>
</Text>
{ suggestion.link && (
<Text
size="body"
lineHeight={ 1.53846153846 } // 20px
className={ `${ baseCssClass }__info` }
>
{ suggestion.link }
</Text>
) }
</CompositeItem>
);
}
function useSearchSuggestions( entityForSuggestions, search ) {
const { config } = entityForSuggestions;
const query = useMemo(
() => ( {
order: 'asc',
context: 'view',
search,
per_page: search ? 20 : 10,
...config.queryArgs( search ),
} ),
[ search, config ]
);
const { records: searchResults, hasResolved: searchHasResolved } =
useEntityRecords(
entityForSuggestions.type,
entityForSuggestions.slug,
query
);
const [ suggestions, setSuggestions ] = useState( EMPTY_ARRAY );
useEffect( () => {
if ( ! searchHasResolved ) return;
let newSuggestions = EMPTY_ARRAY;
if ( searchResults?.length ) {
newSuggestions = searchResults;
if ( config.recordNamePath ) {
newSuggestions = mapToIHasNameAndId(
newSuggestions,
config.recordNamePath
);
}
}
// Update suggestions only when the query has resolved, so as to keep
// the previous results in the UI.
setSuggestions( newSuggestions );
}, [ searchResults, searchHasResolved ] );
return suggestions;
}
function SuggestionList( { entityForSuggestions, onSelect } ) {
const composite = useCompositeState( { orientation: 'vertical' } );
const [ search, setSearch, debouncedSearch ] = useDebouncedInput();
const suggestions = useSearchSuggestions(
entityForSuggestions,
debouncedSearch
);
const { labels } = entityForSuggestions;
const [ showSearchControl, setShowSearchControl ] = useState( false );
if ( ! showSearchControl && suggestions?.length > 9 ) {
setShowSearchControl( true );
}
return (
<>
{ showSearchControl && (
<SearchControl
__nextHasNoMarginBottom
onChange={ setSearch }
value={ search }
label={ labels.search_items }
placeholder={ labels.search_items }
/>
) }
{ !! suggestions?.length && (
<Composite
{ ...composite }
role="listbox"
className="edit-site-custom-template-modal__suggestions_list"
aria-label={ __( 'Suggestions list' ) }
>
{ suggestions.map( ( suggestion ) => (
<SuggestionListItem
key={ suggestion.slug }
suggestion={ suggestion }
search={ debouncedSearch }
onSelect={ onSelect }
entityForSuggestions={ entityForSuggestions }
composite={ composite }
/>
) ) }
</Composite>
) }
{ debouncedSearch && ! suggestions?.length && (
<Text
as="p"
className="edit-site-custom-template-modal__no-results"
>
{ labels.not_found }
</Text>
) }
</>
);
}
function AddCustomTemplateModalContent( { onSelect, entityForSuggestions } ) {
const [ showSearchEntities, setShowSearchEntities ] = useState(
entityForSuggestions.hasGeneralTemplate
);
return (
<VStack
spacing={ 4 }
className="edit-site-custom-template-modal__contents-wrapper"
alignment="left"
>
{ ! showSearchEntities && (
<>
<Text as="p">
{ __(
'Select whether to create a single template for all items or a specific one.'
) }
</Text>
<Flex
className="edit-site-custom-template-modal__contents"
gap="4"
align="initial"
>
<FlexItem
isBlock
as={ Button }
onClick={ () => {
const {
slug,
title,
description,
templatePrefix,
} = entityForSuggestions.template;
onSelect( {
slug,
title,
description,
templatePrefix,
} );
} }
>
<Text
as="span"
weight={ 500 }
lineHeight={ 1.53846153846 } // 20px
>
{ entityForSuggestions.labels.all_items }
</Text>
<Text
as="span"
lineHeight={ 1.53846153846 } // 20px
>
{
// translators: The user is given the choice to set up a template for all items of a post type or taxonomy, or just a specific one.
__( 'For all items' )
}
</Text>
</FlexItem>
<FlexItem
isBlock
as={ Button }
onClick={ () => {
setShowSearchEntities( true );
} }
>
<Text
as="span"
weight={ 500 }
lineHeight={ 1.53846153846 } // 20px
>
{ entityForSuggestions.labels.singular_name }
</Text>
<Text
as="span"
lineHeight={ 1.53846153846 } // 20px
>
{
// translators: The user is given the choice to set up a template for all items of a post type or taxonomy, or just a specific one.
__( 'For a specific item' )
}
</Text>
</FlexItem>
</Flex>
</>
) }
{ showSearchEntities && (
<>
<Text as="p">
{ __(
'This template will be used only for the specific item chosen.'
) }
</Text>
<SuggestionList
entityForSuggestions={ entityForSuggestions }
onSelect={ onSelect }
/>
</>
) }
</VStack>
);
}
export default AddCustomTemplateModalContent;