Skip to content

Commit bc90500

Browse files
authored
Add proposed code to index.ts to make FlunetNukmberField work in SSR mode icw data-enhance (#4181)
1 parent e58efbc commit bc90500

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

src/Core.Assets/src/global.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export { };
2+
declare global {
3+
interface Window {
4+
__fluentnumberfieldObserver: MutationObserver
5+
}
6+
}

src/Core.Assets/src/index.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,41 @@ var beforeStartCalled = false;
9393
var afterStartedCalled = false;
9494

9595

96+
function attachfluentnumberfieldObserver() {
97+
if (!document.body) return;
98+
// Disconnect existing observers
99+
if (window.__fluentnumberfieldObserver) {
100+
window.__fluentnumberfieldObserver.disconnect();
101+
}
102+
103+
function reAddCurrentValue(target: Element) {
104+
target.setAttribute('current-value', target.getAttribute('value') ?? '');
105+
}
106+
const observer = new MutationObserver((mutations) => {
107+
for (const mutation of mutations) {
108+
if (
109+
mutation.target instanceof Element &&
110+
mutation.type === 'attributes' &&
111+
mutation.attributeName === 'current-value' &&
112+
mutation.target.hasAttribute('value') &&
113+
!mutation.target.hasAttribute('current-value')
114+
) {
115+
reAddCurrentValue(mutation.target);
116+
}
117+
}
118+
});
119+
120+
// Observe all fields
121+
document.querySelectorAll('fluent-number-field').forEach(field => {
122+
observer.observe(field, {
123+
attributes: true,
124+
attributeFilter: ['current-value'],
125+
});
126+
});
127+
128+
window.__fluentnumberfieldObserver = observer;
129+
}
130+
96131
export function afterWebStarted(blazor: any) {
97132
if (!afterStartedCalled) {
98133
afterStarted(blazor, 'web');
@@ -343,6 +378,10 @@ export function afterStarted(blazor: Blazor, mode: string) {
343378
blazor.addEventListener('enhancedload', onEnhancedLoad);
344379
}
345380

381+
if (mode === 'web') {
382+
blazor.addEventListener('enhancednavigationstart', attachfluentnumberfieldObserver);
383+
}
384+
346385
afterStartedCalled = true;
347386
}
348387

0 commit comments

Comments
 (0)