react-component / react-component/menu

remove the annoying key duplication warning

Open
#743 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
701
Forks
266
Avg merge
4d 11h
Merged PRs (30d)
3

Description

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch rc-menu@9.16.0 for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/rc-menu/es/hooks/useKeyRecords.js b/node_modules/rc-menu/es/hooks/useKeyRecords.js
index 1afe5c8..d09e433 100644
--- a/node_modules/rc-menu/es/hooks/useKeyRecords.js
+++ b/node_modules/rc-menu/es/hooks/useKeyRecords.js
@@ -1,17 +1,17 @@
 import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
 import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
-import * as React from 'react';
-import { useRef, useCallback } from 'react';
+import * as React from "react";
+import { useRef, useCallback } from "react";
 import warning from "rc-util/es/warning";
 import { nextSlice } from "../utils/timeUtil";
-var PATH_SPLIT = '__RC_UTIL_PATH_SPLIT__';
+var PATH_SPLIT = "__RC_UTIL_PATH_SPLIT__";
 var getPathStr = function getPathStr(keyPath) {
   return keyPath.join(PATH_SPLIT);
 };
 var getPathKeys = function getPathKeys(keyPathStr) {
   return keyPathStr.split(PATH_SPLIT);
 };
-export var OVERFLOW_KEY = 'rc-menu-more';
+export var OVERFLOW_KEY = "rc-menu-more";
 export default function useKeyRecords() {
   var _React$useState = React.useState({}),
     _React$useState2 = _slicedToArray(_React$useState, 2),
@@ -31,9 +31,9 @@ export default function useKeyRecords() {
   };
   var registerPath = useCallback(function (key, keyPath) {
     // Warning for invalidate or duplicated `key`
-    if (process.env.NODE_ENV !== 'production') {
-      warning(!key2pathRef.current.has(key), "Duplicated key '".concat(key, "' used in Menu by path [").concat(keyPath.join(' > '), "]"));
-    }
+    // if (process.env.NODE_ENV !== 'production') {
+    //   warning(!key2pathRef.current.has(key), "Duplicated key '".concat(key, "' used in Menu by path [").concat(keyPath.join(' > '), "]"));
+    // }
 
     // Fill map
     var connectedPath = getPathStr(keyPath);
@@ -55,22 +55,30 @@ export default function useKeyRecords() {
   var refreshOverflowKeys = useCallback(function (keys) {
     setOverflowKeys(keys);
   }, []);
-  var getKeyPath = useCallback(function (eventKey, includeOverflow) {
-    var fullPath = key2pathRef.current.get(eventKey) || '';
-    var keys = getPathKeys(fullPath);
-    if (includeOverflow && overflowKeys.includes(keys[0])) {
-      keys.unshift(OVERFLOW_KEY);
-    }
-    return keys;
-  }, [overflowKeys]);
-  var isSubPathKey = useCallback(function (pathKeys, eventKey) {
-    return pathKeys.filter(function (item) {
-      return item !== undefined;
-    }).some(function (pathKey) {
-      var pathKeyList = getKeyPath(pathKey, true);
-      return pathKeyList.includes(eventKey);
-    });
-  }, [getKeyPath]);
+  var getKeyPath = useCallback(
+    function (eventKey, includeOverflow) {
+      var fullPath = key2pathRef.current.get(eventKey) || "";
+      var keys = getPathKeys(fullPath);
+      if (includeOverflow && overflowKeys.includes(keys[0])) {
+        keys.unshift(OVERFLOW_KEY);
+      }
+      return keys;
+    },
+    [overflowKeys],
+  );
+  var isSubPathKey = useCallback(
+    function (pathKeys, eventKey) {
+      return pathKeys
+        .filter(function (item) {
+          return item !== undefined;
+        })
+        .some(function (pathKey) {
+          var pathKeyList = getKeyPath(pathKey, true);
+          return pathKeyList.includes(eventKey);
+        });
+    },
+    [getKeyPath],
+  );
   var getKeys = function getKeys() {
     var keys = _toConsumableArray(key2pathRef.current.keys());
     if (overflowKeys.length) {
@@ -83,7 +91,9 @@ export default function useKeyRecords() {
    * Find current key related child path keys
    */
   var getSubPathKeys = useCallback(function (key) {
-    var connectedPath = "".concat(key2pathRef.current.get(key)).concat(PATH_SPLIT);
+    var connectedPath = ""
+      .concat(key2pathRef.current.get(key))
+      .concat(PATH_SPLIT);
     var pathKeys = new Set();
     _toConsumableArray(path2keyRef.current.keys()).forEach(function (pathKey) {
       if (pathKey.startsWith(connectedPath)) {
@@ -106,6 +116,6 @@ export default function useKeyRecords() {
     isSubPathKey: isSubPathKey,
     getKeyPath: getKeyPath,
     getKeys: getKeys,
-    getSubPathKeys: getSubPathKeys
+    getSubPathKeys: getSubPathKeys,
   };
 }

This issue body was partially generated by patch-package.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the source corresponding to node_modules/rc-menu/es/hooks/useKeyRecords.js and the existing duplicated-key warning. Compare the reported diff with the repository source and tests; done when the warning behavior is intentionally changed without modifying generated dependency files, with coverage for the affected menu keys.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react, typescript
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.