Skip to content

Commit

Permalink
Merge pull request #565 from open-formulieren/issue/437-select-home-e…
Browse files Browse the repository at this point in the history
…nd-behaviour

Implement cursor start/end on Home/End keypress
  • Loading branch information
sergei-maertens authored Oct 17, 2023
2 parents 2787793 + 5123f46 commit a1c5317
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();
if (evt.shiftKey) evt.target.selectionStart = 0;
else evt.target.setSelectionRange(0, 0);
break;
case 'End':
evt.preventDefault();
const len = evt.target.value.length;
if (evt.shiftKey) evt.target.selectionEnd = len;
else evt.target.setSelectionRange(len, len);
break;
// 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 a1c5317

Please sign in to comment.