Skip to content

Commit

Permalink
fix(EntityModal): prevent Enter from submitting the form in Modal win…
Browse files Browse the repository at this point in the history
…dow (#1047)

## The problem

Enter for a form with single input triggers the form submission. [The
specification](https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#implicit-submission).
The form submission was not properly handled.

## The fix
- hook up on form submission by adding callback onSubmit for `<form />`
element
- prevent default behavior (sending GET with formdata in params and
redirect) and proceed with our handlers (validation and sending as JSON
with AJAX)

## References
- https://splunk.atlassian.net/browse/ADDON-61126
- https://splunk.atlassian.net/browse/ADDON-58706

Resolves #875
  • Loading branch information
vtsvetkov-splunk committed Feb 6, 2024
1 parent 04b847f commit b7f179a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
9 changes: 6 additions & 3 deletions ui/src/components/BaseFormView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -522,8 +522,8 @@ class BaseFormView extends PureComponent<BaseFormProps, BaseFormState> {
}
};

// eslint-disable-next-line react/no-unused-class-component-methods
handleSubmit = () => {
handleSubmit = (event: React.MouseEvent | React.FormEvent) => {
event.preventDefault();
this.clearErrorMsg();
this.props.handleFormSubmit(/* isSubmitting */ true, /* closeEntity */ false);

Expand Down Expand Up @@ -1253,7 +1253,10 @@ class BaseFormView extends PureComponent<BaseFormProps, BaseFormState> {
}
return (
<div>
<form style={this.props.mode === MODE_CONFIG ? { marginTop: '25px' } : {}}>
<form
onSubmit={this.handleSubmit}
style={this.props.mode === MODE_CONFIG ? { marginTop: '25px' } : {}}
>
{this.generateWarningMessage()}
{this.generateErrorMessage()}
{this.entities?.map((e) => {
Expand Down
7 changes: 5 additions & 2 deletions ui/src/components/ConfigurationFormView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ function ConfigurationFormView({ serviceName }) {
});
}, [serviceName]);

const handleSubmit = () => {
form.current.handleSubmit();
/**
* @param event {React.MouseEvent<HTMLButtonElement>}
*/
const handleSubmit = (event) => {
form.current.handleSubmit(event);
};

const handleFormSubmit = (set) => {
Expand Down
7 changes: 4 additions & 3 deletions ui/src/components/EntityModal/EntityModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import styled from 'styled-components';
import WaitSpinner from '@splunk/react-ui/WaitSpinner';
import { _ } from '@splunk/ui-utils/i18n';

import { MODE_CLONE, MODE_CREATE, MODE_EDIT, Mode } from '../../constants/modes';
import { ButtonClickHandler } from '@splunk/react-ui/Button';
import { Mode, MODE_CLONE, MODE_CREATE, MODE_EDIT } from '../../constants/modes';
import { StyledButton } from '../../pages/EntryPageStyle';
import BaseFormView from '../BaseFormView';

Expand Down Expand Up @@ -53,8 +54,8 @@ class EntityModal extends Component<EntityModalProps, EntityModalState> {
this.props.handleRequestClose();
};

handleSubmit = () => {
const result = this.form.current?.handleSubmit();
handleSubmit: ButtonClickHandler = (e) => {
const result = this.form.current?.handleSubmit(e);
if (result) {
this.handleRequestClose();
}
Expand Down
6 changes: 4 additions & 2 deletions ui/src/components/EntityPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { variables } from '@splunk/themes';

import Heading from '@splunk/react-ui/Heading';
import styled from 'styled-components';
import { ButtonClickHandler } from '@splunk/react-ui/Button';
import { MODE_CLONE, MODE_CREATE, MODE_EDIT, Mode } from '../constants/modes';
import BaseFormView from './BaseFormView';
import { SubTitleComponent } from '../pages/Input/InputPageStyle';
Expand Down Expand Up @@ -55,8 +56,8 @@ function EntityPage({
buttonText = _('Update');
}

const handleSubmit = () => {
const result = form.current?.handleSubmit();
const handleSubmit: ButtonClickHandler = (e) => {
const result = form.current?.handleSubmit(e);
if (result) {
handleRequestClose();
}
Expand Down Expand Up @@ -107,6 +108,7 @@ function EntityPage({
style={{ width: '80px' }}
/>
<StyledButton
type="Submit"
appearance="primary"
label={isSubmitting ? <WaitSpinner /> : buttonText}
onClick={handleSubmit}
Expand Down

0 comments on commit b7f179a

Please sign in to comment.