Skip to content

Commit 2212020

Browse files
marianjuhasMarian Juhas
andauthored
feat: Set the tabIndex on tree select (#439)
Co-authored-by: Marian Juhas <[email protected]>
1 parent 3db87c9 commit 2212020

File tree

8 files changed

+42
-4
lines changed

8 files changed

+42
-4
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ A lightweight and fast control to render a select component that can display hie
6565
- [id](#id)
6666
- [searchPredicate](#searchpredicate)
6767
- [inlineSearchInput](#inlinesearchinput)
68+
- [tabIndex](#tabIndex)
6869
- [Styling and Customization](#styling-and-customization)
6970
- [Using default styles](#default-styles)
7071
- [Customizing with Bootstrap, Material Design styles](#customizing-styles)
@@ -414,6 +415,12 @@ Type: `bool` (default: `false`)
414415

415416
`inlineSearchInput=true` makes the search input renders **inside** the dropdown-content. This can be useful when your UX looks something like [this comment](https://github.com/dowjones/react-dropdown-tree-select/issues/308#issue-526467109).
416417

418+
### tabIndex
419+
420+
Type: `number` (default: `0`)
421+
422+
`tabIndex=0` attribute indicates that its element can be focused, and where it participates in sequential keyboard navigation.
423+
417424
## Styling and Customization
418425

419426
### Default styles
@@ -602,6 +609,7 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds
602609
| [<img src="https://avatars3.githubusercontent.com/u/491877?v=4" width="100px;" alt="Lutz Lengemann"/><br /><sub><b>Lutz Lengemann</b></sub>](http://www.dealzeit.de)<br />[💻](https://github.com/dowjones/react-dropdown-tree-select/commits?author=mobilutz "Code") | [<img src="https://avatars0.githubusercontent.com/u/26381655?v=4" width="100px;" alt="Akshay Dipta"/><br /><sub><b>Akshay Dipta</b></sub>](https://github.com/Eainde)<br />[🐛](https://github.com/dowjones/react-dropdown-tree-select/issues?q=author%3AEainde "Bug reports") | [<img src="https://avatars3.githubusercontent.com/u/137158?v=4" width="100px;" alt="Ian Langworth ☠"/><br /><sub><b>Ian Langworth ☠</b></sub>](https://langworth.com/)<br />[🤔](#ideas-statico "Ideas, Planning, & Feedback") | [<img src="https://avatars1.githubusercontent.com/u/5932031?v=4" width="100px;" alt="Stoyan Berov"/><br /><sub><b>Stoyan Berov</b></sub>](https://github.com/stoberov)<br />[🐛](https://github.com/dowjones/react-dropdown-tree-select/issues?q=author%3Astoberov "Bug reports") | [<img src="https://avatars0.githubusercontent.com/u/17863113?v=4" width="100px;" alt="ellinge"/><br /><sub><b>ellinge</b></sub>](https://github.com/ellinge)<br />[💻](https://github.com/dowjones/react-dropdown-tree-select/commits?author=ellinge "Code") [🤔](#ideas-ellinge "Ideas, Planning, & Feedback") [🚧](#maintenance-ellinge "Maintenance") | [<img src="https://avatars3.githubusercontent.com/u/5017449?v=4" width="100px;" alt="Sandy M"/><br /><sub><b>Sandy M</b></sub>](https://github.com/moonjy1993)<br />[💻](https://github.com/dowjones/react-dropdown-tree-select/commits?author=moonjy1993 "Code") [🐛](https://github.com/dowjones/react-dropdown-tree-select/issues?q=author%3Amoonjy1993 "Bug reports") | [<img src="https://avatars1.githubusercontent.com/u/529614?v=4" width="100px;" alt="Gustav Tonér"/><br /><sub><b>Gustav Tonér</b></sub>](https://www.gazab.se)<br />[💻](https://github.com/dowjones/react-dropdown-tree-select/commits?author=gazab "Code") |
603610
| [<img src="https://avatars1.githubusercontent.com/u/3390897?v=4" width="100px;" alt="Kestutis Kasiulynas"/><br /><sub><b>Kestutis Kasiulynas</b></sub>](http://kYem.net)<br />[🐛](https://github.com/dowjones/react-dropdown-tree-select/issues?q=author%3AkYem "Bug reports") [💻](https://github.com/dowjones/react-dropdown-tree-select/commits?author=kYem "Code") | [<img src="https://avatars3.githubusercontent.com/u/20484267?v=4" width="100px;" alt="Jesus Cabrera Gonzalez"/><br /><sub><b>Jesus Cabrera Gonzalez</b></sub>](https://github.com/isuscbrmid)<br />[💻](https://github.com/dowjones/react-dropdown-tree-select/commits?author=isuscbrmid "Code") | [<img src="https://avatars2.githubusercontent.com/u/27359753?v=4" width="100px;" alt="MJRuskin"/><br /><sub><b>MJRuskin</b></sub>](https://github.com/MJRuskin)<br />[💻](https://github.com/dowjones/react-dropdown-tree-select/commits?author=MJRuskin "Code") | [<img src="https://avatars1.githubusercontent.com/u/64946671?v=4" width="100px;" alt="akarshjairaj"/><br /><sub><b>akarshjairaj</b></sub>](https://github.com/akarshjairaj)<br />[💻](https://github.com/dowjones/react-dropdown-tree-select/commits?author=akarshjairaj "Code") | [<img src="https://avatars1.githubusercontent.com/u/539090?v=4" width="100px;" alt="Artem Berdyshev"/><br /><sub><b>Artem Berdyshev</b></sub>](https://github.com/berdyshev)<br />[💻](https://github.com/dowjones/react-dropdown-tree-select/commits?author=berdyshev "Code") [🐛](https://github.com/dowjones/react-dropdown-tree-select/issues?q=author%3Aberdyshev "Bug reports") | [<img src="https://avatars3.githubusercontent.com/u/42154031?v=4" width="100px;" alt="Matheus Wichman"/><br /><sub><b>Matheus Wichman</b></sub>](https://matheushw.com/)<br />[💻](https://github.com/dowjones/react-dropdown-tree-select/commits?author=m4theushw "Code") | [<img src="https://avatars1.githubusercontent.com/u/60662654?v=4" width="100px;" alt="aarce-uncharted"/><br /><sub><b>aarce-uncharted</b></sub>](https://github.com/aarce-uncharted)<br />[💻](https://github.com/dowjones/react-dropdown-tree-select/commits?author=aarce-uncharted "Code") |
604611
| [<img src="https://avatars0.githubusercontent.com/u/1795294?v=4" width="100px;" alt="Mohamad Othman"/><br /><sub><b>Mohamad Othman</b></sub>](http://osmancode.me)<br />[💻](https://github.com/dowjones/react-dropdown-tree-select/commits?author=osmancode "Code") [🤔](#ideas-osmancode "Ideas, Planning, & Feedback") | [<img src="https://avatars3.githubusercontent.com/u/8286468?v=4" width="100px;" alt="kathleenlu"/><br /><sub><b>kathleenlu</b></sub>](https://github.com/smurfs2549)<br />[💻](https://github.com/dowjones/react-dropdown-tree-select/commits?author=smurfs2549 "Code") [🐛](https://github.com/dowjones/react-dropdown-tree-select/issues?q=author%3Asmurfs2549 "Bug reports") |
612+
605613
<!-- ALL-CONTRIBUTORS-LIST:END -->
606614

607615
This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome!

__snapshots__/src/index.test.js.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Generated by [AVA](https://ava.li).
1919
clientId="rdts"
2020
onTrigger={Function {}}
2121
showDropdown={true}
22+
tabIndex={0}
2223
tags={[]}
2324
texts={{}}
2425
>
@@ -189,6 +190,7 @@ Generated by [AVA](https://ava.li).
189190
clientId="rdts"
190191
onTrigger={Function {}}
191192
showDropdown={true}
193+
tabIndex={0}
192194
tags={[]}
193195
texts={{}}
194196
>
@@ -359,6 +361,7 @@ Generated by [AVA](https://ava.li).
359361
disabled={true}
360362
onTrigger={Function {}}
361363
showDropdown={false}
364+
tabIndex={0}
362365
tags={[]}
363366
texts={{}}
364367
>
@@ -467,6 +470,7 @@ Generated by [AVA](https://ava.li).
467470
onChange={Function onChange {}}
468471
onFocus={Function onFocus {}}
469472
showDropdown="default"
473+
tabIndex={0}
470474
texts={{}}
471475
>
472476
<div
@@ -481,6 +485,7 @@ Generated by [AVA](https://ava.li).
481485
mode="radioSelect"
482486
onTrigger={Function {}}
483487
showDropdown={false}
488+
tabIndex={0}
484489
tags={[]}
485490
texts={{}}
486491
>
@@ -619,6 +624,7 @@ Generated by [AVA](https://ava.li).
619624
onChange={Function onChange {}}
620625
onFocus={Function onFocus {}}
621626
showDropdown="default"
627+
tabIndex={0}
622628
texts={{}}
623629
>
624630
<div
@@ -632,6 +638,7 @@ Generated by [AVA](https://ava.li).
632638
clientId="rdts"
633639
onTrigger={Function {}}
634640
showDropdown={false}
641+
tabIndex={0}
635642
tags={[]}
636643
texts={{}}
637644
>
@@ -702,6 +709,7 @@ Generated by [AVA](https://ava.li).
702709
clientId="rdts"
703710
onTrigger={Function {}}
704711
showDropdown={true}
712+
tabIndex={0}
705713
tags={[]}
706714
texts={{}}
707715
>
8 Bytes
Binary file not shown.

__snapshots__/src/trigger/index.test.js.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,4 @@ Generated by [AVA](https://ava.li).
1616
onClick={Function {}}
1717
onKeyDown={Function {}}
1818
role="button"
19-
tabIndex={0}
2019
/>
-11 Bytes
Binary file not shown.

src/index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class DropdownTreeSelect extends Component {
4848
id: PropTypes.string,
4949
searchPredicate: PropTypes.func,
5050
inlineSearchInput: PropTypes.bool,
51+
tabIndex: PropTypes.number,
5152
}
5253

5354
static defaultProps = {
@@ -58,6 +59,7 @@ class DropdownTreeSelect extends Component {
5859
texts: {},
5960
showDropdown: 'default',
6061
inlineSearchInput: false,
62+
tabIndex: 0,
6163
}
6264

6365
constructor(props) {
@@ -290,7 +292,7 @@ class DropdownTreeSelect extends Component {
290292
}
291293

292294
render() {
293-
const { disabled, readOnly, mode, texts, inlineSearchInput } = this.props
295+
const { disabled, readOnly, mode, texts, inlineSearchInput, tabIndex } = this.props
294296
const { showDropdown, currentFocus, tags } = this.state
295297

296298
const activeDescendant = currentFocus ? `${currentFocus}_li` : undefined
@@ -325,7 +327,13 @@ class DropdownTreeSelect extends Component {
325327
.filter(Boolean)
326328
.join(' ')}
327329
>
328-
<Trigger onTrigger={this.onTrigger} showDropdown={showDropdown} {...commonProps} tags={tags}>
330+
<Trigger
331+
onTrigger={this.onTrigger}
332+
showDropdown={showDropdown}
333+
{...commonProps}
334+
tags={tags}
335+
tabIndex={tabIndex}
336+
>
329337
<Tags tags={tags} onTagRemove={this.onTagRemove} {...commonProps}>
330338
{!inlineSearchInput && searchInput}
331339
</Tags>

src/index.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,20 @@ test('appends selected tags to aria-labelledby with text label', t => {
326326
t.deepEqual(wrapper.find('.dropdown-trigger').prop('aria-label'), 'hello world')
327327
})
328328

329+
test('default tabIndex value is 0', t => {
330+
const { tree } = t.context
331+
tree[0].checked = true
332+
const wrapper = mount(<DropdownTreeSelect id="rdts" data={tree} />)
333+
t.deepEqual(wrapper.find('.dropdown-trigger').prop('tabIndex'), 0)
334+
})
335+
336+
test('set tabIndex value', t => {
337+
const { tree } = t.context
338+
tree[0].checked = true
339+
const wrapper = mount(<DropdownTreeSelect id="rdts" data={tree} tabIndex={5} />)
340+
t.deepEqual(wrapper.find('.dropdown-trigger').prop('tabIndex'), 5)
341+
})
342+
329343
test('select correct focused node when using external state data container', t => {
330344
let data = [
331345
{

src/trigger/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Trigger extends PureComponent {
1414
texts: PropTypes.object,
1515
clientId: PropTypes.string,
1616
tags: PropTypes.array,
17+
tabIndex: PropTypes.number,
1718
}
1819

1920
getAriaAttributes = () => {
@@ -36,7 +37,7 @@ class Trigger extends PureComponent {
3637
const attributes = {
3738
id: triggerId,
3839
role: 'button',
39-
tabIndex: 0,
40+
tabIndex: this.props.tabIndex,
4041
'aria-haspopup': mode === 'simpleSelect' ? 'listbox' : 'tree',
4142
'aria-expanded': showDropdown ? 'true' : 'false',
4243
...labelAttributes,

0 commit comments

Comments
 (0)