Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement cursor start/end on Home/End keypress #565

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
[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 @@
defaultMessage="No results found"
/>
)}
onKeyDown={handleKeyDown}
{...props}
onChange={newValue => {
const isSingle = !Array.isArray(newValue);
Expand Down