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

Fix saveOnEnter : Use normal click on Save button and fix helperText linked issue #4410

Merged
merged 10 commits into from
Feb 12, 2020
2 changes: 1 addition & 1 deletion packages/ra-input-rich-text/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const RichTextInput = ({
toolbar = true,
fullWidth = true,
configureQuill,
helperText = false,
helperText,
label,
source,
resource,
Expand Down
52 changes: 49 additions & 3 deletions packages/ra-ui-materialui/src/button/SaveButton.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import React from 'react';
import expect from 'expect';
import { TestContext } from 'ra-core';

import Create from '../detail/Create';
import SimpleForm from '../form/SimpleForm';
import Toolbar from '../form/Toolbar';
import TextInput from '../input/TextInput';
import SaveButton from './SaveButton';

describe('<SaveButton />', () => {
Expand Down Expand Up @@ -41,8 +45,50 @@ describe('<SaveButton />', () => {
/>
</TestContext>
);
fireEvent.click(getByLabelText('ra.action.save'));
expect(onSubmit).toHaveBeenCalled();
});

it('should trigger submit action when Enter key if no saving is in progress', () => {
JulienMattiussi marked this conversation as resolved.
Show resolved Hide resolved
const onSubmit = jest.fn();

const defaultCreateProps = {
basePath: '/foo',
id: '123',
resource: 'foo',
location: {},
match: {},
};

const SaveToolbar = props => (
<Toolbar {...props}>
<SaveButton
label="enterButton"
handleSubmitWithRedirect={onSubmit}
submitOnEnter={true}
saving={false}
/>
</Toolbar>
);

const { getByLabelText } = render(
<TestContext>
<Create {...defaultCreateProps}>
JulienMattiussi marked this conversation as resolved.
Show resolved Hide resolved
<SimpleForm toolbar={<SaveToolbar />}>
<TextInput label="Title" source="title" />
</SimpleForm>
</Create>
</TestContext>
);

const input = getByLabelText('Title');
fireEvent.focus(input);
fireEvent.change(input, { target: { value: 'test' } });

fireEvent.mouseDown(getByLabelText('ra.action.save'));
const button = getByLabelText('enterButton');
JulienMattiussi marked this conversation as resolved.
Show resolved Hide resolved
//console.log(button);
JulienMattiussi marked this conversation as resolved.
Show resolved Hide resolved
//fireEvent.click(getByLabelText('ra.action.save'));
fireEvent.keyPress(input, { key: 'Enter', keyCode: 13 });
expect(onSubmit).toHaveBeenCalled();
});

Expand All @@ -55,7 +101,7 @@ describe('<SaveButton />', () => {
</TestContext>
);

fireEvent.mouseDown(getByLabelText('ra.action.save'));
fireEvent.click(getByLabelText('ra.action.save'));
expect(onSubmit).not.toHaveBeenCalled();
});

Expand All @@ -77,7 +123,7 @@ describe('<SaveButton />', () => {
</TestContext>
);

fireEvent.mouseDown(getByLabelText('ra.action.save'));
fireEvent.click(getByLabelText('ra.action.save'));
expect(dispatchSpy).toHaveBeenCalledWith({
payload: {
message: 'ra.message.invalid_form',
Expand Down
16 changes: 1 addition & 15 deletions packages/ra-ui-materialui/src/button/SaveButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,7 @@ const SaveButton: FC<SaveButtonProps> = ({
const notify = useNotify();
const translate = useTranslate();

// We handle the click event through mousedown because of an issue when
// the button is not as the same place when mouseup occurs, preventing the click
// event to fire.
// It can happen when some errors appear under inputs, pushing the button
// towards the window bottom.
const handleMouseDown = event => {
const handleClick = event => {
if (saving) {
// prevent double submission
event.preventDefault();
Expand All @@ -56,22 +51,13 @@ const SaveButton: FC<SaveButtonProps> = ({
}
};

// As we handle the "click" through the mousedown event, we have to make sure we cancel
// the default click in case the issue mentionned above does not occur.
// Otherwise, this would trigger a standard HTML submit, not the final-form one.
const handleClick = event => {
event.preventDefault();
event.stopPropagation();
};

const type = submitOnEnter ? 'submit' : 'button';
const displayedLabel = label && translate(label, { _: label });
return (
<Button
className={classnames(classes.button, className)}
variant={variant}
type={type}
onMouseDown={handleMouseDown}
onClick={handleClick}
color={saving ? 'default' : 'primary'}
aria-label={displayedLabel}
Expand Down
13 changes: 12 additions & 1 deletion packages/ra-ui-materialui/src/input/ReferenceInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
InputProps,
Pagination,
Sort,
warning as warningLog,
} from 'ra-core';

import sanitizeInputProps from './sanitizeRestProps';
Expand Down Expand Up @@ -192,7 +193,7 @@ interface ReferenceInputViewProps {
classes?: object;
className?: string;
error?: string;
helperText?: string;
helperText?: string | boolean;
id: string;
input: FieldInputProps<any, HTMLElement>;
isRequired: boolean;
Expand Down Expand Up @@ -268,6 +269,15 @@ export const ReferenceInputView: FunctionComponent<ReferenceInputViewProps> = ({
}
: meta;

// helperText should never be set on ReferenceInput, only in child component
// But in a Filter component, the child helperText have to be forced to false
warningLog(
helperText !== undefined && helperText !== false,
"<ReferenceInput> doesn't accept an helperText value, it should be set on the child component"
JulienMattiussi marked this conversation as resolved.
Show resolved Hide resolved
);

const disabledHelperText = helperText === false ? { helperText } : {};

return cloneElement(children, {
allowEmpty,
classes,
Expand All @@ -284,6 +294,7 @@ export const ReferenceInputView: FunctionComponent<ReferenceInputViewProps> = ({
setPagination,
setSort,
translateChoice: false,
...disabledHelperText,
...sanitizeRestProps(rest),
});
};
Expand Down