-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UIQM-399 Linked MARC bib field - editable textbox does not support sh…
…ortcut keys (#475) * UIQM-399 Linked MARC bib field - editable textbox does not support shortcut keys * UIQM-399 Fix tests
- Loading branch information
1 parent
3536324
commit 908fb7b
Showing
10 changed files
with
178 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './useAuthorityLinking'; | ||
export * from './useSubfieldNavigation'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './useSubfieldNavigation'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { useCallback } from 'react'; | ||
|
||
import { | ||
cursorToNextSubfield, | ||
cursorToPrevSubfield, | ||
} from './utils'; | ||
import { KEYBOARD_COMMAND_NAMES } from '../../common/constants'; | ||
|
||
export const useSubfieldNavigation = () => { | ||
const processSubfieldFocus = useCallback(({ target }) => { | ||
const end = target.value.length; | ||
|
||
target.setSelectionRange(end, end); | ||
}, []); | ||
|
||
const keyCommands = [{ | ||
name: KEYBOARD_COMMAND_NAMES.NEXT_SUBFIELD, | ||
handler: cursorToNextSubfield, | ||
}, { | ||
name: KEYBOARD_COMMAND_NAMES.PREV_SUBFIELD, | ||
handler: cursorToPrevSubfield, | ||
}]; | ||
|
||
return { processSubfieldFocus, keyCommands }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
const indexOfRegex = (string, regex) => { | ||
const match = string.match(regex); | ||
|
||
return match ? string.indexOf(match[0]) : -1; | ||
}; | ||
|
||
const lastIndexOfRegex = (string, regex) => { | ||
const match = string.match(regex); | ||
|
||
return match ? string.lastIndexOf(match[match.length - 1]) : -1; | ||
}; | ||
|
||
export const cursorToNextSubfield = (e) => { | ||
e.preventDefault(); | ||
const cursorPosition = e.target.selectionStart; | ||
const valueAfterCursor = e.target.value.substring(cursorPosition); | ||
|
||
const nextSubfieldIndex = indexOfRegex(valueAfterCursor, /\$\w\s/g); | ||
|
||
if (nextSubfieldIndex === -1) { | ||
return; | ||
} | ||
|
||
const newPosition = nextSubfieldIndex + cursorPosition + 3; | ||
|
||
e.target.setSelectionRange(newPosition, newPosition); | ||
}; | ||
|
||
export const cursorToPrevSubfield = (e) => { | ||
e.preventDefault(); | ||
const cursorPosition = e.target.selectionStart; | ||
const startOfCurrentSubfieldPosition = lastIndexOfRegex(e.target.value.substring(0, cursorPosition), /\$\w\s/g); | ||
const valueBeforeCurrentSubfield = e.target.value.substring(0, startOfCurrentSubfieldPosition); | ||
|
||
const prevSubfieldIndex = lastIndexOfRegex(valueBeforeCurrentSubfield, /\$\w\s/g); | ||
|
||
if (prevSubfieldIndex === -1) { | ||
return; | ||
} | ||
|
||
const newPosition = prevSubfieldIndex + 3; | ||
|
||
e.target.setSelectionRange(newPosition, newPosition); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import * as utils from './utils'; | ||
|
||
describe('useSubfieldNavigation utils', () => { | ||
describe('cursorToNextSubfield', () => { | ||
let e; | ||
|
||
beforeEach(() => { | ||
e = { | ||
preventDefault: jest.fn(), | ||
target: { | ||
value: '', | ||
setSelectionRange: jest.fn(), | ||
}, | ||
}; | ||
}); | ||
|
||
describe('when there is a next subfield', () => { | ||
it('should move cursor to the beginning of next subfield', () => { | ||
e.target.value = '$a some value $b some other value'; | ||
e.target.selectionStart = 13; // cursor at `$a some value|` | ||
|
||
utils.cursorToNextSubfield(e); | ||
|
||
expect(e.target.setSelectionRange).toHaveBeenCalledWith(17, 17); // cursor at `$b |some other value` | ||
}); | ||
}); | ||
|
||
describe('when there is no next subfield', () => { | ||
it('should keep cursor where it is', () => { | ||
e.target.value = '$a some value $b some other value'; | ||
e.target.selectionStart = 21; // cursor at `$b some| other value` | ||
|
||
utils.cursorToNextSubfield(e); | ||
|
||
expect(e.target.setSelectionRange).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('cursorToPrevSubfield', () => { | ||
let e; | ||
|
||
beforeEach(() => { | ||
e = { | ||
preventDefault: jest.fn(), | ||
target: { | ||
value: '', | ||
setSelectionRange: jest.fn(), | ||
}, | ||
}; | ||
}); | ||
|
||
describe('when there is a prev subfield', () => { | ||
it('should move cursor to the beginning of prev subfield', () => { | ||
e.target.value = '$a some value $b some other value'; | ||
e.target.selectionStart = 21; // cursor at `$b some| other value` | ||
|
||
utils.cursorToPrevSubfield(e); | ||
|
||
expect(e.target.setSelectionRange).toHaveBeenCalledWith(3, 3); // cursor at `$a |some value` | ||
}); | ||
}); | ||
|
||
describe('when there is no prev subfield', () => { | ||
it('should keep cursor where it is', () => { | ||
e.target.value = '$a some value $b some other value'; | ||
e.target.selectionStart = 13; // cursor at `$a some value|` | ||
|
||
utils.cursorToPrevSubfield(e); | ||
|
||
expect(e.target.setSelectionRange).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); | ||
}); |