Skip to content

Commit 30e8cc0

Browse files
fix: sync editor value to server when same value is typed again (#9568) (CP: 25.2) (#9572)
This PR cherry-picks changes from the original PR #9568 to branch 25.2. --- #### Original PR description > ## Description > > Since a recent change, Firefox treats input fields that have the same value on blur as they had on focus as unchanged, even if the value was changed programmatically in between. This is problematic for custom editors in GridPro, where the value is set programmatically from the server after the client-side component has already focused the editor. If, after starting editing, a user types exactly the same value that the input had before the server updated the editor value, no change event is fired and the editor value is never synced to the server where it would be used to update the item data: > - First edit: > - User edits cell, types "foo", commits > - "foo" is different from the value the editor had before focus (empty value), property is synced to the server > - `item-property-change` fires, server detects change in the editor's value, item is updated on server > - Second edit (same column, different cell) > - User starts editing, web component focuses editor component immediately > - Initially the client-side value is still "foo", the server only updates the editor's value once it receives the `cell-edit-started` event, which eventually results in a client-side update of the editor value, let's say to "bar" > - User types "foo" again, commits > - "foo" is not different from the value the editor had before focusing it, no change event is fired for the editor, it's value is not synced back to the server > - `item-property-change` fires, server does not detect a change in the editor's value, no item update > > This change addresses the issue by always firing a synthetic change event whenever an input element loses focus. The fix is scoped only Firefox, and only to custom editors. For built-in editors no such fix is needed as the edited value is applied by the web component before focusing. The change is not covered by tests, as the issue only manifests in Firefox whereas tests only run in Chrome. > > Fixes #9437 > > ## Type of change > > - Bugfix > Co-authored-by: Sascha Ißbrücker <sissbruecker@vaadin.com>
1 parent 17cfd69 commit 30e8cc0

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

  • vaadin-grid-pro-flow-parent/vaadin-grid-pro-flow/src/main

‎vaadin-grid-pro-flow-parent/vaadin-grid-pro-flow/src/main/java/com/vaadin/flow/component/gridpro/GridPro.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ private void setup() {
151151
var itemKey = getDataCommunicator().getKeyMapper()
152152
.key(e.getItem());
153153
UI.getCurrentOrThrow().getPage().executeJs(
154-
"window.Vaadin.Flow.gridProConnector.selectAll($0, $1, $2)",
154+
"window.Vaadin.Flow.gridProConnector.handleCustomEditorValueUpdate($0, $1, $2)",
155155
column.getEditorField().getElement(), itemKey,
156156
this.getElement());
157157
}

‎vaadin-grid-pro-flow-parent/vaadin-grid-pro-flow/src/main/resources/META-INF/resources/frontend/gridProConnector.js‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* See <https://vaadin.com/commercial-license-and-service-terms> for the full
88
* license.
99
*/
10+
import { isFirefox } from '@vaadin/component-base/src/browser-utils.js';
1011
import { iterateRowCells, updatePart } from '@vaadin/grid/src/vaadin-grid-helpers.js';
1112

1213
function isEditedRow(grid, rowData) {
@@ -16,7 +17,7 @@ function isEditedRow(grid, rowData) {
1617
const LOADING_EDITOR_CELL_ATTRIBUTE = 'loading-editor';
1718

1819
window.Vaadin.Flow.gridProConnector = {
19-
selectAll: (editor, itemKey, grid) => {
20+
handleCustomEditorValueUpdate: (editor, itemKey, grid) => {
2021
if (editor.__itemKey !== itemKey) {
2122
// This is an outdated call that can occur if the user starts editing a cell,
2223
// and quickly starts editing another cell on the same column before the editor
@@ -61,6 +62,21 @@ window.Vaadin.Flow.gridProConnector = {
6162
stopCellEdit.apply(this, arguments);
6263
this._grid.toggleAttribute(LOADING_EDITOR_CELL_ATTRIBUTE, false);
6364
};
65+
66+
if (isFirefox && !component.__firefoxChangeWorkaroundInstalled) {
67+
// The editor is focused before its value is set from the server. Firefox
68+
// treats an input as unchanged when its value on blur equals the value it
69+
// had on focus, ignoring programmatic changes in between. So typing the
70+
// value from the previous edit again would not fire a change event and
71+
// the cell would not update. As a workaround, always fire a synthetic
72+
// change event when an input loses focus.
73+
component.addEventListener('focusout', (e) => {
74+
if (e.target instanceof HTMLInputElement) {
75+
e.target.dispatchEvent(new Event('change', { bubbles: true }));
76+
}
77+
});
78+
component.__firefoxChangeWorkaroundInstalled = true;
79+
}
6480
},
6581

6682
patchEditModeRenderer(column) {

0 commit comments

Comments
 (0)