From 42d95cbfc3d85a8ab79e5ac6fceee5d8f7c6c550 Mon Sep 17 00:00:00 2001 From: David Monjoie Date: Thu, 11 May 2017 13:29:52 +0200 Subject: [PATCH] [FIX] web: keep focus on checkboxes after rerendering Previously, when a checkbox was re-rendered, it was losing focus since it was removed and re-created. Now, if the checkbox itself is the origin point of the re-rendering, we give the focus to the newly re-rendered checkbox, resulting in a seamless experience just as if the checkbox was never removed and re-rendered. --- addons/web/static/src/js/fields/basic_fields.js | 14 ++++++++++++++ .../web/static/tests/fields/basic_fields_tests.js | 8 +++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/addons/web/static/src/js/fields/basic_fields.js b/addons/web/static/src/js/fields/basic_fields.js index 6fc1736870f..b1b51e69e36 100644 --- a/addons/web/static/src/js/fields/basic_fields.js +++ b/addons/web/static/src/js/fields/basic_fields.js @@ -613,6 +613,20 @@ var FieldBoolean = AbstractField.extend({ isSet: function () { return true; }, + /** + * When the checkbox is rerendered, we need to check if it was the actual + * origin of the reset. If it is, we need to activate it back so it looks + * like it was not rerendered but is still the same input. + * + * @override + */ + reset: function (record, event) { + var rendered = this._super.apply(this, arguments); + if (event && event.target.name === this.name) { + this.activate(); + } + return rendered; + }, //-------------------------------------------------------------------------- // Private diff --git a/addons/web/static/tests/fields/basic_fields_tests.js b/addons/web/static/tests/fields/basic_fields_tests.js index 04d0588bfab..8417e503676 100644 --- a/addons/web/static/tests/fields/basic_fields_tests.js +++ b/addons/web/static/tests/fields/basic_fields_tests.js @@ -123,7 +123,7 @@ QUnit.module('basic_fields', { QUnit.module('FieldBoolean'); QUnit.test('boolean field in form view', function (assert) { - assert.expect(11); + assert.expect(12); var form = createView({ View: FormView, @@ -183,6 +183,12 @@ QUnit.module('basic_fields', { .trigger({type: "keyup", which: $.ui.keyCode.ENTER}); assert.strictEqual(form.$('.o_field_boolean input:checked').length, 1, "checkbox should now be checked"); + // blindly press enter again, it should uncheck the checkbox + $(document.activeElement).trigger({type: "keydown", which: $.ui.keyCode.ENTER}); + assert.strictEqual(form.$('.o_field_boolean input:checked').length, 0, + "checkbox should not be checked"); + // blindly press enter again, it should check the checkbox back + $(document.activeElement).trigger({type: "keydown", which: $.ui.keyCode.ENTER}); // save form.$buttons.find('.o_form_button_save').click();