0

I have a list of item with ul li list which build through a map function, looks like -

sizeData.map((size, index) => {
    return (
        <li
            role="none"
            className="size-dropdownlist__item"
            key={sizeData.label}
            onMouseOver={() => this.sizeDataHover(size.price)}
            onFocus={() => this.sizeDataHover(size.price)}
        >
            <div className="size-dropdownlist__item____holder">
                <a
                    role="menuitem"
                    aria-setsize={size.length}
                    aria-posinset={index + 1}
                    tabIndex="0"
                    aria-label={size.label}
                    href={size.URL}
                    onClick={(e) => { this.onSizeDataSelect(e, size.ID); }}
                    className={
                        classNames('size-dropdownlist__item____holder--link',
                            {
                                'holder--link-selected': size.isSelected
                            })
                    }
                >
                    {size.label}
                </a>
            </div>
        </li>
    })
}

The HTML output like -

<ul className="size-dropdownlist">
    <li className="size-dropdownlist__item">
        <div className="size-dropdownlist__item____holder">
            <a href="#" className="size-dropdownlist__item____holder--link">A</a>
        </div>
    </li>
    <li className="size-dropdownlist__item">
        <div className="size-dropdownlist__item____holder">
            <a href="#" className="size-dropdownlist__item____holder--link">B</a>
        </div>
    </li>
    <li className="size-dropdownlist__item">
        <div className="size-dropdownlist__item____holder">
            <a href="#" className="size-dropdownlist__item____holder--link">C</a>
        </div>
    </li>
    <li className="size-dropdownlist__item">
        <div className="size-dropdownlist__item____holder">
            <a href="#" className="size-dropdownlist__item____holder--link">D</a>
        </div>
    </li>
    <li className="size-dropdownlist__item">
        <div className="size-dropdownlist__item____holder">
            <a href="#" className="size-dropdownlist__item____holder--link">E</a>
        </div>
    </li>
    <li className="size-dropdownlist__item">
        <div className="size-dropdownlist__item____holder">
            <a href="#" className="size-dropdownlist__item____holder--link">F</a>
        </div>
    </li>
</ul>

Using CSS I was set it with two rows, display: flex.

|A|B|
|C|D|
|E|F|

Now I wanted to move a selected item through keyboard like - if user move mouse pointer to A and press arrow right the B will show selected and if user press arrow down then C with selected. Similarlly if user press up arrow when on C it's move to A.

Daniel Smith
  • 1,626
  • 3
  • 29
  • 59

3 Answers3

0

As far as I know you can use tabIndex to move between elements with the Tab key. If you want to use custom keys - like the arrow keys - you should use javascript. And there is an already answered question here, which uses jQuery but you can transform it to pure javascript very easily.

Tasos
  • 1,880
  • 13
  • 19
0

I will suggest you make use of the NavLink provided by react-router-dom (mind you, react is declarative by nature and the declarative nature of react makes a lot of structures accessible because they have taken care of a lot of things and anchor tag is just one of them e.g. the navigation link you are talking about right now. So why do some navigation imperatively?).

So my advice is for you to make use of NavLink: I make use of React Router for my routing, so you can see it via here "https://reactrouter.com/web/api/NavLink"

BONUS POINT:

The best place I personally use the anchor tag (<a href="..."></a>) imperatively is when I want to design a website following w3c standard, in that situation, I try to move the user focus straight to the main page when they hit the enter keye.g:

<a href="#main">Skip to main content</a>
Dharman
  • 30,962
  • 25
  • 85
  • 135
Emmanuel Onah
  • 580
  • 4
  • 8