Skip to content

Commit

Permalink
Merge pull request #1957 from WordPress/add/991-page-attributes
Browse files Browse the repository at this point in the history
Add page attributes menu order to sidebar
  • Loading branch information
aduth committed Jul 26, 2017
2 parents 44ad46a + f6921ec commit b61a3b7
Show file tree
Hide file tree
Showing 16 changed files with 248 additions and 80 deletions.
1 change: 1 addition & 0 deletions components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export { default as NoticeList } from './notice/list';
export { default as Panel } from './panel';
export { default as PanelHeader } from './panel/header';
export { default as PanelBody } from './panel/body';
export { default as PanelRow } from './panel/row';
export { default as Placeholder } from './placeholder';
export { default as ResponsiveWrapper } from './responsive-wrapper';
export { default as SandBox } from './sandbox';
Expand Down
16 changes: 16 additions & 0 deletions components/panel/row.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* External dependencies
*/
import classnames from 'classnames';

function PanelRow( { className, children } ) {
const classes = classnames( 'components-panel__row', className );

return (
<div className={ classes }>
{ children }
</div>
);
}

export default PanelRow;
11 changes: 11 additions & 0 deletions components/panel/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,14 @@
.components-panel__body-toggle-icon {
margin-right: -5px;
}

.components-panel__row {
display: flex;
justify-content: space-between;
align-items: center;
margin-top: 20px;

&:empty {
margin-top: 0;
}
}
11 changes: 5 additions & 6 deletions editor/sidebar/discussion-panel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import { connect } from 'react-redux';
* WordPress dependencies
*/
import { __ } from 'i18n';
import { PanelBody, FormToggle, withInstanceId } from 'components';
import { PanelBody, PanelRow, FormToggle, withInstanceId } from 'components';

/**
* Internal Dependencies
*/
import './style.scss';
import { getEditedPostAttribute } from '../../selectors';
import { editPost } from '../../actions';

