1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

fix inputs not blurring when pressing enter (#4692)

This commit is contained in:
Andreas Schmidt 2022-01-19 14:27:55 +01:00 committed by GitHub
parent b7cb10521e
commit 86f14a9cdf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 2 deletions

View File

@ -54,6 +54,7 @@ export class EditableList<T> extends React.Component<Props<T>> {
onSubmit={this.onSubmit} onSubmit={this.onSubmit}
validators={validators} validators={validators}
placeholder={placeholder} placeholder={placeholder}
blurOnEnter={false}
iconRight={({ isDirty }) => isDirty ? <Icon material="keyboard_return" size={16} /> : null} iconRight={({ isDirty }) => isDirty ? <Icon material="keyboard_return" size={16} /> : null}
/> />
</div> </div>

View File

@ -52,6 +52,7 @@ export type InputProps = Omit<InputElementProps, "onChange" | "onSubmit"> & {
iconRight?: IconData; iconRight?: IconData;
contentRight?: string | React.ReactNode; // Any component of string goes after iconRight contentRight?: string | React.ReactNode; // Any component of string goes after iconRight
validators?: InputValidator | InputValidator[]; validators?: InputValidator | InputValidator[];
blurOnEnter?: boolean;
onChange?(value: string, evt: React.ChangeEvent<InputElement>): void; onChange?(value: string, evt: React.ChangeEvent<InputElement>): void;
onSubmit?(value: string, evt: React.KeyboardEvent<InputElement>): void; onSubmit?(value: string, evt: React.KeyboardEvent<InputElement>): void;
}; };
@ -70,6 +71,7 @@ const defaultProps: Partial<InputProps> = {
maxRows: 10000, maxRows: 10000,
showValidationLine: true, showValidationLine: true,
validators: [], validators: [],
blurOnEnter: true,
}; };
export class Input extends React.Component<InputProps, State> { export class Input extends React.Component<InputProps, State> {
@ -267,6 +269,11 @@ export class Input extends React.Component<InputProps, State> {
} else { } else {
this.setDirty(); this.setDirty();
} }
if(this.props.blurOnEnter){
//pressing enter indicates that the edit is complete, we can unfocus now
this.blur();
}
} }
} }

View File

@ -10,6 +10,7 @@ import { Button } from "../button";
import { Stepper } from "../stepper"; import { Stepper } from "../stepper";
import { SubTitle } from "../layout/sub-title"; import { SubTitle } from "../layout/sub-title";
import { Spinner } from "../spinner"; import { Spinner } from "../spinner";
import { debounce } from "lodash";
interface WizardCommonProps<D = any> { interface WizardCommonProps<D = any> {
data?: Partial<D>; data?: Partial<D>;
@ -179,14 +180,16 @@ export class WizardStep extends React.Component<WizardStepProps, WizardStepState
} }
}; };
submit = () => { //because submit MIGHT be called through pressing enter, it might be fired twice.
//we'll debounce it to ensure it isn't
submit = debounce(() => {
if (!this.form.noValidate) { if (!this.form.noValidate) {
const valid = this.form.checkValidity(); const valid = this.form.checkValidity();
if (!valid) return; if (!valid) return;
} }
this.next(); this.next();
}; }, 100);
renderLoading() { renderLoading() {
return ( return (
@ -196,6 +199,17 @@ export class WizardStep extends React.Component<WizardStepProps, WizardStepState
); );
} }
//make sure we call submit if the "enter" keypress doesn't trigger the events
keyDown(evt: React.KeyboardEvent<HTMLElement>) {
if (evt.shiftKey || evt.metaKey || evt.altKey || evt.ctrlKey || evt.repeat) {
return;
}
if(evt.key === "Enter"){
this.submit();
}
}
render() { render() {
const { const {
step, isFirst, isLast, children, step, isFirst, isLast, children,
@ -216,6 +230,7 @@ export class WizardStep extends React.Component<WizardStepProps, WizardStepState
return ( return (
<form className={className} <form className={className}
onSubmit={prevDefault(this.submit)} noValidate={noValidate} onSubmit={prevDefault(this.submit)} noValidate={noValidate}
onKeyDown={(evt) => this.keyDown(evt)}
ref={e => this.form = e}> ref={e => this.form = e}>
{beforeContent} {beforeContent}
<div className={contentClass}> <div className={contentClass}>