@wordpress/edit-post
Version:
Edit Post module for WordPress.
96 lines (84 loc) • 2.79 kB
JavaScript
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import { createElement } from "@wordpress/element";
/**
* External dependencies
*/
import { without, map } from 'lodash';
/**
* WordPress dependencies
*/
import { withSelect, withDispatch } from '@wordpress/data';
import { compose, withInstanceId } from '@wordpress/compose';
import { CheckboxControl } from '@wordpress/components';
/**
* Internal dependencies
*/
import BlockTypesChecklist from './checklist';
function BlockManagerCategory(_ref) {
var instanceId = _ref.instanceId,
category = _ref.category,
blockTypes = _ref.blockTypes,
hiddenBlockTypes = _ref.hiddenBlockTypes,
toggleVisible = _ref.toggleVisible,
toggleAllVisible = _ref.toggleAllVisible;
if (!blockTypes.length) {
return null;
}
var checkedBlockNames = without.apply(void 0, [map(blockTypes, 'name')].concat(_toConsumableArray(hiddenBlockTypes)));
var titleId = 'edit-post-manage-blocks-modal__category-title-' + instanceId;
var isAllChecked = checkedBlockNames.length === blockTypes.length;
var ariaChecked;
if (isAllChecked) {
ariaChecked = 'true';
} else if (checkedBlockNames.length > 0) {
ariaChecked = 'mixed';
} else {
ariaChecked = 'false';
}
return createElement("div", {
role: "group",
"aria-labelledby": titleId,
className: "edit-post-manage-blocks-modal__category"
}, createElement(CheckboxControl, {
checked: isAllChecked,
onChange: toggleAllVisible,
className: "edit-post-manage-blocks-modal__category-title",
"aria-checked": ariaChecked,
label: createElement("span", {
id: titleId
}, category.title)
}), createElement(BlockTypesChecklist, {
blockTypes: blockTypes,
value: checkedBlockNames,
onItemChange: toggleVisible
}));
}
export default compose([withInstanceId, withSelect(function (select) {
var _select = select('core/edit-post'),
getPreference = _select.getPreference;
return {
hiddenBlockTypes: getPreference('hiddenBlockTypes')
};
}), withDispatch(function (dispatch, ownProps) {
var _dispatch = dispatch('core/edit-post'),
showBlockTypes = _dispatch.showBlockTypes,
hideBlockTypes = _dispatch.hideBlockTypes;
return {
toggleVisible: function toggleVisible(blockName, nextIsChecked) {
if (nextIsChecked) {
showBlockTypes(blockName);
} else {
hideBlockTypes(blockName);
}
},
toggleAllVisible: function toggleAllVisible(nextIsChecked) {
var blockNames = map(ownProps.blockTypes, 'name');
if (nextIsChecked) {
showBlockTypes(blockNames);
} else {
hideBlockTypes(blockNames);
}
}
};
})])(BlockManagerCategory);
//# sourceMappingURL=category.js.map