Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blocks: Update ToggleControl to pass boolean onChange #4720

Merged
merged 2 commits into from
Jan 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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$/ );
} );
} );
} );