Skip to content

Commit 80fe9b8

Browse files
authored
Merge pull request #818 from alleyinteractive/feature/post-selector-filters
Feature/post selector filters
2 parents 857812d + 72a70cf commit 80fe9b8

File tree

6 files changed

+51
-10
lines changed

6 files changed

+51
-10
lines changed

.changeset/seven-masks-admire.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@alleyinteractive/block-editor-tools": minor
3+
---
4+
5+
Adds filters prop to PostSelector to allow custom filters or instructions in the modal.

packages/block-editor-tools/src/components/post-picker/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ results, and a custom function for fetching post data given a post ID.
2525
|----------------|----------------|----------|----------|-----------------------------------------------------------------------------------------------------------------|
2626
| allowedTypes | `[]` | No | array | Array with the post types to select from. Defaults to empty array (all types). |
2727
| className | `''` | No | string | Class name for the post picker container. |
28+
| filters | | No | JSX Node(s) | Optional JSX to display between the search field and post list in the modal, to add
29+
instructions or filters. It is up to the implementer to handle changes and adjust the other props (such as allowedTypes or params) accordingly. |
2830
| getPostType | | No | function | Function to retrieve the post type for a post ID. This function must return a string. |
2931
| modalTitle | `Select Post` | No | string | Title of the modal that shows posts. |
3032
| onReset | | Yes | function | Function to reset the post ID to 0. |

packages/block-editor-tools/src/components/post-picker/index.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, JSX } from 'react';
1+
import React, { useState, JSX } from 'react';
22
import styled from 'styled-components';
33

