Skip to content

Commit

Permalink
🚸 [#437] Implement cursor start/end on Home/End keypress
Browse files Browse the repository at this point in the history
Solution taken from react-select issue/discussion.

There is some ambiguity on what the behaviour should be on MacOS, but
due to lack of appropriate device we're leaving that as it is, for
now.
  • Loading branch information
sergei-maertens committed Oct 16, 2023
1 parent 2787793 commit 5123f46
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/components/forms/SelectField/SelectField.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,25 @@ const SelectField = ({
[autoSelectOnlyOption, isSingle, value, JSON.stringify(options), setValue, valueProperty]
);

// Taken from snippet posted in react-select issue:
// https://github.com/JedWatson/react-select/issues/3562#issuecomment-518841754
const handleKeyDown = evt => {
switch (evt.key) {
case 'Home':
evt.preventDefault();

Check warning on line 63 in src/components/forms/SelectField/SelectField.js

View check run for this annotation

Codecov / codecov/patch

src/components/forms/SelectField/SelectField.js#L62-L63

Added lines #L62 - L63 were not covered by tests
if (evt.shiftKey) evt.target.selectionStart = 0;
else evt.target.setSelectionRange(0, 0);
break;
case 'End':
evt.preventDefault();
const len = evt.target.value.length;

Check warning on line 69 in src/components/forms/SelectField/SelectField.js

View check run for this annotation

Codecov / codecov/patch

src/components/forms/SelectField/SelectField.js#L65-L69

Added lines #L65 - L69 were not covered by tests
if (evt.shiftKey) evt.target.selectionEnd = len;
else evt.target.setSelectionRange(len, len);
break;

Check warning on line 72 in src/components/forms/SelectField/SelectField.js

View check run for this annotation

Codecov / codecov/patch

src/components/forms/SelectField/SelectField.js#L71-L72

Added lines #L71 - L72 were not covered by tests
// no default
}
};

return (
<Wrapper>
<FormField invalid={invalid} className="utrecht-form-field--openforms">
Expand Down Expand Up @@ -105,6 +124,7 @@ const SelectField = ({
defaultMessage="No results found"
/>
)}
onKeyDown={handleKeyDown}
{...props}
onChange={newValue => {
const isSingle = !Array.isArray(newValue);
Expand Down

0 comments on commit 5123f46

Please sign in to comment.