Skip to content

Commit

Permalink
Fix: Page Attributes: Order field does not allow negative numbers (#9803
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jorgefilipecosta authored Sep 18, 2018
1 parent f9ea5be commit bd5910b
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 32 deletions.
60 changes: 35 additions & 25 deletions packages/editor/src/components/page-attributes/order.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,52 @@
/**
* External dependencies
*/
import { invoke } from 'lodash';

/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Fragment } from '@wordpress/element';
import { TextControl } from '@wordpress/components';
import { withSelect, withDispatch } from '@wordpress/data';
import { compose, withInstanceId } from '@wordpress/compose';
import { compose, withState } from '@wordpress/compose';

/**
* Internal dependencies
*/
import PostTypeSupportCheck from '../post-type-support-check';

export function PageAttributesOrder( { onUpdateOrder, instanceId, order } ) {
const setUpdatedOrder = ( event ) => {
const newOrder = Number( event.target.value );
if ( newOrder >= 0 ) {
onUpdateOrder( newOrder );
}
};
// Create unique identifier for inputs
const inputId = `editor-page-attributes__order-${ instanceId }`;

return (
<Fragment>
<label htmlFor={ inputId }>
{ __( 'Order' ) }
</label>
<input
type="text"
value={ order || 0 }
export const PageAttributesOrder = withState( {
orderInput: null,
} )(
( { onUpdateOrder, order = 0, orderInput, setState } ) => {
const setUpdatedOrder = ( value ) => {
setState( {
orderInput: value,
} );
const newOrder = Number( value );
if ( Number.isInteger( newOrder ) && invoke( value, [ 'trim' ] ) !== '' ) {
onUpdateOrder( Number( value ) );
}
};
const value = orderInput === null ? order : orderInput;
return (
<TextControl
className="editor-page-attributes__order"
type="number"
label={ __( 'Order' ) }
value={ value }
onChange={ setUpdatedOrder }
id={ inputId }
size={ 6 }
onBlur={ () => {
setState( {
orderInput: null,
} );
} }
/>
</Fragment>
);
}
);
}
);

function PageAttributesOrderWithChecks( props ) {
return (
Expand All @@ -58,5 +69,4 @@ export default compose( [
} );
},
} ) ),
withInstanceId,
] )( PageAttributesOrderWithChecks );
13 changes: 13 additions & 0 deletions packages/editor/src/components/page-attributes/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,16 @@
}
margin-bottom: 10px;
}

.editor-page-attributes__order {
width: 100%;
.components-base-control__field {
display: flex;
justify-content: space-between;
align-items: center;
}

input {
width: 66px;
}
}
68 changes: 61 additions & 7 deletions packages/editor/src/components/page-attributes/test/order.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { shallow } from 'enzyme';
import { mount } from 'enzyme';

/**
* Internal dependencies
Expand All @@ -11,37 +11,91 @@ import { PageAttributesOrder } from '../order';
describe( 'PageAttributesOrder', () => {
it( 'should reject invalid input', () => {
const onUpdateOrder = jest.fn();
const wrapper = shallow(
const wrapper = mount(
<PageAttributesOrder onUpdateOrder={ onUpdateOrder } />
);

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

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

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

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

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

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

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

it( 'should update with valid input', () => {
it( 'should update with zero input', () => {
const onUpdateOrder = jest.fn();
const wrapper = shallow(
const wrapper = mount(
<PageAttributesOrder onUpdateOrder={ onUpdateOrder } />
);

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

expect( onUpdateOrder ).toHaveBeenCalledWith( 0 );
} );

it( 'should update with valid positive input', () => {
const onUpdateOrder = jest.fn();
const wrapper = mount(
<PageAttributesOrder onUpdateOrder={ onUpdateOrder } />
);

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

expect( onUpdateOrder ).toHaveBeenCalledWith( 4 );
} );

it( 'should update with valid negative input', () => {
const onUpdateOrder = jest.fn();
const wrapper = mount(
<PageAttributesOrder onUpdateOrder={ onUpdateOrder } />
);

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

expect( onUpdateOrder ).toHaveBeenCalledWith( -1 );
} );
} );

0 comments on commit bd5910b

Please sign in to comment.