Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
django-split-input
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Julian
django-split-input
Commits
399050c4
Verified
Commit
399050c4
authored
4 years ago
by
Jonathan Weth
Browse files
Options
Downloads
Patches
Plain Diff
Add support for backspaces, tabs and arrow keys to make them work as expected
parent
5a6c4d57
Branches
8-add-support-for-backspaces-tabs-and-arrow-keys
No related tags found
1 merge request
!8
Draft: Resolve "Add support for backspaces, tabs and arrow keys"
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
django_split_input/static/django_split_input/js/input.js
+24
-1
24 additions, 1 deletion
django_split_input/static/django_split_input/js/input.js
with
24 additions
and
1 deletion
django_split_input/static/django_split_input/js/input.js
+
24
−
1
View file @
399050c4
console
.
log
(
"
Input script loaded
"
);
const
KEY_CODES_TAB
=
[
9
,
16
];
const
KEY_CODE_ARROW_LEFT
=
37
;
const
KEY_CODE_ARROW_RIGHT
=
39
;
const
KEY_CODE_BACKSPACE
=
8
;
$
(
document
).
ready
(
function
()
{
let
all_inputs
=
$
(
"
.split-input-group input
"
);
console
.
log
(
"
Split inputs loaded.
"
)
...
...
@@ -15,9 +20,27 @@ $(document).ready(function () {
let
target
=
$
(
e
.
target
);
let
value
=
target
.
val
();
let
max_len
=
target
.
attr
(
"
maxLength
"
);
if
(
value
.
length
>=
max_len
)
{
let
cursorPosition
=
target
.
get
(
0
).
selectionStart
;
// Detect some special key codes to sort them out later
let
isTabEvent
=
KEY_CODES_TAB
.
includes
(
e
.
keyCode
);
let
isKeyArrowLeftEvent
=
e
.
keyCode
===
KEY_CODE_ARROW_LEFT
;
let
isKeyArrowRightEvent
=
e
.
keyCode
===
KEY_CODE_ARROW_RIGHT
let
isKeyArrowEvent
=
isKeyArrowLeftEvent
|
isKeyArrowRightEvent
;
let
isBackspaceEvent
=
e
.
keyCode
===
KEY_CODE_BACKSPACE
;
if
(
// Go to next input ...
(
value
.
length
>=
max_len
&&
!
isTabEvent
&&
!
isKeyArrowEvent
)
// ... if maximum length is reached and it wasn't an arrow key or tab
||
(
cursorPosition
===
value
.
length
&&
isKeyArrowRightEvent
)
// ... or, if the user presses arrow right when the cursor is at the end of the input
)
{
target
.
next
(
"
input.split-input
"
).
focus
();
console
.
log
(
"
Next target selected
"
);
}
else
if
(
// Go to previous input ...
(
value
.
length
===
0
&&
isBackspaceEvent
)
// ... if the complete input is empty and the user presses backspace
||
(
cursorPosition
===
0
&&
isKeyArrowLeftEvent
)
/// ... or, if the user presses arrow left when the cursor is at the beginning of the input
)
{
target
.
prev
(
"
input.split-input
"
).
focus
();
console
.
log
(
"
Prev target selected
"
);
}
})
})
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
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!
Save comment
Cancel
Please
register
or
sign in
to comment