Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

109
Views
How to trigger onClick of focused element by pressing enter in navbar

There is a list of nav items and its focus nav item below each press TAB key, I need to fire the onClick event of the focused nav item when pressing the ENTER key.

This is what I do,

onKeyPress works as expected but do not trigger onClick

NavBar compoenent:

const Navbar = () => {
...
     return (
        <nav>
          <ul style={{ listStyleType: 'none', padding: 0, margin: 0 }}>
            {authenticatedRoutes.map((currentItem: any, index: number) => {
              const isOpen = openedDrawer === currentItem.path
              return (
                <ul
                  key={`ROUTE_LIST_${index}`}
                  style={{
                    listStyleType: 'none',
                  }}
                >
                  <li key={`ROUTE_${index}`}>
                    <div
                      onKeyPress={e => {
                       if (e.key === 'Enter') {
                             const event = new MouseEvent('click')
                             e.target.dispatchEvent(event)
                          }
                      }}
                      onClick={() => {
                        history.push(currentItem.path)
                      }}
                      role="link"
                      tabIndex={0}
                    >
                      <div style={{ display: 'flex', alignItems: 'center' }}>
                        <div
                          style={{
                            ...LEFT_BORDER,
                            backgroundColor: isOpen ? 'transparent' : 'initial',
                          }}
                        />
                        {linkText}
                      </div>
                    </div>
                  </li>
                </ul>
              )
            })}
          </ul>
        </nav>
      ) 
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can't programatically trigger native event hooks on a component easily in react.

The best solution would be to put the code in your onKeyPress as well...

onKeyPress={e => {
  if (e.key === 'Enter') {
    history.push(currentItem.path) // I Added this line
    const event = new MouseEvent('click')
    e.target.dispatchEvent(event)
  }
 }}
 onClick={() => {
   history.push(currentItem.path)
 }}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!