Expand All @@ -25,24 +24,24 @@ function DiscussionPanel( { pingStatus = 'open', commentStatus = 'open', instanc

return (
<PanelBody title={ __( 'Discussion' ) } initialOpen={ false }>
<div className="editor-discussion-panel__row">
<PanelRow>
<label htmlFor={ commentsToggleId }>{ __( 'Allow Comments' ) }</label>
<FormToggle
checked={ commentStatus === 'open' }
onChange={ onToggleComments }
showHint={ false }
id={ commentsToggleId }
/>
</div>
<div className="editor-discussion-panel__row">
</PanelRow>
<PanelRow>
<label htmlFor={ pingbacksToggleId }>{ __( 'Allow Pingbacks & Trackbacks' ) }</label>
<FormToggle
checked={ pingStatus === 'open' }
onChange={ onTogglePingback }
showHint={ false }
id={ pingbacksToggleId }
/>
</div>
</PanelRow>
</PanelBody>
);
}
Expand Down
6 changes: 0 additions & 6 deletions editor/sidebar/discussion-panel/style.scss

This file was deleted.

109 changes: 109 additions & 0 deletions editor/sidebar/page-attributes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* External dependencies
*/
import { connect } from 'react-redux';

/**
* WordPress dependencies
*/
import { __ } from 'i18n';
import { Component } from 'element';
import { PanelBody, PanelRow, withInstanceId } from 'components';

/**
* Internal dependencies
*/
import { editPost } from '../../actions';
import { getCurrentPostType, getEditedPostAttribute } from '../../selectors';

export class PageAttributes extends Component {
constructor() {
super( ...arguments );

this.setUpdatedOrder = this.setUpdatedOrder.bind( this );

this.state = {
supportsPageAttributes: false,
};
}

componentDidMount() {
this.fetchSupports();
}

fetchSupports() {
const { postTypeSlug } = this.props;
this.fetchSupportsRequest = new wp.api.models.Type( { id: postTypeSlug } )
.fetch( { data: { context: 'edit' } } )
.done( ( postType ) => {
const {
'page-attributes': supportsPageAttributes = false,
} = postType.supports;

this.setState( { supportsPageAttributes } );
} );
}

componentWillUnmount() {
if ( this.fetchSupportsRequest ) {
this.fetchSupportsRequest.abort();
}
}

setUpdatedOrder( event ) {
const order = Number( event.target.value );
if ( order >= 0 ) {
this.props.onUpdateOrder( order );
}
}

render() {
const { instanceId, order } = this.props;
const { supportsPageAttributes } = this.state;

// Only render fields if post type supports page attributes
if ( ! supportsPageAttributes ) {
return null;
}

// Create unique identifier for inputs
const inputId = `editor-page-attributes__order-${ instanceId }`;

return (
<PanelBody
title={ __( 'Page Attributes' ) }
initialOpen={ false }
>
<PanelRow>
<label htmlFor={ inputId }>
{ __( 'Order' ) }
</label>
<input
type="text"
value={ order || 0 }
onChange={ this.setUpdatedOrder }
id={ inputId }
size={ 6 } />
</PanelRow>
</PanelBody>
);
}
}

export default connect(
( state ) => {
return {
postTypeSlug: getCurrentPostType( state ),
order: getEditedPostAttribute( state, 'menu_order' ),
};
},
( dispatch ) => {
return {
onUpdateOrder( order ) {
dispatch( editPost( {
menu_order: order,
} ) );
},
};
}
)( withInstanceId( PageAttributes ) );
68 changes: 68 additions & 0 deletions editor/sidebar/page-attributes/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* External dependencies
*/
import { shallow } from 'enzyme';

/**
* Internal dependencies
*/
import { PageAttributes } from '../';

describe( 'PageAttributes', () => {
let fetchSupports;
beforeEach( () => {
fetchSupports = PageAttributes.prototype.fetchSupports;
PageAttributes.prototype.fetchSupports = jest.fn();
} );

afterEach( () => {
PageAttributes.prototype.fetchSupports = fetchSupports;
} );

it( 'should not render anything if no page attributes support', () => {
const wrapper = shallow( <PageAttributes /> );

expect( wrapper.type() ).toBe( null );
} );

it( 'should render input if page attributes support', () => {
const wrapper = shallow( <PageAttributes /> );
wrapper.setState( { supportsPageAttributes: true } );

expect( wrapper.type() ).not.toBe( null );
} );

it( 'should reject invalid input', () => {
const onUpdateOrder = jest.fn();
const wrapper = shallow( <PageAttributes onUpdateOrder={ onUpdateOrder } /> );
wrapper.setState( { supportsPageAttributes: true } );

wrapper.find( 'input' ).simulate( 'change', {
target: {
value: -1,
},
} );

wrapper.find( 'input' ).simulate( 'change', {
target: {
value: 'bad',
},
} );

expect( onUpdateOrder ).not.toHaveBeenCalled();
} );

it( 'should update with valid input', () => {
const onUpdateOrder = jest.fn();
const wrapper = shallow( <PageAttributes onUpdateOrder={ onUpdateOrder } /> );
wrapper.setState( { supportsPageAttributes: true } );

wrapper.find( 'input' ).simulate( 'change', {
target: {
value: 4,
},
} );

expect( onUpdateOrder ).toHaveBeenCalledWith( 4 );
} );
} );
5 changes: 3 additions & 2 deletions editor/sidebar/post-schedule/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import moment from 'moment';
import { __ } from 'i18n';
import { Component } from 'element';
import { dateI18n, settings } from 'date';
import { PanelRow } from 'components';

/**
* Internal dependencies
Expand Down Expand Up @@ -58,7 +59,7 @@ class PostSchedule extends Component {
);

return (
<div className="editor-post-schedule">
<PanelRow className="editor-post-schedule">
<span>{ __( 'Publish' ) }</span>
<button
type="button"
Expand All @@ -85,7 +86,7 @@ class PostSchedule extends Component {
/>
</div>
}
</div>
</PanelRow>
);
}
}
Expand Down
3 changes: 0 additions & 3 deletions editor/sidebar/post-schedule/style.scss
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
@import '~react-datepicker/dist/react-datepicker';

.editor-post-schedule {
display: flex;
justify-content: space-between;
align-items: center;
width: 100%;
position: relative;
}
Expand Down
38 changes: 14 additions & 24 deletions editor/sidebar/post-settings/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { connect } from 'react-redux';

/**
* WordPress dependencies
*/
Expand All @@ -19,24 +14,19 @@ import FeaturedImage from '../featured-image';
import DiscussionPanel from '../discussion-panel';
import LastRevision from '../last-revision';
import TableOfContents from '../table-of-contents';
import PageAttributes from '../page-attributes';

const PostSettings = () => {
return (
<Panel>
<PostStatus />
<LastRevision />
<PostTaxonomies />
<FeaturedImage />
<PostExcerpt />
<DiscussionPanel />
<TableOfContents />
</Panel>
);
};
const panel = (
<Panel>
<PostStatus />
<LastRevision />
<PostTaxonomies />
<FeaturedImage />
<PostExcerpt />
<DiscussionPanel />
<PageAttributes />
<TableOfContents />
</Panel>
);

export default connect(
undefined,
( dispatch ) => ( {
toggleSidebar: () => dispatch( { type: 'TOGGLE_SIDEBAR' } ),
} )
)( PostSettings );
export default () => panel;
Loading

0 comments on commit b61a3b7

Please sign in to comment.