Skip to content

Commit

Permalink
Merge pull request #2339 from WordPress/update/range-control-numeric
Browse files Browse the repository at this point in the history
Blocks: Treat RangeControl value as numeric
  • Loading branch information
aduth authored Aug 11, 2017
2 parents 732d68b + 961c98d commit a08dae6
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 8 deletions.
9 changes: 8 additions & 1 deletion blocks/inspector-controls/range-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,17 @@ import './style.scss';

function RangeControl( { label, value, instanceId, onChange, ...props } ) {
const id = 'inspector-range-control-' + instanceId;
const onChangeValue = ( event ) => onChange( Number( event.target.value ) );

return (
<BaseControl label={ label } id={ id } className="blocks-range-control">
<input className="blocks-range-control__input" id={ id } type="range" value={ value } onChange={ onChange } { ...props } />
<input
className="blocks-range-control__input"
id={ id }
type="range"
value={ value }
onChange={ onChangeValue }
{ ...props } />
<span className="blocks-range-control__hint">{ value }</span>
</BaseControl>
);
Expand Down
23 changes: 23 additions & 0 deletions blocks/inspector-controls/range-control/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* External dependencies
*/
import { mount } from 'enzyme';

/**
* Internal dependencies
*/
import RangeControl from '../';

describe( 'RangeControl', () => {
describe( '#render()', () => {
it( 'triggers change callback with numeric value', () => {
// Mount: With shallow, cannot find input child of BaseControl
const onChange = jest.fn();
const wrapper = mount( <RangeControl onChange={ onChange } /> );

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

expect( onChange ).toHaveBeenCalledWith( 5 );
} );
} );
} );
4 changes: 2 additions & 2 deletions blocks/library/gallery/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ registerBlockType( 'core/gallery', {
edit( { attributes, setAttributes, focus, className } ) {
const { images, columns = defaultColumnsNumber( attributes ), align, imageCrop, linkTo } = attributes;
const setLinkTo = ( value ) => setAttributes( { linkTo: value } );
const setColumnsNumber = ( event ) => setAttributes( { columns: event.target.value } );
const setColumnsNumber = ( value ) => setAttributes( { columns: value } );
const updateAlignment = ( nextAlign ) => setAttributes( { align: nextAlign } );
const toggleImageCrop = () => setAttributes( { imageCrop: ! imageCrop } );

Expand Down Expand Up @@ -159,7 +159,7 @@ registerBlockType( 'core/gallery', {
label={ __( 'Columns' ) }
value={ columns }
onChange={ setColumnsNumber }
min="1"
min={ 1 }
max={ Math.min( MAX_COLUMNS, images.length ) }
/>
<ToggleControl
Expand Down
4 changes: 2 additions & 2 deletions blocks/library/latest-posts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ registerBlockType( 'core/latest-posts', {
<RangeControl
label={ __( 'Columns' ) }
value={ columns }
onChange={ ( event ) => setAttributes( { columns: event.target.value } ) }
min="2"
onChange={ ( value ) => setAttributes( { columns: value } ) }
min={ 2 }
max={ Math.min( MAX_POSTS_COLUMNS, latestPosts.length ) }
/>
}
Expand Down
6 changes: 3 additions & 3 deletions blocks/library/text-columns/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ registerBlockType( 'core/text-columns', {
<RangeControl
label={ __( 'Columns' ) }
value={ columns }
onChange={ ( event ) => setAttributes( { columns: event.target.value } ) }
min="2"
max="4"
onChange={ ( value ) => setAttributes( { columns: value } ) }
min={ 2 }
max={ 4 }
/>
</InspectorControls>
),
Expand Down

0 comments on commit a08dae6

Please sign in to comment.