Don't allow unselecting all items in chart legends (https://github.com/woocommerce/woocommerce-admin/pull/427)

* Don't allow unselecting all items in chart legends

* Add correct color to chart legend labels

* Add tests for chart legend enable/disable items logic
This commit is contained in:
Albert Juhé Lluveras 2018-09-18 16:53:30 +02:00 committed by GitHub
parent f2e0165d5f
commit 9d5cd880dd
3 changed files with 51 additions and 1 deletions

View File

@ -29,6 +29,8 @@ class Legend extends Component {
orderedKeys: data,
colorScheme,
};
const numberOfRowsVisible = data.filter( row => row.visible ).length;
return (
<ul
className={ classNames(
@ -49,7 +51,11 @@ class Legend extends Component {
onBlur={ handleLegendHover }
onFocus={ handleLegendHover }
>
<button onClick={ handleLegendToggle } id={ row.key }>
<button
onClick={ handleLegendToggle }
id={ row.key }
disabled={ row.visible && numberOfRowsVisible <= 1 }
>
<div className="woocommerce-legend__item-container" id={ row.key }>
<span
className={ classNames( 'woocommerce-legend__item-checkmark', {

View File

@ -276,6 +276,7 @@
button {
background-color: $white;
color: $core-grey-dark-500;
display: inline-flex;
flex-direction: row;
flex-wrap: nowrap;

View File

@ -0,0 +1,43 @@
/**
* External dependencies
*
* @format
*/
import { shallow } from 'enzyme';
/**
* Internal dependencies
*/
import Legend from '../legend';
const colorScheme = jest.fn();
const data = [
{
key: 'lorem',
visible: true,
total: 100,
},
{
key: 'ipsum',
visible: true,
total: 100,
},
];
describe( 'Legend', () => {
test( 'should not disable any button if more than one is active', () => {
const topSellingProducts = shallow( <Legend colorScheme={ colorScheme } data={ data } /> );
expect( topSellingProducts.find( 'button' ).get( 0 ).props.disabled ).toBeFalsy();
expect( topSellingProducts.find( 'button' ).get( 1 ).props.disabled ).toBeFalsy();
} );
test( 'should disable the last active button', () => {
data[ 1 ].visible = false;
const topSellingProducts = shallow( <Legend colorScheme={ colorScheme } data={ data } /> );
expect( topSellingProducts.find( 'button' ).get( 0 ).props.disabled ).toBeTruthy();
expect( topSellingProducts.find( 'button' ).get( 1 ).props.disabled ).toBeFalsy();
} );
} );