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

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

Merged
merged 1 commit into from
Jul 20, 2021
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ export class AddQuotaDialog extends React.Component<Props> {
<Input
required autoFocus
placeholder="ResourceQuota name"
trim
validators={systemName}
value={this.quotaName} onChange={v => this.quotaName = v.toLowerCase()}
className="box grow"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ export class AddSecretDialog extends React.Component<Props> {
<Input
autoFocus required
placeholder="Name"
trim
validators={systemName}
value={name} onChange={v => this.name = v}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export class AddNamespaceDialog extends React.Component<Props> {
required autoFocus
iconLeft="layers"
placeholder="Namespace"
trim
validators={systemName}
value={namespace} onChange={v => this.namespace = v.toLowerCase()}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ export class AddHelmRepoDialog extends React.Component<Props> {
<Input
autoFocus required
placeholder="Helm repo name"
trim
validators={systemName}
value={this.helmRepo.name} onChange={v => this.helmRepo.name = v}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,10 @@ export class CreateServiceAccountDialog extends React.Component<Props> {
<WizardStep nextLabel="Create" next={this.createAccount}>
<SubTitle title="Account Name" />
<Input
autoFocus required
autoFocus
required
placeholder="Enter a name"
trim
validators={systemName}
value={name} onChange={v => this.name = v.toLowerCase()}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export class CronJobTriggerDialog extends Component<Props> {
<Input
required autoFocus
placeholder={this.jobName}
trim
validators={[systemName, maxLength]}
maxLength={63}
value={this.jobName} onChange={v => this.jobName = v.toLowerCase()}
Expand Down
24 changes: 13 additions & 11 deletions src/renderer/components/input/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ export type { InputValidator };
type InputElement = HTMLInputElement | HTMLTextAreaElement;
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";
className?: string;
value?: T;
autoSelectOnFocus?: boolean
value?: string;
trim?: boolean;
autoSelectOnFocus?: boolean;
defaultValue?: string;
multiLine?: boolean; // use text-area as input field
maxRows?: number; // when multiLine={true} define max rows size
dirty?: boolean; // show validation errors even if the field wasn't touched yet
Expand All @@ -54,8 +56,8 @@ export type InputProps<T = string> = Omit<InputElementProps, "onChange" | "onSub
iconRight?: string | React.ReactNode;
contentRight?: string | React.ReactNode; // Any component of string goes after iconRight
validators?: InputValidator | InputValidator[];
onChange?(value: T, evt: React.ChangeEvent<InputElement>): void;
onSubmit?(value: T, evt: React.KeyboardEvent<InputElement>): void;
onChange?(value: string, evt: React.ChangeEvent<InputElement>): void;
onSubmit?(value: string, evt: React.KeyboardEvent<InputElement>): void;
};

interface State {
Expand All @@ -72,6 +74,7 @@ const defaultProps: Partial<InputProps> = {
maxRows: 10000,
showValidationLine: true,
validators: [],
defaultValue: "",
};

export class Input extends React.Component<InputProps, State> {
Expand Down Expand Up @@ -102,12 +105,10 @@ export class Input extends React.Component<InputProps, State> {
}

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
if (this.input) return this.input.value; // uncontrolled input

return defaultValue as string;
return trim ? rawValue.trim() : rawValue;
}

focus() {
Expand Down Expand Up @@ -138,7 +139,8 @@ export class Input extends React.Component<InputProps, State> {

private validationId: string;

async validate(value = this.getValue()) {
async validate() {
const value = this.getValue();
let validationId = (this.validationId = ""); // reset every time for async validators
const asyncValidators: Promise<any>[] = [];
const errors: React.ReactNode[] = [];
Expand Down