From afe659d0b83e6b046269d57137843bd2959fe079 Mon Sep 17 00:00:00 2001 From: Marin Atanasov <8436925+tyxla@users.noreply.github.com> Date: Tue, 12 Jul 2022 12:09:25 +0300 Subject: [PATCH] Components: Remove Lodash from `DropdownMenu` (#42218) * Components: Remove Lodash from DropdownMenu * Add changelog * Use optional chaining * Use flatMap() --- packages/components/CHANGELOG.md | 1 + packages/components/src/dropdown-menu/index.js | 7 +++---- packages/components/src/dropdown-menu/index.native.js | 8 +++----- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/packages/components/CHANGELOG.md b/packages/components/CHANGELOG.md index ee87809f1d1e68..517f5e3dd33409 100644 --- a/packages/components/CHANGELOG.md +++ b/packages/components/CHANGELOG.md @@ -24,6 +24,7 @@ - `ComboboxControl`: Refactor away from `_.deburr()` ([#42169](https://github.com/WordPress/gutenberg/pull/42169/)). - `FormTokenField`: Refactor away from `_.identity()` ([#42215](https://github.com/WordPress/gutenberg/pull/42215/)). - `SelectControl`: Use roles and `@testing-library/user-event` in unit tests ([#42308](https://github.com/WordPress/gutenberg/pull/42308)). +- `DropdownMenu`: Refactor away from Lodash ([#42218](https://github.com/WordPress/gutenberg/pull/42218/)). - `ToolbarGroup`: Refactor away from `_.flatMap()` ([#42223](https://github.com/WordPress/gutenberg/pull/42223/)). - `TreeSelect`: Refactor away from `_.flatMap()` ([#42223](https://github.com/WordPress/gutenberg/pull/42223/)). - `Autocomplete`: Refactor away from `_.deburr()` ([#42266](https://github.com/WordPress/gutenberg/pull/42266/)). diff --git a/packages/components/src/dropdown-menu/index.js b/packages/components/src/dropdown-menu/index.js index eff41fee62cc29..9687ecce8ebc7b 100644 --- a/packages/components/src/dropdown-menu/index.js +++ b/packages/components/src/dropdown-menu/index.js @@ -3,7 +3,6 @@ * External dependencies */ import classnames from 'classnames'; -import { flatMap, isEmpty } from 'lodash'; /** * WordPress dependencies @@ -59,13 +58,13 @@ function DropdownMenu( dropdownMenuProps ) { noIcons, } = dropdownMenuProps; - if ( isEmpty( controls ) && ! isFunction( children ) ) { + if ( ! controls?.length && ! isFunction( children ) ) { return null; } // Normalize controls to nested array of objects (sets of controls) let controlSets; - if ( ! isEmpty( controls ) ) { + if ( controls?.length ) { controlSets = controls; if ( ! Array.isArray( controlSets[ 0 ] ) ) { controlSets = [ controlSets ]; @@ -146,7 +145,7 @@ function DropdownMenu( dropdownMenuProps ) { return ( { isFunction( children ) ? children( props ) : null } - { flatMap( controlSets, ( controlSet, indexOfSet ) => + { controlSets?.flatMap( ( controlSet, indexOfSet ) => controlSet.map( ( control, indexOfControl ) => (