-
Notifications
You must be signed in to change notification settings - Fork 757
Expand file tree
/
Copy pathset-element-toggle-value.effect.ts
More file actions
36 lines (34 loc) · 1.38 KB
/
set-element-toggle-value.effect.ts
File metadata and controls
36 lines (34 loc) · 1.38 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
29
30
31
32
33
34
35
36
import { Inputs } from "../index";
import { Observable } from "rxjs/Observable";
import * as FormToggleEvent from "../messages/FormToggleEvent";
import { tap } from "rxjs/operators/tap";
import { withLatestFrom } from "rxjs/operators/withLatestFrom";
import { EffectNames } from "../effects";
export function setElementToggleValueEffect(
xs: Observable<FormToggleEvent.IncomingPayload>,
inputs: Inputs
) {
return xs.pipe(
withLatestFrom(inputs.document$),
tap(([event, document]) => {
const elems = document.getElementsByTagName(event.tagName);
const match = <HTMLInputElement>elems[event.index];
if (match) {
if (event.type === "radio") {
match.checked = true;
}
if (event.type === "checkbox") {
match.checked = event.checked;
}
if (event.tagName === "SELECT") {
const nativeInputValueSetter = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(match), 'value').set;
nativeInputValueSetter.call(match, event.value);
match.dispatchEvent(new Event('change', { bubbles: true }));
}
}
})
);
}
export function setElementToggleValue(event: FormToggleEvent.IncomingPayload) {
return [EffectNames.SetElementToggleValue, event];
}