Skip to content

Commit

Permalink
hold off on validation until submit has been attempted
Browse files Browse the repository at this point in the history
  • Loading branch information
drewdaemon committed May 26, 2023
1 parent 9df5131 commit 60f1956
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export interface SaveModalState {
hasTitleDuplicate: boolean;
isLoading: boolean;
visualizationDescription: string;
hasAttemptedSubmit: boolean;
}

const generateId = htmlIdGenerator();
Expand All @@ -82,10 +83,11 @@ export class SavedObjectSaveModal extends React.Component<Props, SaveModalState>
hasTitleDuplicate: false,
isLoading: false,
visualizationDescription: this.props.description ? this.props.description : '',
hasAttemptedSubmit: false,
};

public render() {
const { isTitleDuplicateConfirmed, hasTitleDuplicate, title } = this.state;
const { isTitleDuplicateConfirmed, hasTitleDuplicate, title, hasAttemptedSubmit } = this.state;
const duplicateWarningId = generateId();

const hasColumns = !!this.props.rightOptions;
Expand All @@ -102,7 +104,10 @@ export class SavedObjectSaveModal extends React.Component<Props, SaveModalState>
data-test-subj="savedObjectTitle"
value={title}
onChange={this.onTitleChange}
isInvalid={(!isTitleDuplicateConfirmed && hasTitleDuplicate) || title.length === 0}
isInvalid={
hasAttemptedSubmit &&
((!isTitleDuplicateConfirmed && hasTitleDuplicate) || title.length === 0)
}
aria-describedby={this.state.hasTitleDuplicate ? duplicateWarningId : undefined}
/>
</EuiFormRow>
Expand Down Expand Up @@ -258,11 +263,22 @@ export class SavedObjectSaveModal extends React.Component<Props, SaveModalState>

private onFormSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
this.saveSavedObject();

const { hasAttemptedSubmit, title } = this.state;

if (!hasAttemptedSubmit) {
this.setState({ hasAttemptedSubmit: true });
}

const isValid = this.props.isValid !== undefined ? this.props.isValid : true;

if (title.length !== 0 && isValid) {
this.saveSavedObject();
}
};

private renderConfirmButton = () => {
const { isLoading, title } = this.state;
const { isLoading } = this.state;

let confirmLabel: string | React.ReactNode = i18n.translate(
'savedObjects.saveModal.saveButtonLabel',
Expand All @@ -275,14 +291,11 @@ export class SavedObjectSaveModal extends React.Component<Props, SaveModalState>
confirmLabel = this.props.confirmButtonLabel;
}

const isValid = this.props.isValid !== undefined ? this.props.isValid : true;

return (
<EuiButton
fill
data-test-subj="confirmSaveSavedObjectButton"
isLoading={isLoading}
isDisabled={title.length === 0 || !isValid}
type="submit"
form={this.formId}
>
Expand Down

0 comments on commit 60f1956

Please sign in to comment.