Skip to content

Commit

Permalink
Lodash: Refactor components away from _.includes() (#43518)
Browse files Browse the repository at this point in the history
* Lodash: Refactor components away from _.includes()

* Add changelog
  • Loading branch information
tyxla committed Aug 23, 2022
1 parent dc2ba4b commit c9bbb3f
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 24 deletions.
5 changes: 5 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@
- `Snackbar`: Refactor away from `_.omit()` ([#43474](https://github.com/WordPress/gutenberg/pull/43474/)).
- `UnitControl`: Refactor away from `_.omit()` ([#43474](https://github.com/WordPress/gutenberg/pull/43474/)).
- `BottomSheet`: Refactor away from `_.omit()` ([#43474](https://github.com/WordPress/gutenberg/pull/43474/)).
- `DropZone`: Refactor away from `_.includes()` ([#43518](https://github.com/WordPress/gutenberg/pull/43518/)).
- `NavigableMenu`: Refactor away from `_.includes()` ([#43518](https://github.com/WordPress/gutenberg/pull/43518/)).
- `Tooltip`: Refactor away from `_.includes()` ([#43518](https://github.com/WordPress/gutenberg/pull/43518/)).
- `TreeGrid`: Refactor away from `_.includes()` ([#43518](https://github.com/WordPress/gutenberg/pull/43518/)).


### Experimental

Expand Down
5 changes: 2 additions & 3 deletions packages/components/src/drop-zone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import classnames from 'classnames';
import { includes } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -60,12 +59,12 @@ export default function DropZoneComponent( {
* From Windows Chrome 96, the `event.dataTransfer` returns both file object and HTML.
* The order of the checks is important to recognise the HTML drop.
*/
if ( includes( event.dataTransfer.types, 'text/html' ) ) {
if ( event.dataTransfer.types.includes( 'text/html' ) ) {
_type = 'html';
} else if (
// Check for the types because sometimes the files themselves
// are only available on drop.
includes( event.dataTransfer.types, 'Files' ) ||
event.dataTransfer.types.includes( 'Files' ) ||
getFilesFromDataTransfer( event.dataTransfer ).length > 0
) {
_type = 'file';
Expand Down
10 changes: 3 additions & 7 deletions packages/components/src/navigable-container/menu.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
// @ts-nocheck
/**
* External dependencies
*/
import { includes } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -35,11 +31,11 @@ export function NavigableMenu(
previous = [ LEFT, UP ];
}

if ( includes( next, keyCode ) ) {
if ( next.includes( keyCode ) ) {
return 1;
} else if ( includes( previous, keyCode ) ) {
} else if ( previous.includes( keyCode ) ) {
return -1;
} else if ( includes( [ DOWN, UP, LEFT, RIGHT ], keyCode ) ) {
} else if ( [ DOWN, UP, LEFT, RIGHT ].includes( keyCode ) ) {
// Key press should be handled, e.g. have event propagation and
// default behavior handled by NavigableContainer but not result
// in an offset.
Expand Down
6 changes: 1 addition & 5 deletions packages/components/src/tooltip/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
// @ts-nocheck
/**
* External dependencies
*/
import { includes } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -211,7 +207,7 @@ function Tooltip( props ) {
// quickly blur/mouseleave before delayedSetIsOver is called.
delayedSetIsOver.cancel();

const _isOver = includes( [ 'focus', 'mouseenter' ], event.type );
const _isOver = [ 'focus', 'mouseenter' ].includes( event.type );
if ( _isOver === isOver ) {
return;
}
Expand Down
13 changes: 4 additions & 9 deletions packages/components/src/tree-grid/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { includes } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -68,7 +63,7 @@ function TreeGrid(

if (
hasModifierKeyPressed ||
! includes( [ UP, DOWN, LEFT, RIGHT, HOME, END ], keyCode )
! [ UP, DOWN, LEFT, RIGHT, HOME, END ].includes( keyCode )
) {
return;
}
Expand All @@ -92,7 +87,7 @@ function TreeGrid(
activeRow.getAttribute( 'aria-expanded' ) === 'false' &&
keyCode === RIGHT;

if ( includes( [ LEFT, RIGHT ], keyCode ) ) {
if ( [ LEFT, RIGHT ].includes( keyCode ) ) {
// Calculate to the next element.
let nextIndex;
if ( keyCode === LEFT ) {
Expand Down Expand Up @@ -177,7 +172,7 @@ function TreeGrid(
// Prevent key use for anything else. This ensures Voiceover
// doesn't try to handle key navigation.
event.preventDefault();
} else if ( includes( [ UP, DOWN ], keyCode ) ) {
} else if ( [ UP, DOWN ].includes( keyCode ) ) {
// Calculate the rowIndex of the next row.
const rows = Array.from(
treeGridElement.querySelectorAll( '[role="row"]' )
Expand Down Expand Up @@ -231,7 +226,7 @@ function TreeGrid(
// Prevent key use for anything else. This ensures Voiceover
// doesn't try to handle key navigation.
event.preventDefault();
} else if ( includes( [ HOME, END ], keyCode ) ) {
} else if ( [ HOME, END ].includes( keyCode ) ) {
// Calculate the rowIndex of the next row.
const rows = Array.from(
treeGridElement.querySelectorAll( '[role="row"]' )
Expand Down

0 comments on commit c9bbb3f

Please sign in to comment.