feat: 🎸 form should support re-entering edit mode on success
This commit is contained in:
2 files changed
+59
-2
No files matched your search
@@ -40,13 +40,27 @@ export default class RoseFormComponent extends Component {
|
||||
/**
|
||||
* Calls the passed `onSubmit` function while disabling the default form
|
||||
* submit behavior.
|
||||
*
|
||||
* Submit handlers _may optionally return a promise_. If they do, the promise
|
||||
* is assumed to reflect the state of the submission operation,
|
||||
* i.e. if the promise resolves the submission was successful and if it
|
||||
* rejects, the submission failed.
|
||||
* @param {Event} e
|
||||
*/
|
||||
@action
|
||||
handleSubmit(e) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
this.args.onSubmit();
|
||||
const submitResult = this.args.onSubmit();
|
||||
// If the submit handler returns a promise, we want to handle success
|
||||
// by re-enabling read-only mode on the form (if applicable).
|
||||
// Since submit is allowed to fail in this context and we don't want
|
||||
// unhandled rejection warnings, we have a no-op catch.
|
||||
if (submitResult?.then) {
|
||||
submitResult
|
||||
.then(() => this.isEditable = false)
|
||||
.catch(() => {});
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ import { module, test } from 'qunit';
|
||||
import { setupRenderingTest } from 'ember-qunit';
|
||||
import { render, find, findAll, click } from '@ember/test-helpers';
|
||||
import hbs from 'htmlbars-inline-precompile';
|
||||
import { resolve, reject } from 'rsvp';
|
||||
|
||||
module('Integration | Component | rose/form', function (hooks) {
|
||||
setupRenderingTest(hooks);
|
||||
@@ -70,7 +71,7 @@ module('Integration | Component | rose/form', function (hooks) {
|
||||
this.cancel = () => {
|
||||
assert.ok(true, 'cancel function may still be passed even for @showEditToggle');
|
||||
};
|
||||
this.onSubmit = () => {};
|
||||
this.submit = () => {};
|
||||
await render(hbs`
|
||||
<Rose::Form
|
||||
@onSubmit={{this.submit}}
|
||||
@@ -102,4 +103,46 @@ module('Integration | Component | rose/form', function (hooks) {
|
||||
assert.equal(findAll('button').length, 1);
|
||||
assert.equal(find('button').textContent.trim(), 'Edit');
|
||||
});
|
||||
|
||||
test('it re-enables read-only mode if the submit handler returns a resolving promise', async function (assert) {
|
||||
assert.expect(15);
|
||||
this.cancel = () => {};
|
||||
await render(hbs`
|
||||
<Rose::Form
|
||||
@onSubmit={{this.submit}}
|
||||
@cancel={{this.cancel}}
|
||||
@showEditToggle={{true}}
|
||||
as |form|
|
||||
>
|
||||
<form.input @label="Label" @value="value" />
|
||||
<form.actions
|
||||
@submitText="Save"
|
||||
@cancelText="Cancel"
|
||||
@enableEditText="Edit" />
|
||||
</Rose::Form>
|
||||
`);
|
||||
// Before enabling edit mode, fields are disabled and the edit mode button is displayed
|
||||
assert.ok(find('.rose-form'));
|
||||
assert.equal(findAll('input[disabled]').length, 1);
|
||||
assert.equal(findAll('button').length, 1);
|
||||
assert.equal(find('button').textContent.trim(), 'Edit');
|
||||
// After entering edit mode, fields are enabled and save/cancel buttons are displayed
|
||||
await click('button');
|
||||
assert.equal(findAll('input[disabled]').length, 0);
|
||||
assert.equal(findAll('button').length, 2);
|
||||
assert.equal(find('[type="submit"]').textContent.trim(), 'Save');
|
||||
assert.equal(find('button:not([type="submit"])').textContent.trim(), 'Cancel');
|
||||
// After saving with failure, fields are enabled and save/cancel buttons are displayed
|
||||
this.submit = () => reject();
|
||||
assert.equal(findAll('input[disabled]').length, 0);
|
||||
assert.equal(findAll('button').length, 2);
|
||||
assert.equal(find('[type="submit"]').textContent.trim(), 'Save');
|
||||
assert.equal(find('button:not([type="submit"])').textContent.trim(), 'Cancel');
|
||||
// After saving with success, fields are disabled again and the edit mode button is displayed
|
||||
this.submit = () => resolve();
|
||||
await click('button[type="submit"]');
|
||||
assert.equal(findAll('input[disabled]').length, 1);
|
||||
assert.equal(findAll('button').length, 1);
|
||||
assert.equal(find('button').textContent.trim(), 'Edit');
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user