Skip to content

Commit 5502d33

Browse files
committed
feat: save dom input values
1 parent 73a86af commit 5502d33

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/app/app.module.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const APP_PROVIDERS = [
2626
];
2727

2828
type StoreType = {
29+
$inputs: any[],
2930
state: InteralStateType,
3031
disposeOldHosts: () => void
3132
};
@@ -55,25 +56,48 @@ type StoreType = {
5556
})
5657
export class AppModule {
5758
constructor(public appRef: ApplicationRef, public appState: AppState) {}
59+
5860
hmrOnInit(store: StoreType) {
5961
if (!store || !store.state) return;
6062
console.log('HMR store', store);
63+
// set state
6164
this.appState._state = store.state;
65+
66+
// set input values
67+
const inputs = document.querySelectorAll('input');
68+
if (store.$inputs && inputs.length === store.$inputs.length) {
69+
store.$inputs.forEach((value, i) => {
70+
let el = inputs[i];
71+
el.value = value;
72+
el.dispatchEvent(new CustomEvent('input', {detail: el.value}));
73+
});
74+
}
75+
6276
this.appRef.tick();
6377
delete store.state;
6478
}
79+
6580
hmrOnDestroy(store: StoreType) {
6681
const cmpLocation = this.appRef.components.map(cmp => cmp.location.nativeElement);
6782
// recreate elements
83+
// save state
6884
const state = this.appState._state;
6985
store.state = state;
7086
store.disposeOldHosts = createNewHosts(cmpLocation);
87+
88+
// save input values
89+
const inputs = document.querySelectorAll('input');
90+
const $inputs = [].slice.call(inputs).map(input => input.value);
91+
store.$inputs = $inputs;
7192
// remove styles
7293
removeNgStyles();
7394
}
95+
7496
hmrAfterDestroy(store: StoreType) {
7597
// display new elements
7698
store.disposeOldHosts();
7799
delete store.disposeOldHosts;
78100
}
101+
79102
}
103+

0 commit comments

Comments
 (0)