Skip to content

Commit fcfad54

Browse files
committed
First step for Preset Post Types
1 parent 1e660d1 commit fcfad54

File tree

3 files changed

+88
-40
lines changed

3 files changed

+88
-40
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* External dependencies
3+
*/
4+
import EntitiesSearch from '@types';
5+
import React, { JSX } from 'react';
6+
7+
/**
8+
* Internal dependencies
9+
*/
10+
import { CompositeEntitiesByKind } from './composite-entities-by-kind';
11+
import { SearchControl } from './search-control';
12+
import { Set } from '../models/set';
13+
14+
type EntitiesValue = EntitiesSearch.Value;
15+
type Entities = EntitiesSearch.Entities< EntitiesValue >;
16+
type KindValue = EntitiesSearch.Kind< string > | string;
17+
18+
type Props = {
19+
entities: Entities;
20+
onChangeEntities: ( values: Entities ) => void;
21+
entitiesComponent: React.ComponentType<
22+
EntitiesSearch.BaseControl< EntitiesValue >
23+
>;
24+
kind: KindValue;
25+
kindOptions: EntitiesSearch.Options< string >;
26+
onChangeKind: ( values: KindValue ) => void;
27+
kindComponent: React.ComponentType<
28+
EntitiesSearch.BaseControl< KindValue >
29+
>;
30+
search: EntitiesSearch.SearchEntitiesFunction< EntitiesValue, KindValue >;
31+
};
32+
33+
export function PresetPostTypes( props: Props ): JSX.Element {
34+
const kindValue = ensureKindValue( props.kind );
35+
36+
return (
37+
<CompositeEntitiesByKind< EntitiesSearch.Value, string >
38+
entities={ {
39+
value: props.entities,
40+
onChange: props.onChangeEntities,
41+
} }
42+
kind={ {
43+
value: kindValue,
44+
options: props.kindOptions,
45+
onChange: props.onChangeKind,
46+
} }
47+
searchEntities={ props.search }
48+
>
49+
{ ( entities, kind, search ) => (
50+
<>
51+
<props.kindComponent { ...kind } />
52+
<SearchControl onChange={ search } />
53+
<props.entitiesComponent { ...entities } />
54+
</>
55+
) }
56+
</CompositeEntitiesByKind>
57+
);
58+
}
59+
60+
function ensureKindValue( value: KindValue ): EntitiesSearch.Kind< string > {
61+
return typeof value === 'string' ? new Set( [ value ] ) : value;
62+
}

sources/client/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export * from './api/search-entities';
22

33
export * from './components/composite-entities-by-kind';
44
export * from './components/plural-select-control';
5+
export * from './components/preset-post-types';
56
export * from './components/radio-control';
67
export * from './components/search-control';
78
export * from './components/singular-select-control';

sources/server/src/Modules/E2e/resources/js/post-types-example-block/index.js

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ document.addEventListener('DOMContentLoaded', () => {
1414
SingularSelectControl,
1515
PluralSelectControl,
1616
RadioControl,
17+
PresetPostTypes,
1718
ToggleControl,
1819
SearchControl,
1920
CompositeEntitiesByKind,
@@ -40,10 +41,32 @@ document.addEventListener('DOMContentLoaded', () => {
4041
}
4142

4243
const PostPostTypesControllerElement = createElement(
43-
CompositeEntitiesByKind,
44+
PresetPostTypes,
4445
{
46+
entities: new Set(props.attributes.posts),
47+
onChangeEntities: (posts) =>
48+
props.setAttributes({ posts: posts.toArray() }),
49+
entitiesComponent: ToggleControl,
50+
51+
kind: new Set(props.attributes.postType),
52+
kindOptions: convertEntitiesToControlOptions(
53+
postTypesEntities
54+
.records()
55+
.filter(
56+
(record) =>
57+
!UNSUPPORTED_CPTS.includes(record.slug)
58+
),
59+
'name',
60+
'slug'
61+
),
62+
onChangeKind: (postType) =>
63+
props.setAttributes({
64+
postType: postType.toArray(),
65+
}),
66+
kindComponent: ToggleControl,
67+
4568
// TODO Wrap around a throttle or debounce function
46-
searchEntities: async (
69+
search: async (
4770
phrase,
4871
postType,
4972
queryArguments
@@ -60,44 +83,6 @@ document.addEventListener('DOMContentLoaded', () => {
6083
'id'
6184
);
6285
},
63-
entities: {
64-
value: new Set(props.attributes.posts),
65-
onChange: (posts) =>
66-
props.setAttributes({ posts: posts.toArray() }),
67-
},
68-
kind: {
69-
value: new Set(props.attributes.postType),
70-
options: convertEntitiesToControlOptions(
71-
postTypesEntities
72-
.records()
73-
.filter(
74-
(record) =>
75-
!UNSUPPORTED_CPTS.includes(record.slug)
76-
),
77-
'name',
78-
'slug'
79-
),
80-
onChange: (postType) =>
81-
props.setAttributes({
82-
postType: postType.toArray(),
83-
}),
84-
},
85-
},
86-
(posts, type, search) => {
87-
return [
88-
createElement(ToggleControl, {
89-
...type,
90-
key: 'post-type-radio',
91-
}),
92-
createElement(SearchControl, {
93-
onChange: search,
94-
key: 'search',
95-
}),
96-
createElement(ToggleControl, {
97-
...posts,
98-
key: 'posts-toggle',
99-
}),
100-
];
10186
}
10287
);
10388

0 commit comments

Comments
 (0)