@wordpress/fields 0.10.0 → 0.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -128,9 +128,7 @@ export function CreateTemplatePartModalContents( {
128
128
 
129
129
  const defaultTemplatePartAreas = useSelect(
130
130
  ( select ) =>
131
- select( coreStore ).getEntityRecord< {
132
- default_template_part_areas: Array< TemplatePartArea >;
133
- } >( 'root', '__unstableBase' )?.default_template_part_areas,
131
+ select( coreStore ).getCurrentTheme()?.default_template_part_areas,
134
132
  []
135
133
  );
136
134
 
@@ -200,59 +198,61 @@ export function CreateTemplatePartModalContents( {
200
198
  { __( 'Area' ) }
201
199
  </BaseControl.VisualLabel>
202
200
  <div className="fields-create-template-part-modal__area-radio-group">
203
- { ( defaultTemplatePartAreas ?? [] ).map( ( item ) => {
204
- const icon = getTemplatePartIcon( item.icon );
205
- return (
206
- <div
207
- key={ item.area }
208
- className="fields-create-template-part-modal__area-radio-wrapper"
209
- >
210
- <input
211
- type="radio"
212
- id={ getAreaRadioId(
213
- item.area,
214
- instanceId
215
- ) }
216
- name={ `fields-create-template-part-modal__area-${ instanceId }` }
217
- value={ item.area }
218
- checked={ area === item.area }
219
- onChange={ () => {
220
- setArea( item.area );
221
- } }
222
- aria-describedby={ getAreaRadioDescriptionId(
223
- item.area,
224
- instanceId
225
- ) }
226
- />
227
- <Icon
228
- icon={ icon }
229
- className="fields-create-template-part-modal__area-radio-icon"
230
- />
231
- <label
232
- htmlFor={ getAreaRadioId(
233
- item.area,
234
- instanceId
235
- ) }
236
- className="fields-create-template-part-modal__area-radio-label"
201
+ { ( defaultTemplatePartAreas ?? [] ).map(
202
+ ( item: TemplatePartArea ) => {
203
+ const icon = getTemplatePartIcon( item.icon );
204
+ return (
205
+ <div
206
+ key={ item.area }
207
+ className="fields-create-template-part-modal__area-radio-wrapper"
237
208
  >
238
- { item.label }
239
- </label>
240
- <Icon
241
- icon={ check }
242
- className="fields-create-template-part-modal__area-radio-checkmark"
243
- />
244
- <p
245
- className="fields-create-template-part-modal__area-radio-description"
246
- id={ getAreaRadioDescriptionId(
247
- item.area,
248
- instanceId
249
- ) }
250
- >
251
- { item.description }
252
- </p>
253
- </div>
254
- );
255
- } ) }
209
+ <input
210
+ type="radio"
211
+ id={ getAreaRadioId(
212
+ item.area,
213
+ instanceId
214
+ ) }
215
+ name={ `fields-create-template-part-modal__area-${ instanceId }` }
216
+ value={ item.area }
217
+ checked={ area === item.area }
218
+ onChange={ () => {
219
+ setArea( item.area );
220
+ } }
221
+ aria-describedby={ getAreaRadioDescriptionId(
222
+ item.area,
223
+ instanceId
224
+ ) }
225
+ />
226
+ <Icon
227
+ icon={ icon }
228
+ className="fields-create-template-part-modal__area-radio-icon"
229
+ />
230
+ <label
231
+ htmlFor={ getAreaRadioId(
232
+ item.area,
233
+ instanceId
234
+ ) }
235
+ className="fields-create-template-part-modal__area-radio-label"
236
+ >
237
+ { item.label }
238
+ </label>
239
+ <Icon
240
+ icon={ check }
241
+ className="fields-create-template-part-modal__area-radio-checkmark"
242
+ />
243
+ <p
244
+ className="fields-create-template-part-modal__area-radio-description"
245
+ id={ getAreaRadioDescriptionId(
246
+ item.area,
247
+ instanceId
248
+ ) }
249
+ >
250
+ { item.description }
251
+ </p>
252
+ </div>
253
+ );
254
+ }
255
+ ) }
256
256
  </div>
257
257
  </fieldset>
258
258
  <HStack justify="right">
@@ -28,6 +28,8 @@ import { getItemTitle } from '../../actions/utils';
28
28
  import type { BasePost } from '../../types';
29
29
  import { unlock } from '../../lock-unlock';
30
30
 
31
+ const EMPTY_ARRAY: [] = [];
32
+
31
33
  export const TemplateEdit = ( {
32
34
  data,
33
35
  field,
@@ -39,7 +41,7 @@ export const TemplateEdit = ( {
39
41
  typeof data.id === 'number' ? data.id : parseInt( data.id, 10 );
40
42
  const slug = data.slug;
41
43
 
42
- const { availableTemplates, templates } = useSelect(
44
+ const { canSwitchTemplate, templates } = useSelect(
43
45
  ( select ) => {
44
46
  const allTemplates =
45
47
  select( coreStore ).getEntityRecords< WpTemplate >(
@@ -49,7 +51,7 @@ export const TemplateEdit = ( {
49
51
  per_page: -1,
50
52
  post_type: postType,
51
53
  }
52
- ) ?? [];
54
+ ) ?? EMPTY_ARRAY;
53
55
 
54
56
  const { getHomePage, getPostsPageId } = unlock(
55
57
  select( coreStore )
@@ -63,40 +65,41 @@ export const TemplateEdit = ( {
63
65
 
64
66
  return {
65
67
  templates: allTemplates,
66
- availableTemplates: allowSwitchingTemplate
67
- ? allTemplates.filter(
68
- ( template ) =>
69
- template.is_custom &&
70
- template.slug !== data.template &&
71
- !! template.content.raw // Skip empty templates.
72
- )
73
- : [],
68
+ canSwitchTemplate: allowSwitchingTemplate,
74
69
  };
75
70
  },
76
- [ data.template, postId, postType ]
71
+ [ postId, postType ]
77
72
  );
78
73
 
79
- const templatesAsPatterns = useMemo(
80
- () =>
81
- availableTemplates.map( ( template ) => ( {
74
+ const templatesAsPatterns = useMemo( () => {
75
+ if ( ! canSwitchTemplate ) {
76
+ return [];
77
+ }
78
+ return templates
79
+ .filter(
80
+ ( template ) =>
81
+ template.is_custom &&
82
+ template.slug !== data.template &&
83
+ // Skip empty templates.
84
+ !! template.content.raw
85
+ )
86
+ .map( ( template ) => ( {
82
87
  name: template.slug,
83
88
  blocks: parse( template.content.raw ),
84
89
  title: decodeEntities( template.title.rendered ),
85
90
  id: template.id,
86
- } ) ),
87
- [ availableTemplates ]
88
- );
91
+ } ) );
92
+ }, [ canSwitchTemplate, data.template, templates ] );
89
93
 
90
94
  const shownTemplates = useAsyncList( templatesAsPatterns );
91
95
 
92
96
  const value = field.getValue( { item: data } );
97
+ const foundTemplate = templates.find(
98
+ ( template ) => template.slug === value
99
+ );
93
100
 
94
101
  const currentTemplate = useSelect(
95
102
  ( select ) => {
96
- const foundTemplate = templates?.find(
97
- ( template ) => template.slug === value
98
- );
99
-
100
103
  if ( foundTemplate ) {
101
104
  return foundTemplate;
102
105
  }
@@ -128,7 +131,7 @@ export const TemplateEdit = ( {
128
131
  );
129
132
  }
130
133
  },
131
- [ postType, slug, templates, value ]
134
+ [ foundTemplate, postType, slug ]
132
135
  );
133
136
 
134
137
  const [ showModal, setShowModal ] = useState( false );