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

Refactor ToggleControl to use functional component #23116

Merged
merged 1 commit into from
Jun 15, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`core/more/edit should match snapshot when noTeaser is false 1`] = `
<React.Fragment>
<InspectorControls>
<ForwardRef(PanelBody)>
<WithInstanceId(ToggleControl)
<ToggleControl
checked={false}
help={[Function]}
label="Hide the excerpt on the full content page"
Expand Down Expand Up @@ -34,7 +34,7 @@ exports[`core/more/edit should match snapshot when noTeaser is true 1`] = `
<React.Fragment>
<InspectorControls>
<ForwardRef(PanelBody)>
<WithInstanceId(ToggleControl)
<ToggleControl
checked={true}
help={[Function]}
label="Hide the excerpt on the full content page"
Expand Down
78 changes: 32 additions & 46 deletions packages/components/src/toggle-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,62 +7,48 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { withInstanceId } from '@wordpress/compose';
import { useInstanceId } from '@wordpress/compose';

/**
* Internal dependencies
*/
import FormToggle from '../form-toggle';
import BaseControl from '../base-control';

class ToggleControl extends Component {
constructor() {
super( ...arguments );

this.onChange = this.onChange.bind( this );
export default function ToggleControl( {
label,
checked,
help,
className,
onChange,
} ) {
function onChangeToggle( event ) {
onChange( event.target.checked );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous onChange handler was checking whether the onChange prop was set or not. Should we do the same?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The onChange prop is required, without this prop the component will not be usable. When prop is undefined the JS engine will report an error, so the programmer will know what he did wrong.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense and it's also documented as such in the README.

}
const instanceId = useInstanceId( ToggleControl );
const id = `inspector-toggle-control-${ instanceId }`;

onChange( event ) {
if ( this.props.onChange ) {
this.props.onChange( event.target.checked );
}
let describedBy, helpLabel;
if ( help ) {
describedBy = id + '__help';
helpLabel = isFunction( help ) ? help( checked ) : help;
}

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

let describedBy, helpLabel;
if ( help ) {
describedBy = id + '__help';
helpLabel = isFunction( help ) ? help( checked ) : help;
}

return (
<BaseControl
return (
<BaseControl
id={ id }
help={ helpLabel }
className={ classnames( 'components-toggle-control', className ) }
>
<FormToggle
id={ id }
help={ helpLabel }
className={ classnames(
'components-toggle-control',
className
) }
>
<FormToggle
id={ id }
checked={ checked }
onChange={ this.onChange }
aria-describedby={ describedBy }
/>
<label
htmlFor={ id }
className="components-toggle-control__label"
>
{ label }
</label>
</BaseControl>
);
}
checked={ checked }
onChange={ onChangeToggle }
aria-describedby={ describedBy }
/>
<label htmlFor={ id } className="components-toggle-control__label">
{ label }
</label>
</BaseControl>
);
}

export default withInstanceId( ToggleControl );
22 changes: 3 additions & 19 deletions packages/components/src/toggle-control/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,12 @@ import renderer from 'react-test-renderer';
*/
import ToggleControl from '../';

/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';

// this is needed because TestUtils does not accept a stateless component.
// anything run through a HOC ends up as a stateless component.
const getTestComponent = ( WrappedComponent, props ) => {
class TestComponent extends Component {
render() {
return <WrappedComponent { ...this.props } />;
}
}
return <TestComponent { ...props } />;
};

describe( 'ToggleControl', () => {
it( 'triggers change callback with numeric value', () => {
// Mount: With shallow, cannot find input child of BaseControl
const onChange = jest.fn();
const wrapper = renderer.create(
getTestComponent( ToggleControl, { onChange } )
<ToggleControl onChange={ onChange } />
);
wrapper.root
.findByType( 'input' )
Expand All @@ -43,7 +27,7 @@ describe( 'ToggleControl', () => {
// Mount: With shallow, cannot find input child of BaseControl
const onChange = jest.fn();
const wrapper = renderer.create(
getTestComponent( ToggleControl, { onChange } )
<ToggleControl onChange={ onChange } />
);

const input = wrapper.root.findByType( 'input' );
Expand All @@ -55,7 +39,7 @@ describe( 'ToggleControl', () => {
// Mount: With shallow, cannot find input child of BaseControl
const onChange = jest.fn();
const wrapper = renderer.create(
getTestComponent( ToggleControl, { help: true, onChange } )
<ToggleControl onChange={ onChange } help={ true } />
);

const input = wrapper.root.findByType( 'input' );
Expand Down