Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
357 views
in Technique[技术] by (71.8m points)

javascript - React context menu onClick not working on multiple context menu

I have the following code which triggers context menu, but when I do a search and get multiple results having same word, the onClick is not working, where as if I get one result with search word the onClick works fine. I tried giving unique id's to menu items as well, but still it's not working. Can you help me to figure out where is the issue?

const getHighlightText = (text, keyword, value) => {
    const startIndex = text.indexOf(keyword);
    const entryId = value.entryid;
    const entryParent = value.entryparent;
    const entryType = value.entrytype;

    return startIndex !== -1 ? (
        <span>
            {text.substring(0, startIndex)}
            <span style={{ color: '#03a0ce' }}>
                <ContextMenuTrigger id='HIGHLIGHTER_CONTEXT_MENU_1' holdToDisplay={1000}>
                    {text.substring(startIndex, startIndex + keyword.length)}
                </ContextMenuTrigger>
                <ContextMenu id='HIGHLIGHTER_CONTEXT_MENU_1' hideOnLeave={true}>
                    {(catalogType === 'M' || catalogType === 'L') ?
                      <MenuItem data={{event: 'CreateLibrary'}}
                                onClick={(event) => handleClickOnCatalogIcon(event, 'createLibrary')}
                      >
                          Create Library
                      </MenuItem> : null}
                    
                </ContextMenu>
            </span>
            {text.substring(startIndex + keyword.length)}
        </span>
    ) : (
        <span>
            <ContextMenuTrigger id='HIGHLIGHTER_CONTEXT_MENU_2' holdToDisplay={1000}>
                {text}
            </ContextMenuTrigger>
            <ContextMenu id='HIGHLIGHTER_CONTEXT_MENU_2' hideOnLeave={true}>
                    {(catalogType === 'M' || catalogType === 'L') ?
                      <MenuItem data={{event: 'CreateLibrary'}}
                                onClick={(event) => handleClickOnCatalogIcon(event, 'createLibrary')}
                      >
                          Create Library
                      </MenuItem> : null}
                </ContextMenu>
        </span>
    );
};
question from:https://stackoverflow.com/questions/66050992/react-context-menu-onclick-not-working-on-multiple-context-menu

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think the problem might be with context menu id, it should always be unique according to the documentation. Try adding a random string to the id.

const getHighlightText = (text, keyword, value) => {
const startIndex = text.indexOf(keyword);
const entryId = value.entryid;
const entryParent = value.entryparent;
const entryType = value.entrytype;

const randomString1 = `${entryId}-${btoa(Math.random().toString()).substring(0,16)}`
const randomString2 = `${entryParent}-${btoa(Math.random().toString()).substring(0,16)}`

return startIndex !== -1 ? (
    <span>
        {text.substring(0, startIndex)}
        <span style={{ color: '#03a0ce' }}>
            <ContextMenuTrigger id={randomString1} holdToDisplay={1000}>
                {text.substring(startIndex, startIndex + keyword.length)}
            </ContextMenuTrigger>
            <ContextMenu id={randomString1} hideOnLeave={true}>
                {(catalogType === 'M' || catalogType === 'L') ?
                  <MenuItem data={{event: 'CreateLibrary'}}
                            onClick={(event) => handleClickOnCatalogIcon(event, 'createLibrary')}
                  >
                      Create Library
                  </MenuItem> : null}
                
            </ContextMenu>
        </span>
        {text.substring(startIndex + keyword.length)}
    </span>
) : (
    <span>
        <ContextMenuTrigger id={randomString2} holdToDisplay={1000}>
            {text}
        </ContextMenuTrigger>
        <ContextMenu id={randomString2} hideOnLeave={true}>
                {(catalogType === 'M' || catalogType === 'L') ?
                  <MenuItem data={{event: 'CreateLibrary'}}
                            onClick={(event) => handleClickOnCatalogIcon(event, 'createLibrary')}
                  >
                      Create Library
                  </MenuItem> : null}
            </ContextMenu>
    </span>
  );
};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...