Skip to content

Commit

Permalink
Components: Remove Lodash from DropdownMenu (#42218)
Browse files Browse the repository at this point in the history
* Components: Remove Lodash from DropdownMenu

* Add changelog

* Use optional chaining

* Use flatMap()
  • Loading branch information
tyxla authored Jul 12, 2022
1 parent 4719b98 commit afe659d
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)).
Expand Down
7 changes: 3 additions & 4 deletions packages/components/src/dropdown-menu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* External dependencies
*/
import classnames from 'classnames';
import { flatMap, isEmpty } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -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 ];
Expand Down Expand Up @@ -146,7 +145,7 @@ function DropdownMenu( dropdownMenuProps ) {
return (
<NavigableMenu { ...mergedMenuProps } role="menu">
{ isFunction( children ) ? children( props ) : null }
{ flatMap( controlSets, ( controlSet, indexOfSet ) =>
{ controlSets?.flatMap( ( controlSet, indexOfSet ) =>
controlSet.map( ( control, indexOfControl ) => (
<Button
key={ [
Expand Down
8 changes: 3 additions & 5 deletions packages/components/src/dropdown-menu/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import classnames from 'classnames';
import { flatMap, isEmpty } from 'lodash';
import { Platform } from 'react-native';
/**
* WordPress dependencies
Expand Down Expand Up @@ -53,13 +52,13 @@ function DropdownMenu( {
popoverProps,
toggleProps,
} ) {
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 ];
Expand Down Expand Up @@ -131,8 +130,7 @@ function DropdownMenu( {
title={ label }
style={ { paddingLeft: 0, paddingRight: 0 } }
>
{ flatMap(
controlSets,
{ controlSets?.flatMap(
( controlSet, indexOfSet ) =>
controlSet.map(
( control, indexOfControl ) => (
Expand Down

0 comments on commit afe659d

Please sign in to comment.