| 1 |
/** |
| 2 |
* WordPress dependencies. |
| 3 |
*/ |
| 4 |
import { InspectorControls, RichText, useBlockProps } from '@wordpress/block-editor'; |
| 5 |
import { CheckboxControl, PanelBody } from '@wordpress/components'; |
| 6 |
import { useMemo } from '@wordpress/element'; |
| 7 |
import { __, sprintf } from '@wordpress/i18n'; |
| 8 |
|
| 9 |
/** |
| 10 |
* Window dependencies. |
| 11 |
*/ |
| 12 |
const { searchablePostTypes } = window.epComments; |
| 13 |
|
| 14 |
/** |
| 15 |
* Edit component. |
| 16 |
* |
| 17 |
* @param {object} props Component props. |
| 18 |
* @param {object} props.attributes Block attributes. |
| 19 |
* @param {Function} props.setAttributes Block attribute setter. |
| 20 |
* @returns {Function} Component. |
| 21 |
*/ |
| 22 |
export default ({ attributes, setAttributes }) => { |
| 23 |
const { label, postTypes } = attributes; |
| 24 |
|
| 25 |
const blockProps = useBlockProps({ |
| 26 |
className: 'ep-widget-search-comments', |
| 27 |
}); |
| 28 |
|
| 29 |
const allSelected = useMemo(() => { |
| 30 |
return postTypes.length === 0; |
| 31 |
}, [postTypes]); |
| 32 |
|
| 33 |
/** |
| 34 |
* Handle checking a post type option. |
| 35 |
* |
| 36 |
* @param {string} postType Post type slug. |
| 37 |
* @param {boolean} checked Whether post type is selected. |
| 38 |
* @returns {void} |
| 39 |
*/ |
| 40 |
const onChange = (postType, checked) => { |
| 41 |
const newPostTypes = checked |
| 42 |
? [...postTypes, postType] |
| 43 |
: postTypes.filter((v) => v !== postType); |
| 44 |
|
| 45 |
setAttributes({ postTypes: newPostTypes }); |
| 46 |
}; |
| 47 |
|
| 48 |
/** |
| 49 |
* Handle selecting all post types. |
| 50 |
* |
| 51 |
* @param {boolean} checked Whether all has been selected. |
| 52 |
* @returns {void} |
| 53 |
*/ |
| 54 |
const onSelectAll = (checked) => { |
| 55 |
if (checked) { |
| 56 |
setAttributes({ postTypes: [] }); |
| 57 |
} else { |
| 58 |
setAttributes({ postTypes: Object.keys(searchablePostTypes) }); |
| 59 |
} |
| 60 |
}; |
| 61 |
|
| 62 |
return ( |
| 63 |
<> |
| 64 |
<div {...blockProps}> |
| 65 |
<RichText |
| 66 |
aria-label={__('Label text')} |
| 67 |
placeholder={__('Add label…')} |
| 68 |
withoutInteractiveFormatting |
| 69 |
value={label} |
| 70 |
onChange={(html) => setAttributes({ label: html })} |
| 71 |
/> |
| 72 |
<input |
| 73 |
autoComplete="off" |
| 74 |
className="ep-widget-search-comments-input" |
| 75 |
disabled |
| 76 |
type="search" |
| 77 |
/> |
| 78 |
</div> |
| 79 |
<InspectorControls> |
| 80 |
<PanelBody title={__('Search settings', 'elasticpress')}> |
| 81 |
<CheckboxControl |
| 82 |
checked={allSelected} |
| 83 |
label={__('Search all comments', 'elasticpress')} |
| 84 |
onChange={onSelectAll} |
| 85 |
/> |
| 86 |
{Object.entries(searchablePostTypes).map(([postType, postTypeLabel]) => { |
| 87 |
const label = sprintf( |
| 88 |
/* translators: %s: Post type label, plural. */ |
| 89 |
__('Search comments on %s', 'elasticpress'), |
| 90 |
postTypeLabel, |
| 91 |
); |
| 92 |
|
| 93 |
return ( |
| 94 |
<CheckboxControl |
| 95 |
checked={postTypes.includes(postType)} |
| 96 |
indeterminate={allSelected} |
| 97 |
label={label} |
| 98 |
onChange={(checked) => onChange(postType, checked)} |
| 99 |
key={postType} |
| 100 |
/> |
| 101 |
); |
| 102 |
})} |
| 103 |
</PanelBody> |
| 104 |
</InspectorControls> |
| 105 |
</> |
| 106 |
); |
| 107 |
}; |
| 108 |
|