From dfc4b8b8b831c6d5e2c280b06c5871d91e66f337 Mon Sep 17 00:00:00 2001 From: Marin Atanasov <8436925+tyxla@users.noreply.github.com> Date: Mon, 18 Jul 2022 06:07:43 +0300 Subject: [PATCH] Lodash: Refactor a few components away from `_.isEmpty()` (#42468) * Components: Refactor away from a few _.isEmpty() * Add changelog --- packages/components/CHANGELOG.md | 3 +++ packages/components/src/box-control/utils.js | 19 ++++++------------- .../components/src/radio-control/index.tsx | 3 +-- .../components/src/select-control/index.tsx | 3 +-- 4 files changed, 11 insertions(+), 17 deletions(-) diff --git a/packages/components/CHANGELOG.md b/packages/components/CHANGELOG.md index bbc4625a45367f..d6cf16d4d5e81a 100644 --- a/packages/components/CHANGELOG.md +++ b/packages/components/CHANGELOG.md @@ -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) diff --git a/packages/components/src/box-control/utils.js b/packages/components/src/box-control/utils.js index 689df3d67d9851..248f5bc48b621a 100644 --- a/packages/components/src/box-control/utils.js +++ b/packages/components/src/box-control/utils.js @@ -1,8 +1,3 @@ -/** - * External dependencies - */ -import { isEmpty } from 'lodash'; - /** * WordPress dependencies */ @@ -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 ); } diff --git a/packages/components/src/radio-control/index.tsx b/packages/components/src/radio-control/index.tsx index 5a4ffe874dacf2..6b4c1df8d30e0c 100644 --- a/packages/components/src/radio-control/index.tsx +++ b/packages/components/src/radio-control/index.tsx @@ -1,7 +1,6 @@ /** * External dependencies */ -import { isEmpty } from 'lodash'; import classnames from 'classnames'; import type { ChangeEvent } from 'react'; @@ -65,7 +64,7 @@ export function RadioControl( const onChangeValue = ( event: ChangeEvent< HTMLInputElement > ) => onChange( event.target.value ); - if ( isEmpty( options ) ) { + if ( ! options?.length ) { return null; } diff --git a/packages/components/src/select-control/index.tsx b/packages/components/src/select-control/index.tsx index 3b43b66651b272..252230baefa9ab 100644 --- a/packages/components/src/select-control/index.tsx +++ b/packages/components/src/select-control/index.tsx @@ -1,7 +1,6 @@ /** * External dependencies */ -import { isEmpty } from 'lodash'; import classNames from 'classnames'; import type { ChangeEvent, FocusEvent, ForwardedRef } from 'react'; @@ -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 );