Skip to content
GitLab
Explore
Projects
Groups
Snippets
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Bootstrap
bootstrap
Commits
3b3366e1
Commit
3b3366e1
authored
7 years ago
by
Patrick H. Lauke
Committed by
GitHub
7 years ago
Browse files
Options
Download
Plain Diff
remove dropdown.js reliance on roles and fix keyboard navigation
parents
8c975327
6301fabe
7 merge requests
!28721
Hot test
,
!27561
Adds font-weight-medium to font weight classes
,
!22598
test
,
!25326
Adjust examples
,
!23995
Add back cursor: pointer for .btn-link
,
!23178
Spinner
,
!17021
v4
Changes
2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
js/src/dropdown.js
+14
-15
js/src/dropdown.js
js/tests/unit/dropdown.js
+319
-203
js/tests/unit/dropdown.js
with
333 additions
and
218 deletions
+333
-218
js/src/dropdown.js
+
14
-
15
View file @
3b3366e1
...
...
@@ -25,10 +25,11 @@ const Dropdown = (($) => {
const
JQUERY_NO_CONFLICT
=
$
.
fn
[
NAME
]
const
ESCAPE_KEYCODE
=
27
// KeyboardEvent.which value for Escape (Esc) key
const
SPACE_KEYCODE
=
32
// KeyboardEvent.which value for space key
const
TAB_KEYCODE
=
9
// KeyboardEvent.which value for tab key
const
ARROW_UP_KEYCODE
=
38
// KeyboardEvent.which value for up arrow key
const
ARROW_DOWN_KEYCODE
=
40
// KeyboardEvent.which value for down arrow key
const
RIGHT_MOUSE_BUTTON_WHICH
=
3
// MouseEvent.which value for the right button (assuming a right-handed mouse)
const
REGEXP_KEYDOWN
=
new
RegExp
(
`
${
ARROW_UP_KEYCODE
}
|
${
ARROW_DOWN_KEYCODE
}
|
${
ESCAPE_KEYCODE
}
|
${
SPACE_KEYCODE
}
`
)
const
REGEXP_KEYDOWN
=
new
RegExp
(
`
${
ARROW_UP_KEYCODE
}
|
${
ARROW_DOWN_KEYCODE
}
|
${
ESCAPE_KEYCODE
}
`
)
const
Event
=
{
HIDE
:
`hide
${
EVENT_KEY
}
`
,
...
...
@@ -37,8 +38,8 @@ const Dropdown = (($) => {
SHOWN
:
`shown
${
EVENT_KEY
}
`
,
CLICK
:
`click
${
EVENT_KEY
}
`
,
CLICK_DATA_API
:
`click
${
EVENT_KEY
}${
DATA_API_KEY
}
`
,
FOCUSI
N_DATA_API
:
`
focusi
n
${
EVENT_KEY
}${
DATA_API_KEY
}
`
,
KEY
DOWN
_DATA_API
:
`key
down
${
EVENT_KEY
}${
DATA_API_KEY
}
`
KEYDOW
N_DATA_API
:
`
keydow
n
${
EVENT_KEY
}${
DATA_API_KEY
}
`
,
KEY
UP
_DATA_API
:
`key
up
${
EVENT_KEY
}${
DATA_API_KEY
}
`
}
const
ClassName
=
{
...
...
@@ -51,11 +52,9 @@ const Dropdown = (($) => {
BACKDROP
:
'
.dropdown-backdrop
'
,
DATA_TOGGLE
:
'
[data-toggle="dropdown"]
'
,
FORM_CHILD
:
'
.dropdown form
'
,
ROLE_MENU
:
'
[role="menu"]
'
,
ROLE_LISTBOX
:
'
[role="listbox"]
'
,
MENU
:
'
.dropdown-menu
'
,
NAVBAR_NAV
:
'
.navbar-nav
'
,
VISIBLE_ITEMS
:
'
[role="menu"] li:not(.disabled) a,
'
+
'
[role="listbox"] li:not(.disabled) a
'
VISIBLE_ITEMS
:
'
.dropdown-menu .dropdown-item:not(.disabled)
'
}
...
...
@@ -164,7 +163,8 @@ const Dropdown = (($) => {
}
static
_clearMenus
(
event
)
{
if
(
event
&&
event
.
which
===
RIGHT_MOUSE_BUTTON_WHICH
)
{
if
(
event
&&
(
event
.
which
===
RIGHT_MOUSE_BUTTON_WHICH
||
event
.
type
===
'
keyup
'
&&
event
.
which
!==
TAB_KEYCODE
))
{
return
}
...
...
@@ -181,7 +181,7 @@ const Dropdown = (($) => {
}
if
(
event
&&
(
event
.
type
===
'
click
'
&&
/input|textarea/i
.
test
(
event
.
target
.
tagName
)
||
event
.
type
===
'
focusin
'
)
/input|textarea/i
.
test
(
event
.
target
.
tagName
)
||
event
.
type
===
'
keyup
'
&&
event
.
which
===
TAB_KEYCODE
)
&&
$
.
contains
(
parent
,
event
.
target
))
{
continue
}
...
...
@@ -218,7 +218,7 @@ const Dropdown = (($) => {
}
static
_dataApiKeydownHandler
(
event
)
{
if
(
!
REGEXP_KEYDOWN
.
test
(
event
.
which
)
||
if
(
!
REGEXP_KEYDOWN
.
test
(
event
.
which
)
||
/button/i
.
test
(
event
.
target
.
tagName
)
&&
event
.
which
===
SPACE_KEYCODE
||
/input|textarea/i
.
test
(
event
.
target
.
tagName
))
{
return
}
...
...
@@ -233,8 +233,8 @@ const Dropdown = (($) => {
const
parent
=
Dropdown
.
_getParentFromElement
(
this
)
const
isActive
=
$
(
parent
).
hasClass
(
ClassName
.
SHOW
)
if
(
!
isActive
&&
event
.
which
!==
ESCAPE_KEYCODE
||
isActive
&&
event
.
which
===
ESCAPE_KEYCODE
)
{
if
(
!
isActive
&&
(
event
.
which
!==
ESCAPE_KEYCODE
||
event
.
which
!==
SPACE_KEYCODE
)
||
isActive
&&
(
event
.
which
===
ESCAPE_KEYCODE
||
event
.
which
===
SPACE_KEYCODE
)
)
{
if
(
event
.
which
===
ESCAPE_KEYCODE
)
{
const
toggle
=
$
(
parent
).
find
(
Selector
.
DATA_TOGGLE
)[
0
]
...
...
@@ -279,9 +279,8 @@ const Dropdown = (($) => {
$
(
document
)
.
on
(
Event
.
KEYDOWN_DATA_API
,
Selector
.
DATA_TOGGLE
,
Dropdown
.
_dataApiKeydownHandler
)
.
on
(
Event
.
KEYDOWN_DATA_API
,
Selector
.
ROLE_MENU
,
Dropdown
.
_dataApiKeydownHandler
)
.
on
(
Event
.
KEYDOWN_DATA_API
,
Selector
.
ROLE_LISTBOX
,
Dropdown
.
_dataApiKeydownHandler
)
.
on
(
`
${
Event
.
CLICK_DATA_API
}
${
Event
.
FOCUSIN_DATA_API
}
`
,
Dropdown
.
_clearMenus
)
.
on
(
Event
.
KEYDOWN_DATA_API
,
Selector
.
MENU
,
Dropdown
.
_dataApiKeydownHandler
)
.
on
(
`
${
Event
.
CLICK_DATA_API
}
${
Event
.
KEYUP_DATA_API
}
`
,
Dropdown
.
_clearMenus
)
.
on
(
Event
.
CLICK_DATA_API
,
Selector
.
DATA_TOGGLE
,
Dropdown
.
prototype
.
toggle
)
.
on
(
Event
.
CLICK_DATA_API
,
Selector
.
FORM_CHILD
,
(
e
)
=>
{
e
.
stopPropagation
()
...
...
This diff is collapsed.
Click to expand it.
js/tests/unit/dropdown.js
+
319
-
203
View file @
3b3366e1
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Explore
Projects
Groups
Snippets