chore: Add onEnter and onEscape to Input Component#1850
chore: Add onEnter and onEscape to Input Component#1850camielvs wants to merge 1 commit into02-23-feat_submit_pipeline_tags_on_runsfrom
Conversation
|
Warning This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
This stack of pull requests is managed by Graphite. Learn more about stacking. |
e07ea07 to
39e086e
Compare
| onEnter?: () => void; | ||
| onEscape?: () => void; |
There was a problem hiding this comment.
I do like the idea - simplifies a lot. I noticed that in most cases onEnter means "onSave" and "onEscape" means "onCancel". So maybe we should name them same way?
E.g. in InlineTextEditor same named as "onSave" and "onCancel" - we may introduce naming inconsistencies or establish a good convention.
There was a problem hiding this comment.
Maybe... I can see the merit in this. onSave and onClose and more descriptive and nicer to work with.
One thing I like about onEnter and onEscape is that its syntactically ambiguous and thus does not assume anything about what the keys are doing. If we ever need to use the enter key or escape key for actions other than the typical save/close then it's clear how to go about achieving this and we wouldn't need to add any additional logic.
My gut feeling for now is to keep as-is but I am not attached. If you feel strongly about switching to onSave and onClose then I am happy to change.
39e086e to
422e5be
Compare
e5b794e to
075d6c9
Compare
🎩 To tophat this PR:You can add the following URL parameter to your browser to tophat this PR: |
| onEscape(); | ||
| } | ||
| }} | ||
| {...props} |
There was a problem hiding this comment.
NIT: onKeyDown might be overridden if to provide props. We may make it safe. But up to you
function Input({
className,
type,
readOnly,
variant = readOnly ? "readOnly" : "default",
onEnter,
onEscape,
onKeyDown,
...props
}: ...) {
const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === "Enter" && onEnter) {
e.preventDefault();
e.stopPropagation();
onEnter();
} else if (e.key === "Escape" && onEscape) {
e.preventDefault();
e.stopPropagation();
onEscape();
}
onKeyDown?.(e);
};
return (
<input
...
onKeyDown={handleKeyDown}
{...props} // no longer contains onKeyDown
/>
);
}
Description
Add native
onEnterandonEscapehandlers toInputcomponent to streamline use of these common actions.Related Issue and Pull requests
Type of Change
Checklist
Screenshots (if applicable)
Test Instructions
Additional Comments