Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made each taxonomy appear on its own panel. #5183

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions components/panel/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
}
.components-panel__body.is-opened > .components-panel__body-title {
margin: -1 * $panel-padding;
margin-bottom: 0;
margin-bottom: 5px;
}

.components-panel__body-toggle.components-button {
Expand Down Expand Up @@ -131,7 +131,8 @@
max-width: 75%;
}

&:empty {
&:empty,
&:first-of-type {
margin-top: 0;
}
}
Expand Down
4 changes: 0 additions & 4 deletions edit-post/components/sidebar/block-inspector-panel/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
margin: 0 0 1em 0;
}

&.is-opened > .components-panel__body-title {
margin-bottom: 5px;
}

.components-panel__body-toggle {
color: $dark-gray-500;
}
Expand Down
21 changes: 11 additions & 10 deletions edit-post/components/sidebar/post-taxonomies/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,32 @@ import { connect } from 'react-redux';
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { PanelBody } from '@wordpress/components';
import { PostTaxonomies as PostTaxonomiesForm, PostTaxonomiesCheck } from '@wordpress/editor';

/**
* Internal dependencies
*/
import { isEditorSidebarPanelOpened } from '../../../store/selectors';
import { toggleGeneralSidebarEditorPanel } from '../../../store/actions';
import TaxonomyPanel from './taxonomy-panel';

/**
* Module Constants
*/
const PANEL_NAME = 'post-taxonomies';

