fix(lit): add requestUpdate to TextField, DateTimeInput, CheckBox#1034
fix(lit): add requestUpdate to TextField, DateTimeInput, CheckBox#1034ppazosp wants to merge 2 commits intogoogle:mainfrom
Conversation
Add `updateBoundData()` helper to Root base class that encapsulates the setData + requestUpdate pattern. Refactor Slider to use it. Closes google#597
There was a problem hiding this comment.
Code Review
This pull request refactors data-binding logic across several UI components by introducing a centralized updateBoundData method in the Root base class, which reduces duplication and ensures consistent state updates. Feedback suggests further optimizing this by moving the path-existence guard clauses into the base method to simplify subclass implementations.
| protected updateBoundData(relativePath: string, value: DataValue) { | ||
| if (!this.processor) { | ||
| return; | ||
| } | ||
| this.processor.setData( | ||
| this.component, | ||
| relativePath, | ||
| value, | ||
| this.surfaceId ?? A2uiMessageProcessor.DEFAULT_SURFACE_ID | ||
| ); | ||
| this.requestUpdate(); | ||
| } |
There was a problem hiding this comment.
To further reduce code duplication, you could move the guard clauses from the subclasses into this helper method. This would make the updateBoundData method more robust and simplify the calling code in Checkbox, DateTimeInput, Slider, and TextField.
The signature could be changed to accept the object containing the path, rather than the path string itself.
For example:
protected updateBoundData(valueHolder: { path?: string } | null, value: DataValue) {
if (!valueHolder || !('path' in valueHolder) || !valueHolder.path) {
return;
}
if (!this.processor) {
return;
}
this.processor.setData(
this.component,
valueHolder.path,
value,
this.surfaceId ?? A2uiMessageProcessor.DEFAULT_SURFACE_ID
);
this.requestUpdate();
}Then, in the subclasses, the call would be simplified to this.updateBoundData(this.value, value); (or this.text) without any preceding guards.
protected updateBoundData(valueHolder: { path?: string } | null, value: DataValue) {
if (!valueHolder || !('path' in valueHolder) || !valueHolder.path) {
return;
}
if (!this.processor) {
return;
}
this.processor.setData(
this.component,
valueHolder.path,
value,
this.surfaceId ?? A2uiMessageProcessor.DEFAULT_SURFACE_ID
);
this.requestUpdate();
}
Description
Refactors
TextField,DateTimeInput, andCheckBox(v0.8) to use theupdateBoundData()helper introduced in #1022, ensuring these components callrequestUpdate()aftersetData().Without this fix, these components update the data model but do not trigger a re-render, so other components bound to the same data path will not reflect the new value.
Fixes #1023
Depends on #1022
Pre-launch Checklist
If you need help, consider asking for advice on the discussion board.