Skip to content

Commit

Permalink
Blocks: Update ToggleControl to pass boolean onChange (#4720)
Browse files Browse the repository at this point in the history
* Blocks: Update ToggleControl to pass boolean onChange

* Blocks: Simply ToggleControl help describedby assignment
  • Loading branch information
aduth committed Jan 30, 2018
1 parent b5e2ae7 commit 0d974c8
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 11 deletions.
48 changes: 37 additions & 11 deletions blocks/inspector-controls/toggle-control/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { withInstanceId, FormToggle } from '@wordpress/components';

/**
Expand All @@ -9,19 +10,44 @@ import { withInstanceId, FormToggle } from '@wordpress/components';
import BaseControl from './../base-control';
import './style.scss';

function ToggleControl( { label, checked, help, instanceId, onChange } ) {
const id = 'inspector-toggle-control-' + instanceId;
class ToggleControl extends Component {
constructor() {
super( ...arguments );

return (
<BaseControl label={ label } id={ id } help={ help } className="blocks-toggle-control">
<FormToggle
this.onChange = this.onChange.bind( this );
}

onChange( event ) {
if ( this.props.onChange ) {
this.props.onChange( event.target.checked );
}
}

render() {
const { label, checked, help, instanceId } = this.props;
const id = 'inspector-toggle-control-' + instanceId;

let describedBy;
if ( help ) {
describedBy = id + '__help';
}

return (
<BaseControl
label={ label }
id={ id }
checked={ checked }
onChange={ onChange }
aria-describedby={ !! help ? id + '__help' : undefined }
/>
</BaseControl>
);
help={ help }
className="blocks-toggle-control"
>
<FormToggle
id={ id }
checked={ checked }
onChange={ this.onChange }
aria-describedby={ describedBy }
/>
</BaseControl>
);
}
}

export default withInstanceId( ToggleControl );
43 changes: 43 additions & 0 deletions blocks/inspector-controls/toggle-control/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* External dependencies
*/
import { mount } from 'enzyme';

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

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

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

expect( onChange ).toHaveBeenCalledWith( true );
} );

describe( 'help', () => {
it( 'does not render input with describedby if no help prop', () => {
// Mount: With shallow, cannot find input child of BaseControl
const onChange = jest.fn();
const wrapper = mount( <ToggleControl onChange={ onChange } /> );

const input = wrapper.find( 'input' );

expect( input.prop( 'aria-describedby' ) ).toBeUndefined();
} );

it( 'renders input with describedby if help prop', () => {
// Mount: With shallow, cannot find input child of BaseControl
const onChange = jest.fn();
const wrapper = mount( <ToggleControl help onChange={ onChange } /> );

const input = wrapper.find( 'input' );

expect( input.prop( 'aria-describedby' ) ).toMatch( /^inspector-toggle-control-.*__help$/ );
} );
} );
} );

0 comments on commit 0d974c8

Please sign in to comment.