function PostTaxonomies( { isOpened, onTogglePanel } ) {
function PostTaxonomies() {
return (
<PostTaxonomiesCheck>
<PanelBody
title={ __( 'Categories & Tags' ) }
opened={ isOpened }
onToggle={ onTogglePanel }
>
<PostTaxonomiesForm />
</PanelBody>
<PostTaxonomiesForm
taxonomyWrapper={ ( content, taxonomy ) => {
return (
<TaxonomyPanel taxonomy={ taxonomy }>
{ content }
</TaxonomyPanel>
);
} }
/>
</PostTaxonomiesCheck>
);
}
Expand Down
46 changes: 46 additions & 0 deletions edit-post/components/sidebar/post-taxonomies/taxonomy-panel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* External Dependencies
*/
import { get } from 'lodash';

/**
* WordPress dependencies
*/
import { compose } from '@wordpress/element';
import { PanelBody } from '@wordpress/components';
import { withSelect, withDispatch } from '@wordpress/data';

function TaxonomyPanel( { taxonomy, isOpened, onTogglePanel, children } ) {
const taxonomyMenuName = get( taxonomy, [ 'labels', 'menu_name' ] );
if ( ! taxonomyMenuName ) {
return null;
}
return (
<PanelBody
title={ taxonomyMenuName }
opened={ isOpened }
onToggle={ onTogglePanel }
>
{ children }
</PanelBody>
);
}

export default compose(
withSelect( ( select, ownProps ) => {
const slug = get( ownProps.taxonomy, [ 'slug' ] );
const panelName = slug ? `taxonomy-panel-${ slug }` : '';
return {
panelName,
isOpened: slug ?
select( 'core/edit-post' ).isEditorSidebarPanelOpened( panelName ) :
false,
};
} ),
withDispatch( ( dispatch, ownProps ) => ( {
onTogglePanel: () => {
dispatch( 'core/edit-post' ).
toggleGeneralSidebarEditorPanel( ownProps.panelName );
},
} ) ),
)( TaxonomyPanel );
35 changes: 16 additions & 19 deletions editor/components/post-taxonomies/flat-term-selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class FlatTermSelector extends Component {
}

render() {
const { label, slug, taxonomy } = this.props;
const { slug, taxonomy } = this.props;
const { loading, availableTerms, selectedTerms } = this.state;
const termNames = availableTerms.map( ( term ) => term.name );
const newTermPlaceholderLabel = get(
Expand All @@ -178,24 +178,21 @@ class FlatTermSelector extends Component {
const removeTermLabel = sprintf( _x( 'Remove %s: %%s', 'term' ), singularName );

return (
<div className="editor-post-taxonomies__flat-terms-selector">
<h3 className="editor-post-taxonomies__flat-terms-selector-title">{ label }</h3>
<FormTokenField
value={ selectedTerms }
displayTransform={ unescapeString }
suggestions={ termNames }
onChange={ this.onChange }
onInputChange={ this.searchTerms }
maxSuggestions={ MAX_TERMS_SUGGESTIONS }
disabled={ loading }
placeholder={ newTermPlaceholderLabel }
messages={ {
added: termAddedLabel,
removed: termRemovedLabel,
remove: removeTermLabel,
} }
/>
</div>
<FormTokenField
value={ selectedTerms }
displayTransform={ unescapeString }
suggestions={ termNames }
onChange={ this.onChange }
onInputChange={ this.searchTerms }
maxSuggestions={ MAX_TERMS_SUGGESTIONS }
disabled={ loading }
placeholder={ newTermPlaceholderLabel }
messages={ {
added: termAddedLabel,
removed: termRemovedLabel,
remove: removeTermLabel,
} }
/>
);
}
}
Expand Down
90 changes: 44 additions & 46 deletions editor/components/post-taxonomies/hierarchical-term-selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ class HierarchicalTermSelector extends Component {
}

render() {
const { label, slug, taxonomy, instanceId } = this.props;
const { slug, taxonomy, instanceId } = this.props;
const { availableTermsTree, availableTerms, formName, formParent, loading, showForm } = this.state;
const labelWithFallback = ( labelProperty, fallbackIsCategory, fallbackIsNotCategory ) => get(
taxonomy,
Expand All @@ -248,54 +248,52 @@ class HierarchicalTermSelector extends Component {
const inputId = `editor-post-taxonomies__hierarchical-terms-input-${ instanceId }`;

/* eslint-disable jsx-a11y/no-onchange */
return (
<div className="editor-post-taxonomies__hierarchical-terms-selector">
<h3 className="editor-post-taxonomies__hierarchical-terms-selector-title">{ label }</h3>
{ this.renderTerms( availableTermsTree ) }
{ ! loading &&
return [
...this.renderTerms( availableTermsTree ),
! loading && (
<button
key="term-add-button"
onClick={ this.onToggleForm }
className="button-link editor-post-taxonomies__hierarchical-terms-add"
aria-expanded={ showForm }
>
{ newTermButtonLabel }
</button>
),
showForm && (
<form onSubmit={ this.onAddTerm } key="hierarchical-terms-form">
<label
htmlFor={ inputId }
className="editor-post-taxonomies__hierarchical-terms-label"
>
{ newTermLabel }
</label>
<input
type="text"
id={ inputId }
className="editor-post-taxonomies__hierarchical-terms-input"
value={ formName }
onChange={ this.onChangeFormName }
required
/>
{ !! availableTerms.length &&
<TreeSelect
label={ parentSelectLabel }
noOptionLabel={ noParentOption }
onChange={ this.onChangeFormParent }
selectedId={ formParent }
tree={ availableTermsTree }
/>
}
<button
onClick={ this.onToggleForm }
className="button-link editor-post-taxonomies__hierarchical-terms-add"
aria-expanded={ showForm }
type="submit"
className="button editor-post-taxonomies__hierarchical-terms-submit"
>
{ newTermButtonLabel }
{ newTermSubmitLabel }
</button>
}
{ showForm &&
<form onSubmit={ this.onAddTerm }>
<label
htmlFor={ inputId }
className="editor-post-taxonomies__hierarchical-terms-label"
>
{ newTermLabel }
</label>
<input
type="text"
id={ inputId }
className="editor-post-taxonomies__hierarchical-terms-input"
value={ formName }
onChange={ this.onChangeFormName }
required
/>
{ !! availableTerms.length &&
<TreeSelect
label={ parentSelectLabel }
noOptionLabel={ noParentOption }
onChange={ this.onChangeFormParent }
selectedId={ formParent }
tree={ availableTermsTree }
/>
}
<button
type="submit"
className="button editor-post-taxonomies__hierarchical-terms-submit"
>
{ newTermSubmitLabel }
</button>
</form>
}
</div>
);
</form>
),
];
/* eslint-enable jsx-a11y/no-onchange */
}
}
Expand Down
38 changes: 19 additions & 19 deletions editor/components/post-taxonomies/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
* External Dependencies
*/
import { connect } from 'react-redux';
import { filter, includes } from 'lodash';
import { filter, identity, includes } from 'lodash';

/**
* WordPress dependencies
*/
import { withAPIData } from '@wordpress/components';
import { compose } from '@wordpress/element';
import { compose, Fragment } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -18,24 +18,24 @@ import HierarchicalTermSelector from './hierarchical-term-selector';
import FlatTermSelector from './flat-term-selector';
import { getCurrentPostType } from '../../store/selectors';

export function PostTaxonomies( { postType, taxonomies } ) {
export function PostTaxonomies( { postType, taxonomies, taxonomyWrapper = identity } ) {
const availableTaxonomies = filter( taxonomies.data, ( taxonomy ) => includes( taxonomy.types, postType ) );

return (
<div>
{ availableTaxonomies.map( ( taxonomy ) => {
const TaxonomyComponent = taxonomy.hierarchical ? HierarchicalTermSelector : FlatTermSelector;
return (
<TaxonomyComponent
key={ taxonomy.slug }
label={ taxonomy.name }
restBase={ taxonomy.rest_base }
slug={ taxonomy.slug }
/>
);
} ) }
</div>
);
return availableTaxonomies.map( ( taxonomy ) => {
const TaxonomyComponent = taxonomy.hierarchical ? HierarchicalTermSelector : FlatTermSelector;
return (
<Fragment key={ `taxonomy-${ taxonomy.slug }` }>
{
taxonomyWrapper(
<TaxonomyComponent
restBase={ taxonomy.rest_base }
slug={ taxonomy.slug }
/>,
taxonomy
)
}
</Fragment>
);
} );
}

const applyConnect = connect(
Expand Down
11 changes: 0 additions & 11 deletions editor/components/post-taxonomies/style.scss
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
.editor-post-taxonomies__flat-terms-selector,
.editor-post-taxonomies__hierarchical-terms-selector {
margin-top: $panel-padding;
}

.editor-sidebar .editor-post-taxonomies__flat-terms-selector-title,
.editor-sidebar .editor-post-taxonomies__hierarchical-terms-selector-title {
display: block;
margin-bottom: 10px;
}

.editor-post-taxonomies__hierarchical-terms-choice {
margin-bottom: 5px;
}
Expand Down
Loading