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

Add ability to trim the value from <Input> (#3333)

- If set (default = false) then the input will be trimmed of whitespace
  before validation and before submit

- Use trim on all input's that use the systemName validator

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2021-07-20 09:30:02 -04:00 committed by GitHub
parent 14b70a6c53
commit a4f252864b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 21 additions and 12 deletions

View File

@ -175,6 +175,7 @@ export class AddQuotaDialog extends React.Component<Props> {
<Input <Input
required autoFocus required autoFocus
placeholder="ResourceQuota name" placeholder="ResourceQuota name"
trim
validators={systemName} validators={systemName}
value={this.quotaName} onChange={v => this.quotaName = v.toLowerCase()} value={this.quotaName} onChange={v => this.quotaName = v.toLowerCase()}
className="box grow" className="box grow"

View File

@ -223,6 +223,7 @@ export class AddSecretDialog extends React.Component<Props> {
<Input <Input
autoFocus required autoFocus required
placeholder="Name" placeholder="Name"
trim
validators={systemName} validators={systemName}
value={name} onChange={v => this.name = v} value={name} onChange={v => this.name = v}
/> />

View File

@ -100,6 +100,7 @@ export class AddNamespaceDialog extends React.Component<Props> {
required autoFocus required autoFocus
iconLeft="layers" iconLeft="layers"
placeholder="Namespace" placeholder="Namespace"
trim
validators={systemName} validators={systemName}
value={namespace} onChange={v => this.namespace = v.toLowerCase()} value={namespace} onChange={v => this.namespace = v.toLowerCase()}
/> />

View File

@ -177,6 +177,7 @@ export class AddHelmRepoDialog extends React.Component<Props> {
<Input <Input
autoFocus required autoFocus required
placeholder="Helm repo name" placeholder="Helm repo name"
trim
validators={systemName} validators={systemName}
value={this.helmRepo.name} onChange={v => this.helmRepo.name = v} value={this.helmRepo.name} onChange={v => this.helmRepo.name = v}
/> />

View File

@ -88,8 +88,10 @@ export class CreateServiceAccountDialog extends React.Component<Props> {
<WizardStep nextLabel="Create" next={this.createAccount}> <WizardStep nextLabel="Create" next={this.createAccount}>
<SubTitle title="Account Name" /> <SubTitle title="Account Name" />
<Input <Input
autoFocus required autoFocus
required
placeholder="Enter a name" placeholder="Enter a name"
trim
validators={systemName} validators={systemName}
value={name} onChange={v => this.name = v.toLowerCase()} value={name} onChange={v => this.name = v.toLowerCase()}
/> />

View File

@ -123,6 +123,7 @@ export class CronJobTriggerDialog extends Component<Props> {
<Input <Input
required autoFocus required autoFocus
placeholder={this.jobName} placeholder={this.jobName}
trim
validators={[systemName, maxLength]} validators={[systemName, maxLength]}
maxLength={63} maxLength={63}
value={this.jobName} onChange={v => this.jobName = v.toLowerCase()} value={this.jobName} onChange={v => this.jobName = v.toLowerCase()}

View File

@ -41,11 +41,13 @@ export type { InputValidator };
type InputElement = HTMLInputElement | HTMLTextAreaElement; type InputElement = HTMLInputElement | HTMLTextAreaElement;
type InputElementProps = InputHTMLAttributes<InputElement> & TextareaHTMLAttributes<InputElement> & DOMAttributes<InputElement>; type InputElementProps = InputHTMLAttributes<InputElement> & TextareaHTMLAttributes<InputElement> & DOMAttributes<InputElement>;
export type InputProps<T = string> = Omit<InputElementProps, "onChange" | "onSubmit"> & { export type InputProps = Omit<InputElementProps, "onChange" | "onSubmit"> & {
theme?: "round-black" | "round"; theme?: "round-black" | "round";
className?: string; className?: string;
value?: T; value?: string;
autoSelectOnFocus?: boolean trim?: boolean;
autoSelectOnFocus?: boolean;
defaultValue?: string;
multiLine?: boolean; // use text-area as input field multiLine?: boolean; // use text-area as input field
maxRows?: number; // when multiLine={true} define max rows size maxRows?: number; // when multiLine={true} define max rows size
dirty?: boolean; // show validation errors even if the field wasn't touched yet dirty?: boolean; // show validation errors even if the field wasn't touched yet
@ -55,8 +57,8 @@ export type InputProps<T = string> = Omit<InputElementProps, "onChange" | "onSub
iconRight?: string | React.ReactNode; iconRight?: string | React.ReactNode;
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[];
onChange?(value: T, evt: React.ChangeEvent<InputElement>): void; onChange?(value: string, evt: React.ChangeEvent<InputElement>): void;
onSubmit?(value: T, evt: React.KeyboardEvent<InputElement>): void; onSubmit?(value: string, evt: React.KeyboardEvent<InputElement>): void;
}; };
interface State { interface State {
@ -73,6 +75,7 @@ const defaultProps: Partial<InputProps> = {
maxRows: 10000, maxRows: 10000,
showValidationLine: true, showValidationLine: true,
validators: [], validators: [],
defaultValue: "",
}; };
export class Input extends React.Component<InputProps, State> { export class Input extends React.Component<InputProps, State> {
@ -102,12 +105,10 @@ export class Input extends React.Component<InputProps, State> {
} }
getValue(): string { getValue(): string {
const { value, defaultValue = "" } = this.props; const { trim, value, defaultValue } = this.props;
const rawValue = value ?? this.input?.value ?? defaultValue;
if (value !== undefined) return value; // controlled input return trim ? rawValue.trim() : rawValue;
if (this.input) return this.input.value; // uncontrolled input
return defaultValue as string;
} }
focus() { focus() {
@ -138,7 +139,8 @@ export class Input extends React.Component<InputProps, State> {
private validationId: string; private validationId: string;
async validate(value = this.getValue()) { async validate() {
const value = this.getValue();
let validationId = (this.validationId = ""); // reset every time for async validators let validationId = (this.validationId = ""); // reset every time for async validators
const asyncValidators: Promise<any>[] = []; const asyncValidators: Promise<any>[] = [];
const errors: React.ReactNode[] = []; const errors: React.ReactNode[] = [];