44
import {
@@ -20,6 +20,7 @@ import Post from './post';
2020
interface PostPickerProps {
2121
allowedTypes?: string[];
2222
className?: string;
23+
filters?: React.ReactNode;
2324
getPostType?: (id: number) => string;
2425
modalTitle?: string;
2526
modalFormat?: 'grid' | 'list';
@@ -61,6 +62,7 @@ const StyledNotice = styled(Notice)`
6162
const PostPicker = ({
6263
allowedTypes,
6364
className,
65+
filters,
6466
getPostType,
6567
modalTitle = __('Select Post', 'alley-scripts'),
6668
modalFormat = 'grid',
@@ -189,6 +191,7 @@ const PostPicker = ({
189191
onUpdate={onUpdate}
190192
searchRender={searchRender}
191193
suppressPostIds={suppressPostIds}
194+
filters={filters}
192195
/>
193196
) : null}
194197
</Container>

packages/block-editor-tools/src/components/post-picker/post-list.tsx

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
import {
2-
useCallback, useEffect, useState, JSX,
1+
import React, {
2+
useCallback,
3+
useEffect,
4+
useRef,
5+
useState,
6+
JSX,
37
} from 'react';
48
import styled, { css } from 'styled-components';
59
import apiFetch from '@wordpress/api-fetch';
@@ -14,6 +18,7 @@ import Post from './post';
1418

1519
interface PostListProps {
1620
baseUrl: string;
21+
filters?: React.ReactNode;
1722
format?: 'grid' | 'list';
1823
searchRender?: (post: object) => JSX.Element;
1924
selected?: number;
@@ -27,15 +32,10 @@ interface Params {
2732
}
2833

2934
const Container = styled.div`
30-
height: calc(90vh - 220px);
3135
// Allow space for focus state.
3236
margin: 0 -0.5rem;
3337
overflow-y: auto;
3438
padding: 0 0.5rem;
35-
36-
@media (min-width: 600px) {
37-
height: calc(70vh - 230px);
38-
}
3939
`;
4040

4141
const Wrapper = styled.div<{ $format?: string; }>`
@@ -126,6 +126,7 @@ const LoadMore = styled.div`
126126
*/
127127
const PostList = ({
128128
baseUrl,
129+
filters,
129130
format,
130131
searchRender,
131132
selected,
@@ -244,6 +245,12 @@ const PostList = ({
244245
};
245246
}, [getPosts, initialLoad, pathParams]);
246247

248+
useEffect(() => {
249+
handleSearchTextChange(pathParams.searchValue);
250+
}, [baseUrl]); // eslint-disable-line react-hooks/exhaustive-deps
251+
252+
const filterDiv = useRef<HTMLDivElement>(null);
253+
const modalHeight = window.innerWidth < 600 ? '95vh' : '70vh';
247254
return (
248255
<>
249256
<TextControl
@@ -253,7 +260,8 @@ const PostList = ({
253260
// @ts-ignore
254261
onChange={handleSearchTextChange}
255262
/>
256-
<Container>
263+
{filters ? <div ref={filterDiv}>{filters}</div> : null}
264+
<Container style={{ height: filterDiv.current?.offsetHeight ? `calc(${modalHeight} - ${filterDiv.current?.offsetHeight}px - 254px)` : `calc(${modalHeight} - 254px)` }}>
257265
<Wrapper $format={format}>
258266
<h2 id="post-picker-results-heading" className="screen-reader-text">
259267
{__('Search Results', 'alley-scripts')}

packages/block-editor-tools/src/components/post-picker/search-modal.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, JSX } from 'react';
1+
import React, { useState, JSX } from 'react';
22
import styled, { css } from 'styled-components';
33
import { __ } from '@wordpress/i18n';
44
import {
@@ -11,6 +11,7 @@ import PostList from './post-list';
1111
interface SearchModalProps {
1212
baseUrl: string;
1313
closeModal: () => void;
14+
filters?: React.ReactNode;
1415
format?: 'grid' | 'list';
1516
modalTitle: string;
1617
onUpdate: (id: number) => void;
@@ -50,6 +51,7 @@ const Buttons = styled.div`
5051
const SearchModal = ({
5152
baseUrl,
5253
closeModal,
54+
filters,
5355
format = 'grid',
5456
modalTitle,
5557
onUpdate,
@@ -81,6 +83,7 @@ const SearchModal = ({
8183
setSelected={setSelected}
8284
searchRender={searchRender}
8385
suppressPostIds={suppressPostIds}
86+
filters={filters}
8487
/>
8588
<Buttons>
8689
<Button

plugin/entries/slotfills/index.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ registerPlugin('alley-scripts-plugin-sidebar', {
4040
const [
4141
selectedCheckboxes, setSelectedCheckboxes,
4242
] = useState<string[]>([]); // eslint-disable-line react-hooks/rules-of-hooks
43+
const [
44+
testFilterPostTypes,
45+
setTestFilterPostTypes,
46+
] = useState<String[]>(['post']); // eslint-disable-line react-hooks/rules-of-hooks
4347

4448
const {
4549
alley_scripts_audio_picker_id: audioPickerId = '',
@@ -51,6 +55,20 @@ registerPlugin('alley-scripts-plugin-sidebar', {
5155
alley_scripts_term_selector: termSelector = [],
5256
} = meta;
5357

58+
function TestFilter() {
59+
return (
60+
<Checkboxes
61+
label={__('Filter Post Types', 'alley-scripts')}
62+
value={testFilterPostTypes}
63+
onChange={(newValue: string[]) => setTestFilterPostTypes(newValue.length ? newValue : ['post'])}
64+
options={[
65+
{ value: 'post', label: __('Posts', 'alley-scripts') },
66+
{ value: 'page', label: __('Pages', 'alley-scripts') },
67+
]}
68+
/>
69+
);
70+
}
71+
5472
return (
5573
<>
5674
<PluginSidebar
@@ -106,6 +124,8 @@ registerPlugin('alley-scripts-plugin-sidebar', {
106124
onUpdate={(id: number) => setMeta({ alley_scripts_post_picker_id: id })}
107125
onReset={() => setMeta({ alley_scripts_post_picker_id: 0 })}
108126
value={postId}
127+
filters={<TestFilter />}
128+
allowedTypes={testFilterPostTypes.length ? testFilterPostTypes : []}
109129
/>
110130
</PanelBody>
111131
<PanelBody initialOpen title={__('Post Picker (List)', 'alley-scripts')}>

0 commit comments

Comments
 (0)