forked from rstudio/shiny
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumber.ts
More file actions
88 lines (75 loc) · 2.41 KB
/
Copy pathnumber.ts
File metadata and controls
88 lines (75 loc) · 2.41 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import $ from "jquery";
import { $escape, hasDefinedProperty, updateLabel } from "../../utils";
import { TextInputBindingBase } from "./text";
type NumberHTMLElement = HTMLInputElement;
type NumberReceiveMessageData = {
label: string;
value?: string | null;
min?: string | null;
max?: string | null;
step?: string | null;
};
function getLabelNode(el: NumberHTMLElement): JQuery<HTMLElement> {
return $(el)
.parent()
.find('label[for="' + $escape(el.id) + '"]');
}
class NumberInputBinding extends TextInputBindingBase {
find(scope: HTMLElement): JQuery<HTMLElement> {
// Inputs also have .shiny-input-number class
return $(scope).find('input[type="number"]');
}
getValue(
el: NumberHTMLElement
): string[] | number | string | null | undefined {
const numberVal = $(el).val();
if (typeof numberVal == "string") {
if (/^\s*$/.test(numberVal))
// Return null if all whitespace
return null;
}
// If valid Javascript number string, coerce to number
const numberValue = Number(numberVal);
if (!isNaN(numberValue)) {
return numberValue;
}
return numberVal; // If other string like "1e6", send it unchanged
}
setValue(el: NumberHTMLElement, value: number): void {
el.value = "" + value;
}
getType(el: NumberHTMLElement): string {
return "shiny.number";
el;
}
async receiveMessage(
el: NumberHTMLElement,
data: NumberReceiveMessageData
): Promise<void> {
// Setting values to `""` will remove the attribute value from the DOM element.
// The attr key will still remain, but there is not value... ex: `<input id="foo" type="number" min max/>`
if (hasDefinedProperty(data, "value")) el.value = data.value ?? "";
if (hasDefinedProperty(data, "min")) el.min = data.min ?? "";
if (hasDefinedProperty(data, "max")) el.max = data.max ?? "";
if (hasDefinedProperty(data, "step")) el.step = data.step ?? "";
await updateLabel(data.label, getLabelNode(el));
$(el).trigger("change");
}
getState(el: NumberHTMLElement): {
label: string;
value: ReturnType<NumberInputBinding["getValue"]>;
min: number;
max: number;
step: number;
} {
return {
label: getLabelNode(el).text(),
value: this.getValue(el),
min: Number(el.min),
max: Number(el.max),
step: Number(el.step),
};
}
}
export { NumberInputBinding };
export type { NumberReceiveMessageData };