Skip to content

Commit

Permalink
Lodash: Refactor a few components away from _.isEmpty() (#42468)
Browse files Browse the repository at this point in the history
* Components: Refactor away from a few _.isEmpty()

* Add changelog
  • Loading branch information
tyxla authored Jul 18, 2022
1 parent a9e8a85 commit dfc4b8b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 17 deletions.
3 changes: 3 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
- `ScrollLock`: Convert to TypeScript ([#42303](https://github.com/WordPress/gutenberg/pull/42303)).
- `TreeSelect`: Refactor away from `_.compact()` ([#42438](https://github.com/WordPress/gutenberg/pull/42438)).
- `MediaEdit`: Refactor away from `_.compact()` for mobile ([#42438](https://github.com/WordPress/gutenberg/pull/42438)).
- `BoxControl`: Refactor away from `_.isEmpty()` ([#42468](https://github.com/WordPress/gutenberg/pull/42468)).
- `RadioControl`: Refactor away from `_.isEmpty()` ([#42468](https://github.com/WordPress/gutenberg/pull/42468)).
- `SelectControl`: Refactor away from `_.isEmpty()` ([#42468](https://github.com/WordPress/gutenberg/pull/42468)).

## 19.15.0 (2022-07-13)

Expand Down
19 changes: 6 additions & 13 deletions packages/components/src/box-control/utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { isEmpty } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -145,14 +140,12 @@ export function isValuesMixed( values = {}, selectedUnits, sides = ALL_SIDES ) {
export function isValuesDefined( values ) {
return (
values !== undefined &&
! isEmpty(
Object.values( values ).filter(
// Switching units when input is empty causes values only
// containing units. This gives false positive on mixed values
// unless filtered.
( value ) => !! value && /\d/.test( value )
)
)
Object.values( values ).filter(
// Switching units when input is empty causes values only
// containing units. This gives false positive on mixed values
// unless filtered.
( value ) => !! value && /\d/.test( value )
).length > 0
);
}

Expand Down
3 changes: 1 addition & 2 deletions packages/components/src/radio-control/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* External dependencies
*/
import { isEmpty } from 'lodash';
import classnames from 'classnames';
import type { ChangeEvent } from 'react';

Expand Down Expand Up @@ -65,7 +64,7 @@ export function RadioControl(
const onChangeValue = ( event: ChangeEvent< HTMLInputElement > ) =>
onChange( event.target.value );

if ( isEmpty( options ) ) {
if ( ! options?.length ) {
return null;
}

Expand Down
3 changes: 1 addition & 2 deletions packages/components/src/select-control/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* External dependencies
*/
import { isEmpty } from 'lodash';
import classNames from 'classnames';
import type { ChangeEvent, FocusEvent, ForwardedRef } from 'react';

Expand Down Expand Up @@ -59,7 +58,7 @@ function UnforwardedSelectControl(
const helpId = help ? `${ id }__help` : undefined;

// Disable reason: A select with an onchange throws a warning.
if ( isEmpty( options ) && ! children ) return null;
if ( ! options?.length && ! children ) return null;

const handleOnBlur = ( event: FocusEvent< HTMLSelectElement > ) => {
onBlur( event );
Expand Down

0 comments on commit dfc4b8b

Please sign in to comment.