-
Notifications
You must be signed in to change notification settings - Fork 240
/
value.ts
151 lines (128 loc) Β· 3.47 KB
/
value.ts
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import {isElementType} from '../utils'
import {prepareInterceptor} from './interceptor'
import {setUISelection} from './selection'
const UIValue = Symbol('Displayed value in UI')
const InitialValue = Symbol('Initial value to compare on blur')
const TrackChanges = Symbol('Track programmatic changes for React workaround')
type Value = {
[UIValue]?: typeof UIValue
toString(): string
}
declare global {
interface Element {
[UIValue]?: string
[InitialValue]?: string
[TrackChanges]?: {
previousValue?: string
tracked?: string[]
nextValue?: string
}
}
}
function valueInterceptor(
this: HTMLInputElement | HTMLTextAreaElement,
v: Value | string,
) {
const isUI = typeof v === 'object' && v[UIValue]
if (isUI) {
this[UIValue] = String(v)
setPreviousValue(this, String(this.value))
} else {
trackOrSetValue(this, String(v))
}
return {
applyNative: !!isUI,
realArgs: sanitizeValue(this, v),
}
}
function sanitizeValue(
element: HTMLInputElement | HTMLTextAreaElement,
v: Value | string,
) {
// Workaround for JSDOM
if (
isElementType(element, 'input', {type: 'number'}) &&
String(v) !== '' &&
!Number.isNaN(Number(v))
) {
// Setting value to "1." results in `null` in JSDOM
return String(Number(v))
}
return String(v)
}
export function prepareValueInterceptor(element: HTMLInputElement) {
prepareInterceptor(element, 'value', valueInterceptor)
}
export function setUIValue(
element: HTMLInputElement | HTMLTextAreaElement,
value: string,
) {
element.value = {
[UIValue]: UIValue,
toString: () => value,
} as unknown as string
}
export function getUIValue(element: HTMLInputElement | HTMLTextAreaElement) {
return element[UIValue] === undefined
? element.value
: String(element[UIValue])
}
export function setInitialValue(
element: HTMLInputElement | HTMLTextAreaElement,
) {
element[InitialValue] = element.value
}
export function getInitialValue(
element: HTMLInputElement | HTMLTextAreaElement,
) {
return element[InitialValue]
}
function setPreviousValue(
element: HTMLInputElement | HTMLTextAreaElement,
v: string,
) {
element[TrackChanges] = {...element[TrackChanges], previousValue: v}
}
export function startTrackValue(
element: HTMLInputElement | HTMLTextAreaElement,
) {
element[TrackChanges] = {
...element[TrackChanges],
nextValue: String(element.value),
tracked: [],
}
}
function trackOrSetValue(
element: HTMLInputElement | HTMLTextAreaElement,
v: string,
) {
element[TrackChanges]?.tracked?.push(v)
if (!element[TrackChanges]?.tracked) {
setCleanValue(element, v)
}
}
function setCleanValue(
element: HTMLInputElement | HTMLTextAreaElement,
v: string,
) {
element[UIValue] = undefined
element[InitialValue] = v
// Programmatically setting the value property
// moves the cursor to the end of the input.
setUISelection(element, {focusOffset: v.length})
}
/**
* @returns `true` if we recognize a React state reset and update
*/
export function endTrackValue(element: HTMLInputElement | HTMLTextAreaElement) {
const changes = element[TrackChanges]
element[TrackChanges] = undefined
const isJustReactStateUpdate =
changes?.tracked?.length === 2 &&
changes.tracked[0] === changes.previousValue &&
changes.tracked[1] === changes.nextValue
if (changes?.tracked?.length && !isJustReactStateUpdate) {
setCleanValue(element, changes.tracked[changes.tracked.length - 1])
}
return isJustReactStateUpdate
}