Skip to content

Commit

Permalink
Used TermTreeSelect in HierarchicalTermSelector. (#5160)
Browse files Browse the repository at this point in the history
This change removes duplicate code from HierarchicalTermSelector.
  • Loading branch information
jorgefilipecosta authored Feb 21, 2018
1 parent aca2c3d commit 3f43e32
Showing 1 changed file with 11 additions and 31 deletions.
42 changes: 11 additions & 31 deletions editor/components/post-taxonomies/hierarchical-term-selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
* External dependencies
*/
import { connect } from 'react-redux';
import { get, unescape as unescapeString, without, map, repeat, find, some } from 'lodash';
import { get, unescape as unescapeString, without, find, some } from 'lodash';

/**
* WordPress dependencies
*/
import { __, _x, sprintf } from '@wordpress/i18n';
import { Component, compose } from '@wordpress/element';
import { withAPIData, withInstanceId, withSpokenMessages } from '@wordpress/components';
import { TreeSelect, withAPIData, withInstanceId, withSpokenMessages } from '@wordpress/components';
import { buildTermsTree } from '@wordpress/utils';

/**
Expand Down Expand Up @@ -60,8 +60,8 @@ class HierarchicalTermSelector extends Component {
this.setState( { formName: newValue } );
}

onChangeFormParent( event ) {
this.setState( { formParent: event.target.value } );
onChangeFormParent( newParent ) {
this.setState( { formParent: newParent } );
}

onToggleForm() {
Expand Down Expand Up @@ -213,15 +213,6 @@ class HierarchicalTermSelector extends Component {
} );
}

renderParentSelectorOptions( terms, level = 0 ) {
return map( terms, ( term ) => ( [
<option key={ term.id } value={ term.id }>
{ repeat( '\u00A0', level * 3 ) + unescapeString( term.name ) }
</option>,
...this.renderParentSelectorOptions( term.children, level + 1 ),
] ) );
}

render() {
const { label, slug, taxonomy, instanceId } = this.props;
const { availableTermsTree, availableTerms, formName, formParent, loading, showForm } = this.state;
Expand All @@ -248,7 +239,6 @@ class HierarchicalTermSelector extends Component {
const noParentOption = `— ${ parentSelectLabel } —`;
const newTermSubmitLabel = newTermButtonLabel;
const inputId = `editor-post-taxonomies__hierarchical-terms-input-${ instanceId }`;
const selectId = `editor-post-taxonomies__hierarchical-terms-select-${ instanceId }`;

/* eslint-disable jsx-a11y/no-onchange */
return (
Expand Down Expand Up @@ -281,23 +271,13 @@ class HierarchicalTermSelector extends Component {
required
/>
{ !! availableTerms.length &&
<div>
<label
htmlFor={ selectId }
className="editor-post-taxonomies__hierarchical-terms-label"
>
{ parentSelectLabel }
</label>
<select
id={ selectId }
className="editor-post-taxonomies__hierarchical-terms-input"
value={ formParent }
onChange={ this.onChangeFormParent }
>
<option value="">{ noParentOption }</option>
{ this.renderParentSelectorOptions( availableTermsTree ) }
</select>
</div>
<TreeSelect
label={ parentSelectLabel }
noOptionLabel={ noParentOption }
onChange={ this.onChangeFormParent }
selectedId={ formParent }
tree={ availableTermsTree }
/>
}
<button
type="submit"
Expand Down

0 comments on commit 3f43e32

Please sign in to comment.