Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lodash: Remove from Navigation components #41865

Merged
merged 1 commit into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"react-colorful": "^5.3.1",
"react-dates": "^21.8.0",
"reakit": "^1.3.8",
"remove-accents": "^0.4.2",
"uuid": "^8.3.0"
},
"peerDependencies": {
Expand Down
11 changes: 8 additions & 3 deletions packages/components/src/navigation/group/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 { find, uniqueId } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -16,16 +15,22 @@ import { NavigationGroupContext } from './context';
import { GroupTitleUI } from '../styles/navigation-styles';
import { useNavigationContext } from '../context';

let uniqueId = 0;

export default function NavigationGroup( { children, className, title } ) {
const [ groupId ] = useState( uniqueId( 'group-' ) );
const [ groupId ] = useState( `group-${ ++uniqueId }` );
const {
navigationTree: { items },
} = useNavigationContext();

const context = { group: groupId };

// Keep the children rendered to make sure invisible items are included in the navigation tree.
if ( ! find( items, { group: groupId, _isVisible: true } ) ) {
if (
! Object.values( items ).some(
( item ) => item.group === groupId && item._isVisible
)
) {
return (
<NavigationGroupContext.Provider value={ context }>
{ children }
Expand Down
5 changes: 3 additions & 2 deletions packages/components/src/navigation/item/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import classnames from 'classnames';
import { uniqueId } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -16,10 +15,12 @@ import { useNavigationContext } from '../context';
import { useNavigationTreeItem } from './use-navigation-tree-item';
import { ItemBaseUI } from '../styles/navigation-styles';

let uniqueId = 0;

export default function NavigationItemBase( props ) {
const { children, className, ...restProps } = props;

const [ itemId ] = useState( uniqueId( 'item-' ) );
const [ itemId ] = useState( `item-${ ++uniqueId }` );

useNavigationTreeItem( itemId, props );
const { navigationTree } = useNavigationContext();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { filter } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -49,7 +44,9 @@ function MenuTitleSearch( {
return;
}

const count = filter( items, '_isVisible' ).length;
const count = Object.values( items ).filter(
( item ) => item._isVisible
).length;
const resultsFoundMessage = sprintf(
/* translators: %d: number of results. */
_n( '%d result found.', '%d results found.', count ),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { filter } from 'lodash';

/**
* WordPress dependencies
*/
Expand All @@ -19,7 +14,9 @@ export default function NavigationSearchNoResultsFound( { search } ) {
navigationTree: { items },
} = useNavigationContext();

const resultsCount = filter( items, '_isVisible' ).length;
const resultsCount = Object.values( items ).filter(
( item ) => item._isVisible
).length;

if ( ! search || !! resultsCount ) {
return null;
Expand Down
23 changes: 13 additions & 10 deletions packages/components/src/navigation/use-navigation-tree-nodes.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { omit } from 'lodash';

/**
* WordPress dependencies
*/
Expand All @@ -13,14 +8,22 @@ export const useNavigationTreeNodes = () => {

const getNode = ( key ) => nodes[ key ];

const addNode = ( key, value ) =>
setNodes( ( original ) => ( {
const addNode = ( key, value ) => {
// eslint-disable-next-line no-unused-vars
const { children, ...newNode } = value;
Comment on lines +12 to +13
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Has this ever come up in the other Lodash removal PRs? I wonder if we should consider the ignoreRestSiblings option. I can see it has only been discussed once before in #3208 (comment).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call - and yeah! I think I introduced at least one other of these disables, and I don't particularly fancy that. Will try your suggestion in another PR, thanks 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion that we enable ignoreRestSiblings: #41897.

return setNodes( ( original ) => ( {
...original,
[ key ]: omit( value, 'children' ),
[ key ]: newNode,
} ) );
};

const removeNode = ( key ) =>
setNodes( ( original ) => omit( original, key ) );
const removeNode = ( key ) => {
return setNodes( ( original ) => {
// eslint-disable-next-line no-unused-vars
const { [ key ]: removedNode, ...remainingNodes } = original;
return remainingNodes;
} );
};

return { nodes, getNode, addNode, removeNode };
};
4 changes: 2 additions & 2 deletions packages/components/src/navigation/utils.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* External dependencies
*/
import { deburr } from 'lodash';
import removeAccents from 'remove-accents';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL! Let me check that out... oh 😄

Screenshot of the remove-accents npm package page that shows tyxla is the repo owner and it has 1.5M weekly downloads


// @see packages/block-editor/src/components/inserter/search-items.js
export const normalizeInput = ( input ) =>
deburr( input ).replace( /^\//, '' ).toLowerCase();
removeAccents( input ).replace( /^\//, '' ).toLowerCase();

export const normalizedSearch = ( title, search ) =>
-1 !== normalizeInput( title ).indexOf( normalizeInput( search ) );