-
Notifications
You must be signed in to change notification settings - Fork 757
Expand file tree
/
Copy pathset-element-value.effect.ts
More file actions
28 lines (26 loc) · 1.03 KB
/
set-element-value.effect.ts
File metadata and controls
28 lines (26 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { Inputs } from "../index";
import { Observable } from "rxjs/Observable";
import * as KeyupEvent from "../messages/KeyupEvent";
import { tap } from "rxjs/operators/tap";
import { withLatestFrom } from "rxjs/operators/withLatestFrom";
import { EffectNames } from "../effects";
export function setElementValueEffect(
xs: Observable<KeyupEvent.IncomingPayload>,
inputs: Inputs
) {
return xs.pipe(
withLatestFrom(inputs.document$),
tap(([event, document]) => {
const elems = document.getElementsByTagName(event.tagName);
const match = elems[event.index];
if (match) {
const nativeInputValueSetter = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(match), 'value').set;
nativeInputValueSetter.call(match, event.value);
match.dispatchEvent(new Event('change', { bubbles: true }));
}
})
);
}
export function setElementValue(event: KeyupEvent.IncomingPayload) {
return [EffectNames.SetElementValue, event];
}