Skip to content

Commit

Permalink
Merge pull request #4410 from marmelab/fix-save-on-enter
Browse files Browse the repository at this point in the history
Fix saveOnEnter : Use normal click on Save button and fix helperText linked issue
  • Loading branch information
fzaninotto authored Feb 12, 2020
2 parents b1c228f + 6f16ef2 commit 196cb43
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 20 deletions.
32 changes: 32 additions & 0 deletions cypress/integration/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,38 @@ describe('Create Page', () => {
EditPage.delete();
});

it('should redirect to edit page after submit on enter', () => {
const values = [
{
type: 'input',
name: 'title',
value: 'Test title',
},
{
type: 'textarea',
name: 'teaser',
value: 'Test teaser',
},
{
type: 'rich-text-input',
name: 'body',
value: 'Test body',
},
];

CreatePage.setValues(values);
CreatePage.submitWithKeyboard();
EditPage.waitUntilVisible();
cy.get(EditPage.elements.input('title')).should(el =>
expect(el).to.have.value('Test title')
);
cy.get(EditPage.elements.input('teaser')).should(el =>
expect(el).to.have.value('Test teaser')
);

EditPage.delete();
});

it('should redirect to show page after create success with "Save and show"', () => {
const values = [
{
Expand Down
7 changes: 7 additions & 0 deletions cypress/support/CreatePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ export default url => ({
cy.wait(200); // let the notification disappear (could block further submits)
},

submitWithKeyboard() {
cy.get('input:first').type('{enter}');
cy.get(this.elements.snackbar);
cy.get(this.elements.body).click(); // dismiss notification
cy.wait(200); // let the notification disappear (could block further submits)
},

submitAndShow() {
cy.get(this.elements.submitAndShowButton).click();
cy.get(this.elements.snackbar);
Expand Down
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
6 changes: 3 additions & 3 deletions packages/ra-ui-materialui/src/button/SaveButton.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('<SaveButton />', () => {
</TestContext>
);

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

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

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

Expand All @@ -77,7 +77,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 a helperText prop. Set the helperText prop on the child component instead"
);

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

0 comments on commit 196cb43

Please sign in to comment.